Changing an Inherited Rails Association

I have a Team class that inherits from the Group class. Both teams and groups have membership in the same association. However, I need to run the method after adding membership to the Team, but not the 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 => :membership_check

private
   def membership_check(membership)
   end
end

Is there any way to change the inherited association in Team, so I don’t need to redefine the whole thing, but just add: after_add to intercept it?

Any help would be appreciated.

+3
source share
3 answers

Try this and let me know if that works. I should have thought. Make sure your method is membership_checkno longer private.

class Group < ActiveRecord::Base
  has_many :memberships, 
           :class_name => 'Connection',
           :foreign_key => 'connectable_id',
           :as => :connectable,
           :dependent => :destroy,
           :after_add => proc {|gr, dev| gr.membership_check(dev) if gr.respond_to? :membership_check}
end

class Team < Group
  def membership_check(membership)
  end
end

P.S. , membership_check, , , .

P.P.S membership_check private, gr.send(: membership_check, dev), . , .

+1

- add_company_membership Group , Team, , . :after_add .

, , : after_add Group, .

0

:after_add ? , , ?

0

Source: https://habr.com/ru/post/1750200/


All Articles