Skip to content

Instantly share code, notes, and snippets.

@tomafro
Created June 23, 2009 08:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomafro/134428 to your computer and use it in GitHub Desktop.
Save tomafro/134428 to your computer and use it in GitHub Desktop.
# Is this crazy?
class AccountRole < ActiveRecord::Base
belongs_to :account
belongs_to :target, :polymorphic => true
validates_presence_of :role
end
class Project < ActiveRecord::Base
has_many :account_roles, :as => :target
has_many :admins, :through => :account_roles, :source => :account, :conditions => {:account_roles => {:role => "admin"}} do
# This might be the crazy bit. Or it might be the genius bit. I'm not sure.
def add(account)
AccountRole.create!(:role => 'admin', :target => proxy_owner, :account => account)
end
def remove(account)
# Note, scoping should automatically fill in the target conditions
AccountRole.destroy_all(:conditions => {:account_id => account_id, :role => 'admin'})
end
end
end
# Usage:
Project.first.admins.add(an_account)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment