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

Thanks

try ‘@record.new_record

On Thu, Jul 1, 2010 at 9:57 PM, badnaam [email protected] wrote:

To unsubscribe from this group, send email to [email protected].
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.


Filipe Quadros Borges

email: [email protected]
msn: [email protected]

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.new
=> #<Story id: nil, name: nil, url: nil, created_at: nil,
updated_at: nil>
story.class
=> 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

  1. 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:

story.new_record?
=> 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:
story.save
=> 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:
story.new_record?
=> false
story.id
=> 1

Filipe Quadros Borges wrote:

try ‘@record.new_record

ActiveRecord::Base

On Thu, Jul 1, 2010 at 9:57 PM, badnaam [email protected] wrote:

To unsubscribe from this group, send email to [email protected].
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.


Filipe Quadros Borges

email: [email protected]
msn: [email protected]

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

stumbled upon this:
https://rails.lighthouseapp.com/projects/8994/tickets/1219-new_record-not-set-when-using-joins
let me know if there is any clue here?
(assert ?)

badnaam wrote:

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

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

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

On Jul 1, 6:49 pm, Hassan S. [email protected]

On Thu, Jul 1, 2010 at 6:09 PM, badnaam [email protected] wrote:

Strangely, this method returns true for an update

Can you post a simple test case demonstrating this behavior?


Hassan S. ------------------------ [email protected]
twitter: @hassan