Forum: Ruby on Rails Bug or Feature / @var.save

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B188d046267bb5cddbc457580551297d?d=identicon&s=25 km (Guest)
on 2007-05-17 18:29
Hi all,

I have just a little problem. I want to save an object back to my
database table.

if I do itlike that:

e = Event.new(params)
e.save!

e.id returns the correct id of the created db entry

however if I do it like that.

e = Event.new(params).save!

e is just true (which is correct, but I hoped I'll get back the object)

Can you tell, I am still not very familiar with Ruby :-/

How can I do this in one line?
Thanks for a hint
Karl
821395fe70906c8290df7f18ac4ac6cf?d=identicon&s=25 Rick Olson (Guest)
on 2007-05-17 18:43
(Received via mailing list)
On 5/17/07, km <rails-mailing-list@andreas-s.net> wrote:
>
> e.id returns the correct id of the created db entry
>
> however if I do it like that.
>
> e = Event.new(params).save!

You're setting e to the return value of save!  You can use create!
though:

e = Event.create!(params)

--
Rick Olson
http://lighthouseapp.com
http://weblog.techno-weenie.net
http://mephistoblog.com
675475d0b65710be6d992eb5eb2c61c2?d=identicon&s=25 Gregory Seidman (Guest)
on 2007-05-17 18:44
(Received via mailing list)
On Thu, May 17, 2007 at 06:29:10PM +0200, km wrote:
>
> e.id returns the correct id of the created db entry
>
> however if I do it like that.
>
> e = Event.new(params).save!
>
> e is just true (which is correct, but I hoped I'll get back the object)
>
> Can you tell, I am still not very familiar with Ruby :-/

Yes, I can tell. You will find using Rails much easier and more
productive
if you actually know Ruby. Go learn it.

> How can I do this in one line?

(e = Event.new(params)).save!

> Thanks for a hint
> Karl
--Greg
This topic is locked and can not be replied to.