check for new record?

Is it possible to check if the record that is about to be saved is a brand new record or an update? I am trying to do this inside a before_save callback. I am on rails 2.3.8


try '@record.new_record'

Strangely, this method returns true for an update, thats why I wasnt sure. Does this only check if that "self" instance has been saved or not?

I just read this from the "Simply Rails 2" book

model.new_record? If it is not yet saved, will return true. If saved, will return false.

Excerpt from this book:

From the Rails console, let’s create this Story class, and an instance of the class called story, by entering these commands:

class Story < ActiveRecord::Base; end

=> nil

story =

=> #<Story id: nil, name: nil, url: nil, created_at: nil, updated_at: nil>


=> Story(id: integer, name: string, link: string, created_at: datetime, updated_at: datetime) As you can see, the syntax for creating a new ActiveRecord object is identical to the syntax we used to create other Ruby objects in Chapter 3. At this point, we’ve created a new Story object. However, this object exists in memory only—we haven’t stored it in our database yet. We can confirm the fact that our Story object hasn’t been saved yet by checking the return value of the new_record? method:


=> true Since the object has not been saved yet, it will be lost when we exit the Rails console. To save it to the database, we need to invoke the object’s save method:

=> true Now that we’ve saved our object (a return value of true indicates that the save method was successful) our story is no longer a new record. It’s even been assigned a unique ID, as shown below:


=> false

=> 1

Filipe Quadros Borges wrote:

Well so its of no use if one wants to check if the record is a brand new record or an updated one

i'm trying to see if any would return false in your case, as the record already exists for update.

stumbled upon this: let me know if there is any clue here? (assert ?)

badnaam wrote:

Can you post a simple test case demonstrating this behavior?

I will try, I am a newb not sure how to do tests