Created
June 23, 2009 08:23
-
-
Save tomafro/134428 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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