加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Rails ActiveRecord CounterCache和Callbacks

发布时间:2020-12-17 02:45:17 所属栏目:百科 来源:网络整理
导读:counter_cache是??否递增和递减激活active_record回调? User AR has_many :cups after_update :do_something def do_something "Will I be called when number of cups updated ?" endendCup AR belongs_to :user,counter_cache: trueend 在上面的代码中,当
counter_cache是??否递增和递减激活active_record回调?

User << AR

  has_many :cups

  after_update :do_something

  def do_something
     "Will I be called when number of cups updated ?"
  end

end

Cup << AR

   belongs_to :user,counter_cache: true

end

在上面的代码中,当添加一个新杯并且它属于一个用户时,将调用函数do_something,将在该用户上调用update来更新cups_count,但是从我试过的看起来似乎是counter_cache更新没有火回调,可能是因为他们自己内部回调?

谢谢

解决方法

从 source用于计数器缓存,似乎ActiveRecord正在进行直接数据库更新,这将跳过回调.

update_all(updates.join(','),primary_key => id )

根据documentation for update_all,它确实跳过了回调.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读