如果保存子关系中的对象并设置了触摸,是否有active_ordd钩子用于activerecord对象?

时间:2014-02-21 09:55:55

标签: ruby-on-rails activerecord

我有

class Users < ActiveRecord::Base
  belongs_to :team, touch: true
end

class Team < ActiveRecord::Base
  has_many :users
  before_save :update_popularity
  before_update :update_popularity

  def popularity
    #secret algorythm
  end

private
  def update_popularity
    # This is not called
  end
end


User.first.name = 'John'

当我保存用户时,我也希望更新团队人气。但是,似乎没有调用before_filter?

有没有正确的方法呢?

1 个答案:

答案 0 :(得分:0)

试试这个before_update :update_popularity

更新

在审核了touch方法here的API文档后,他们会说:

  

请注意,不会执行任何验证,只会进行after_touch回调   执行。