I was trying to do something line Entity.new.save.id to create a new Entity, save it, and return the id to the local variable @entity. After working with it, it doesn''t work. Is there another way to do this without having to go to something like: @entity = Entity.new @entity.save @somevar = @entity.id I am new to Ruby and Rails, so please forgive me if this is a dumb question. I come from the land of Coldfusion and something like this would work there. Any insight would be appreciated. -- Eric Fleming efleming-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs -~----------~----~----~----~------~----~------~--~---
(Moving to the general Rails list) On 19.9.2006, at 16.24, Eric Fleming wrote:> I was trying to do something line Entity.new.save.id to create a > new Entity, save it, and return the id to the local variable > @entity. After working with it, it doesn''t work. Is there another > way to do this without having to go to something like: > > @entity = Entity.new > @entity.save > @somevar = @entity.id > > I am new to Ruby and Rails, so please forgive me if this is a dumb > question. I come from the land of Coldfusion and something like > this would work there. Any insight would be appreciated.The problem is that save doesn''t return the actual object. It returns true if the save was successful and false otherwise. You might want to use the ActiveRecord#create [1] method which does the same as new + save, and also returns the object. However, you still need to check whether the object really was saved because create returns the object whether or not it was saved. You might want to consult the api docs for ActiveRecord for more details. //jarkko [1] http://caboo.se/doc/classes/ActiveRecord/Base.html#M005926 -- Jarkko Laine http://jlaine.net http://odesign.fi
On 9/19/06, Eric Fleming <efleming-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > I was trying to do something line Entity.new.save.id to create a new > Entity, save it, and return the id to the local variable @entity. After > working with it, it doesn''t work. Is there another way to do this without > having to go to something like: > > @entity = Entity.new > @entity.save > @somevar = @entity.id > > I am new to Ruby and Rails, so please forgive me if this is a dumb > question. I come from the land of Coldfusion and something like this would > work there. Any insight would be appreciated. >Perhaps validation failed? if @entity.save # save succeeded and id assigned from database @somevar = @entity.id else # validation errors prevented save puts @entity.errors.inspect end jeremy --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs -~----------~----~----~----~------~----~------~--~---