Forum: Ruby on Rails saving a has_one association question

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.
Martin G. (Guest)
on 2006-05-16 05:38
(Received via mailing list)
Hiall,

Say I have

class User
  has_one :community
end

class Community
  belongs_to :user
end

Will the following work and both save user and community if validation
doesn't fail?

user = params[:user]
user.community = params[:community]
user.save

Or do I have to do something like the following

user = params[:user]
if user.save
  user.community = params[:community]
end

to be on the safe side?

cheers
Martin

PS: Sorry that I don't play with script/console, I'm the guy who
posted about exactly this nice helper not working for him on his
"beloved" windows box ...
Philip H. (Guest)
on 2006-05-16 06:21
(Received via mailing list)
> end
> user = params[:user]
> if user.save
>  user.community = params[:community]
> end
>
> to be on the safe side?

Could be wrong, but I think you want...

user = User.new(params[:user])
user.community.create(params[:community])
user.save

(assuming this is a new record)

-philip
This topic is locked and can not be replied to.