Ruby on rails after_remove, after_add has_many callbacks: via

I have a model that matches the following diagram:

class foo < ActiveRecord::Base

  has_many :bar, :dependent => :destroy

  has_many :baz, :through => :bar, :uniq => true,
    :after_add => :update_baz_count,
    :after_remove => :update_baz_count

  def update_baz_count(record)
    debugger
    # stuff...
  end
end

I am trying to maintain an account of a unique baz associated with foo through bar. But for some reason, subsequent after_add and after_remove callbacks are never called when I add a panel (which should have a baz) to foo. Any ideas why? I used these callbacks with habtm and they work great.

Thank.

+5
source share
2 answers

, . after_create after_destroy bar. , .

#in Bar class
after_create :update_foo_baz_count
after_destroy :update_foo_baz_count

def update_foo_baz_count
  self.foo.update_baz_count
end
+5

, after_destroy has_many :through

:

: through - , , , .

add/remove, , .

+5

All Articles