Re: Problems with has_one - not saving association

Demetrius Nunes
2005-06-15T02:21:27Z

Ben Jackson wrote:
> Here are my models:
> class Person < ActiveRecord::Base
>     has_one :user
>     # class code...
>
> class User < ActiveRecord::Base
>     # class code...
>
> And my controller:
> class PersonController < ApplicationController
>     def create
>         unless logged_in?
>             @user = User.new(@params[:user])
>         else
>             @user = @session[''user'']
>         end
>         @person = Person.new(@params[:person])
>         @person.user = @user
>         render_text @person.user_id
>     end
> end
>
> @user.id is tracing out as expected, but the render_text call keep
> spitting out 0. What am I doing wrong?

You didn''t called @person.save. When you assign the user object to
@person, it does not set the user_id until save is called.

rgds