【发布时间】:2018-01-28 00:26:10
【问题描述】:
在我的应用程序中,我的 Post 模型具有 title 和 price 等属性。
我有 2 个before_actions,我想仅在title 发生更改时运行,而另外一个仅在price 更改时运行。
class Post < ActiveRecord::Base
before_update :do_something_with_title
before_update :do_something_with_price
def do_something_with_title
// my code for title here
end
def do_something_with_price?
// my code for price here
end
我知道我可以使用changed? 并给before_action 一个if: 条件,但这将适用于post 中的任何属性发生变化的情况,而不仅仅是title 和price 发生变化的情况。
感谢任何帮助!
【问题讨论】:
标签: ruby ruby-on-rails-4 model before-filter