是否有可能有一个'before_save'回调来检测'has_one'关系是否已经改变(关系结束时关系不关系)?例如,某事会像这样:
@person.picture = @picture
@person.picture_changed? # true
@person.save
@person.picture_changed? # false
答案 0 :(得分:1)
也许它适用于
@person.picture_id_changed?
答案 1 :(得分:-1)
尝试relation.changed?
...您可能还想查看观察者,具体取决于您想要实现的目标。
示例:
class Model < ActiveRecord::Base
has_one :relation
before_save :check_relation_changed
private def check_relation_changed
do_something if relation.changed?
end
end
参考:http://ryandaigle.com/articles/2008/3/31/what-s-new-in-edge-rails-dirty-objects