【发布时间】:2011-06-16 02:18:10
【问题描述】:
所以我有一个模型 foo,它有_and_belongs_to_many bar。
foo.bar 给了我所有与 foo 相关的栏。每次我删除一个 bar 时,我都会通过 :after_remove 回调调用 Foo.decrement_counter(:bar_count, self[:id]) 。
我正在测试删除与 foo 无关的栏。这也减少了计数器......这是我不想要的。如何确保仅在实际从关联中删除条时才减少计数器?
has_and_belongs_to_many :bar, :after_remove => :dec_bar_count, :after_add => :inc_bar_count, :uniq => true
def dec_bar_count(record)
Foo.decrement_counter(:bar_count, self[:id])
end
谢谢。
【问题讨论】:
-
能否发布您用于
after_remove回调的完整代码? -
我已经用代码更新了问题。
标签: ruby-on-rails callback associations counter