The name “saved_record?” might be misleading – developers might think that this method returns false before the “save” method was called and true after the call:
user.saved_record? # => false
user.save
user.saved_record? # => true
Of course, this is not what the method does (“dirty?” should be used for this).
Because object=Model.find_or_create...() will result in object.new_record? never being true. In my code, I have occasionally done:
if object.new_record?.nil? # new_record? is false on create, but nil on find
Which is depending on a kind of behavior that is clearly subject to change. I could have been a bit more invasive with object.instance_variable_get("@new_record_before_save"), but.. "Ew!"
Where the find_or_create call is internal to a another wrapper
method.
Where you need to operate on a saved record [most likely
needing the id attribute].
@jose three tickets? i just see mine [from last month] and this one [from today]. and it looks like my implementation does a little more than what this one does which seems to simply wrap !new_record? with saved_record? [unless i’m missing something]?
Matt: you might not need to run said code for every save. in my use case, i only had to use it once out of all the saves i do for the class in my entire app.