How can I check if an ActiveRecord object is new or is already persisted?
2 Answers
An ActiveRecord object lifecycle:
1.new record
item = Item.new
item.new_record? #=> true
2.persisted
item.save
item.persisted? #=> true
3.changed
item.name = "other"
item.changed? #=> true
4.destroyed
item.destroy
item.destroyed? #=> true
1 Comment
Michael Koper
I'd like to add
item.previously_new_record? and item.previously_persisted? to the list