Determine Whether an ActiveRecord Object is a New Record or Not
This short blog post will show you how to determine if an ActiveRecord object is new or not.
Published on:October 4, 2013
Sometimes we need to determine whether an ActiveRecord object is new or not. An example would be if we use first_or_initialize. In order to do this, all you need to do is use the
new_record?method. For example:
book = Books.where(isbn: "0374180660").first_or_initialize if book.new_record? # => true if book is new, false if it wasn't. # Perform misc stuff here. end