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

后端 未结 8 1244
失恋的感觉
失恋的感觉 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:40

    I like to be specific even I aware that :id shouldn't change in normal, but

    (byebug) id_change.first.nil?
    true
    

    It always cheaper to be specific instead finding very weird unexpected bug.

    The same way if I expect true flag from untrustworthy argument

    def foo?(flag)
      flag == true
    end
    

    This saves a lot of hours to not sitting on weird bugs.

提交回复
热议问题