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

后端 未结 8 1279
失恋的感觉
失恋的感觉 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条回答
  •  猫巷女王i
    2020-12-23 09:31

    There is an after_create callback which is only called if the record is a new record, after it is saved. There is also an after_update callback for use if this was an existing record which was changed and saved. The after_save callback is called in both cases, after either after_create or after_update is called.

    Use after_create if you need something to happen once after a new record has been saved.

    More info here: http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html

提交回复
热议问题