Check if the model has been modified or created in the before_save phase

I want to check if a model is being created in the before_save Rails callback. I also want to check if it has been changed (during upgrade).

thanks

+6
source share
1 answer

Can you use new_record? to find out if you have a new object and changed? to see if something has changed:

 before_save :pancakes def pancakes if new_record? # Not in the database yet. elsif changed? # Already exists but it has unsaved changes. end end 
+11
source

Source: https://habr.com/ru/post/904500/


All Articles