Ruby-on-rails 仅当 Rails 中的属性发生更改时才运行回调
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24314584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Run a callback only if an attribute has changed in Rails
提问by Hommer Smith
I have the following association in my app:
我的应用程序中有以下关联:
# Page
belongs_to :status
I want to run a callback anytime the status_idof a pagehas changed.
我想在status_idapage发生变化时运行回调。
So, if page.status_idgoes from 4 to 5, I want to be able to catch that.
所以,如果page.status_id从 4 到 5,我希望能够抓住它。
How to do so?
怎么做?
回答by pdobb
Rails 5.1+
导轨 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
改变 ActiveRecord::Dirty 的提交在这里: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
这是有关这些更改的博客文章:https: //www.ombulas.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+:
以下是我为自己在 Rails 5.1+ 中对 ActiveRecord::Dirty 的更改所做的总结:
ActiveRecord::Dirty
ActiveRecord::Dirty
https://api.rubyonrails.org/classes/ActiveRecord/AttributeMethods/Dirty.html
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_savefilter:
在修改对象之后和保存到数据库之前,或在before_save过滤器中:
changesshould now bechanges_to_savechanged?should now behas_changes_to_save?changedshould now bechanged_attribute_names_to_save<attribute>_changeshould now be<attribute>_change_to_be_saved<attribute>_changed?should now bewill_save_change_to_<attribute>?<attribute>_wasshould now be<attribute>_in_database
changes现在应该是changes_to_savechanged?现在应该是has_changes_to_save?changed现在应该是changed_attribute_names_to_save<attribute>_change现在应该是<attribute>_change_to_be_saved<attribute>_changed?现在应该是will_save_change_to_<attribute>?<attribute>_was现在应该是<attribute>_in_database
After Saving (BREAKING CHANGE)
保存后(中断更改)
After modifying an object and after saving to the database, or within the after_savefilter:
修改对象并保存到数据库后,或在after_save过滤器内:
saved_changes(replacesprevious_changes)saved_changes?saved_change_to_<attribute>saved_change_to_<attribute>?<attribute>_before_last_save
saved_changes(替换previous_changes)saved_changes?saved_change_to_<attribute>saved_change_to_<attribute>?<attribute>_before_last_save
Rails <= 5.0
导轨 <= 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_savecallback 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.
这利用了before_save回调可以根据方法调用的返回值有条件地执行的事实。该status_id_changed?方法来自ActiveModel::Dirty,它允许我们通过简单地附加_changed?到属性名称来检查特定属性是否已更改。
When the do_somethingmethod should be called is up to your needs. It could be before_saveor after_saveor any of the defined ActiveRecord::Callbacks.
何时do_something调用该方法取决于您的需要。它可以是before_save或after_save或任何已定义的 ActiveRecord::Callbacks。
回答by Mateus Luiz
The attribute_changed?is deprecated in Rails 5.1, now just use will_save_change_to_attribute?.
在attribute_changed?被弃用的Rails 5.1,现在只需使用will_save_change_to_attribute?。
For more information, see this issue.
有关更多信息,请参阅此问题。
回答by Monideep
Try this
尝试这个
after_validation :do_something, if: ->(obj){ obj.status_id.present? and obj.status_id_changed? }
def do_something
# your code
end
Reference - http://apidock.com/rails/ActiveRecord/Dirty

