Saving a has_one association question

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 …

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 forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs