How to determine if any field in a model / nested model changed?

后端 未结 4 1883
执笔经年
执笔经年 2020-12-25 10:45

Is there some quick way to find out if any of a model\'s fields or any fields of it\'s nested models (a.k.a. associations) changed?

4条回答
  •  生来不讨喜
    2020-12-25 11:01

    with 5.1 + the following works for me:

    saved_changes? will let you know if your object has changed:

    my_object.saved_changes?
    => true
    

    saved_changes will let you know which fields changed, the before value and the after value for each field:

    my_object.saved_changes
    => {"first_name"=>['Jim', 'Jimmy'], "updated_at"=>[Thu, 06 Dec 2018 18:45:00 UTC +00:00, Thu, 06 Dec 2018 18:52:44 UTC +00:00]}
    

提交回复
热议问题