Several times I found myself of need of a method that tells whether an ActiveRecord instance has just been created. For example, when writing generic activity logging code that needs to know whether model is newly created as opposed to updated, but the invocation is in a service outside of the model itself and thus I can’t just write
after_update hooks, except perhaps just to set custom flags for the service to inspect.
It’s not the same as
new_record? is only true before record is saved, not after save. I’m thinking
just_created? name. Calling it
created? would be too ambiguous and higher chance to clash with similarly named method by application developers in their own code.
newly_created? is the same as
marked_for_destruction? is to
destroyed?, or as
changes is to
In my app I’ve implemented the code like this:
id_changes = previous_changes[‘id’]
id_changes && id_changes.last && !id_changes.first
Is this something anyone else found doing, and would it be helpful for ActiveRecord to provide this method?