after_commit 属性 [英] after_commit for an attribute

查看:28
本文介绍了after_commit 属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中使用了 after_commit.

I am using an after_commit in my application.

我希望它仅在我的模型中更新特定字段时触发.有人知道怎么做吗?

I would like it to trigger only when a particular field is updated in my model. Anyone know how to do that?

推荐答案

老问题,但这是我发现的一种方法,可能适用于 after_commit 回调(处理 paukul 的回答).至少,这些值都在 IRB 提交后保持不变.

Old question, but this is one method that I've found that might work with the after_commit callback (working off paukul's answer). At least, the values both persist post-commit in IRB.

after_commit :callback, 
  if: proc { |record| 
    record.previous_changes.key?(:attribute) &&
      record.previous_changes[:attribute].first != record.previous_changes[:attribute].last
  }

这篇关于after_commit 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆