How to determine if a record is just created or updated in after_save

后端 未结 8 1278
失恋的感觉
失恋的感觉 2020-12-23 08:44

The #new_record? function determines if a record has been saved. But it is always false in the after_save hook. Is there a way to determine whether the record i

8条回答
  •  自闭症患者
    2020-12-23 09:29

    I was looking to use this for an after_save callback.

    A simpler solution is to use id_changed? (since it won't change on update) or even created_at_changed? if timestamp columns are present.

    Update: As @mitsy points out, if this check is needed outside of callbacks then use id_previously_changed?. See docs.

提交回复
热议问题