Modifying an inherited Rails association
Posted
by Chris Kilmer
on Stack Overflow
See other posts from Stack Overflow
or by Chris Kilmer
Published on 2010-06-15T22:23:54Z
Indexed on
2010/06/15
22:32 UTC
Read the original article
Hit count: 147
ruby-on-rails
|ruby
I have a Team class that inherits from a Group class. Both Team and Groups have memberships through the same association. However, I need to run a method after a Team memberships is added but not a Group. I currently have something like this:
class Group < ActiveRecord::Base
has_many :memberships,
:class_name => 'Connection',
:foreign_key => 'connectable_id',
:as => :connectable,
:dependent => :destroy
end
class Team < Group
has_many :memberships,
:class_name => 'Connection',
:foreign_key => 'connectable_id',
:as => :connectable,
:dependent => :destroy,
:after_add => :add_company_membership
private
def membership_check(membership)
end
end
Is there some way to modify the inherited association in Team so that I don't have to redefine the entire thing but rather just add the :after_add hook it?
Any help would be appreciated.
© Stack Overflow or respective owner