仅当Rails中的属性已更改时才运行回调 [英] Run a callback only if an attribute has changed in Rails

查看:133
本文介绍了仅当Rails中的属性已更改时才运行回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序具有以下关联:

 #页面
属于:状态

我想在 status_id 中的任何时候运行回调c $ c>页面已更改。



因此,如果 page.status_id 从4变为5,我希望能够抓住它。 / p>

该怎么做?

解决方案

Rails 5.1 +



  class Page< ActiveRecord :: Base 
before_save:do_something,如果::will_save_change_to_status_id?

私人

def do_something
#...
结束
结束

更改ActiveRecord :: Dirty的提交在这里: https://github.com/rails/rails/commit/16ae3db5a5c6a08383b974ae6c96faac5b4a3c81



此处是有关这些更改的博客文章: https://www.ombulabs.com/blog/ rails / upgrades / active-record-5-1-api-changes.html



这是我为自己做的关于ActiveRecord更改的摘要: :Dirty in Rails 5.1 +:



ActiveRecord :: Dirty



https://api.rubyonrails.org/classes/ActiveRecord/AttributeMethods/Dirty.html



保存前(OPTIO NAL更改)



在修改对象之后并保存到数据库之前,或者在 before_save 过滤器内:


  • 更改现在应为 changes_to_save

  • 已更改?现在应为 has_changes_to_save?

  • 已更改现在应为 changed_attribute_names_to_save

  • < attribute__change 现在应为< attribute__change_to_be_saved

  • < attribute_gt__changed?现在应该是 will_save_change_to_< attribute>?

  • < attribute__was 现在应该是< attribute__in_database



保存后(BREAKING CHANGE)



修改对象并保存到数据库后,或在<$ c $之内c> after_save 过滤器:




  • 保存的更改(重复ace 上一个更改

  • 保存的更改?

  • saved_change_to_< attribute>

  • saved_change_to_to_< attribute? / li>
  • < attribute__before_last_save






Rails< = 5.0



  class Page< ActiveRecord :: Base 
before_save:do_something,如果::status_id_changed?

私人

def do_something
#...
结束
结束

这利用了以下事实: before_save 回调可以根据方法调用的返回值有条件地执行。 status_id_changed?方法来自 ActiveModel: :Dirty ,它使我们可以通过在属性名称后附加 _changed?来检查特定属性是否已更改。



何时应调用 do_something 方法取决于您的需要。可以是 before_save after_save 或任何定义的ActiveRecord :: Callbacks


I have the following association in my app:

# Page 
belongs_to :status

I want to run a callback anytime the status_id of a page has changed.

So, if page.status_id goes from 4 to 5, I want to be able to catch that.

How to do so?

解决方案

Rails 5.1+

class Page < ActiveRecord::Base
  before_save :do_something, if: :will_save_change_to_status_id?

  private

  def do_something
    # ...
  end
end

The commit that changed ActiveRecord::Dirty is here: https://github.com/rails/rails/commit/16ae3db5a5c6a08383b974ae6c96faac5b4a3c81

Here is a blog post on these changes: https://www.ombulabs.com/blog/rails/upgrades/active-record-5-1-api-changes.html

Here is the summary I made for myself on the changes to ActiveRecord::Dirty in Rails 5.1+:

ActiveRecord::Dirty

https://api.rubyonrails.org/classes/ActiveRecord/AttributeMethods/Dirty.html

Before Saving (OPTIONAL CHANGE)

After modifying an object and before saving to the database, or within the before_save filter:

  • changes should now be changes_to_save
  • changed? should now be has_changes_to_save?
  • changed should now be changed_attribute_names_to_save
  • <attribute>_change should now be <attribute>_change_to_be_saved
  • <attribute>_changed? should now be will_save_change_to_<attribute>?
  • <attribute>_was should now be <attribute>_in_database

After Saving (BREAKING CHANGE)

After modifying an object and after saving to the database, or within the after_save filter:

  • saved_changes (replaces previous_changes)
  • saved_changes?
  • saved_change_to_<attribute>
  • saved_change_to_<attribute>?
  • <attribute>_before_last_save

Rails <= 5.0

class Page < ActiveRecord::Base
  before_save :do_something, if: :status_id_changed?

  private

  def do_something
    # ...
  end
end

This utilizes the fact that the before_save callback can conditionally execute based on the return value of a method call. The status_id_changed? method comes from ActiveModel::Dirty, which allows us to check if a specific attribute has changed by simply appending _changed? to the attribute name.

When the do_something method should be called is up to your needs. It could be before_save or after_save or any of the defined ActiveRecord::Callbacks.

这篇关于仅当Rails中的属性已更改时才运行回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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