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

后端 未结 4 1939
执笔经年
执笔经年 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:09

    To see if a ActiveRecord object has changed, you can call:

    instance.changed?
    

    To see if a specific attribute was changed, you can do:

    instance.attr_changed?
    

    where attr is the attribute you want to check.

    To check for if a nested model changed, you can just do:

    instance.nested_model.changed?
    

提交回复
热议问题