[Rails-spinoffs] ActiveRecord Question?

(Moving to the general Rails list)

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
@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.


[1] http://caboo.se/doc/classes/ActiveRecord/Base.html#M005926