代码之家  ›  专栏  ›  技术社区  ›  Kevin Sylvestre

测试Rails中是否有一个关系发生了更改

  •  2
  • Kevin Sylvestre  · 技术社区  · 14 年前

    是否可以有一个“before-save”回调来检测“has-one”关系是否已更改(关系 关系结束时的模型?例如,某些行为如下:

    @person.picture = @picture
    @person.picture_changed? # true
    @person.save
    @person.picture_changed? # false
    
    2 回复  |  直到 13 年前
        1
  •  -1
  •   hurikhan77    14 年前

    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

        2
  •  1
  •   BvuRVKyUVlViVIc7    13 年前

    @person.picture_id_changed?