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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
try ''@record.new_record'' http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M001843 On Thu, Jul 1, 2010 at 9:57 PM, badnaam <asitkmishra-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- Filipe Quadros Borges email: fqborges-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org msn: fqborges-PkbjNfxxIARBDgjK7y7TUQ@public.gmane.org -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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? On Jul 1, 6:03 pm, Filipe Quadros Borges <fqbor...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> try ''...@record.new_record'' > > http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M001843 > > On Thu, Jul 1, 2010 at 9:57 PM, badnaam <asitkmis...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > 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 > > > -- > > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en. > > -- > Filipe Quadros Borges > > email: fqbor...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org > msn: fqbor...-PkbjNfxxIARBDgjK7y7TUQ@public.gmane.org-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 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:>> 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'' > > http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M001843 > > On Thu, Jul 1, 2010 at 9:57 PM, badnaam <asitkmishra-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >> >> > > > > -- > Filipe Quadros Borges > > email: fqborges-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org > msn: fqborges-PkbjNfxxIARBDgjK7y7TUQ@public.gmane.org-- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Well so its of no use if one wants to check if the record is a brand new record or an updated one On Jul 1, 6:13 pm, RailsFan Radha <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 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 > 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: > > >> 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'' > > >http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M001843 > > > On Thu, Jul 1, 2010 at 9:57 PM, badnaam <asitkmis...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > >> For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en. > > > -- > > Filipe Quadros Borges > > > email: fqbor...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org > > msn: fqbor...-PkbjNfxxIARBDgjK7y7TUQ@public.gmane.org > > -- > Posted viahttp://www.ruby-forum.com/.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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-- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Thu, Jul 1, 2010 at 6:09 PM, badnaam <asitkmishra-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Strangely, this method returns true for an updateCan you post a simple test case demonstrating this behavior? -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org twitter: @hassan -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
I will try, I am a newb not sure how to do tests On Jul 1, 6:49 pm, Hassan Schroeder <hassan.schroe...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Thu, Jul 1, 2010 at 6:09 PM, badnaam <asitkmis...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Strangely, this method returns true for an update > > Can you post a simple test case demonstrating this behavior? > > -- > Hassan Schroeder ------------------------ hassan.schroe...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org > twitter: @hassan-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.