ActiveRecord::AssociationTypeMismatch exception

Hello:

I am quite new to Ruby and Rails. Here are the details of a sample use
case that I am trying to build.

Development environment: (on Ubuntu 9.04)

{"commit"=>"Create",
"authenticity_token"=>"1459525ad4fd5ae39be0c011edc5c45fdbff4337",
"user"=>{"work_phone"=>"",
"address"=>{"address1"=>"ad1",
"city"=>"any town",
"address2"=>"line2",
"zip"=>"12345",
"country"=>"USA",
"state"=>"CA"},
"profile_name"=>"pfname",
"home_phone"=>"",
"cell_phone"=>"",
"last_name"=>"last",
"first_name"=>"first",
"email"=>"",
"active"=>"1"}}

It is a simple 1-1 relationship - each user has one address. In this
use case I am trying to create data in two tables using a single
form.

your code looks like you expect params[:address] to be set, but it
isn't (instead params[:user][:address] is set).
Rails is then trying to do user.address = params[:user][:address]
which doesn't work because you're trying to stuff a hash into an
association. It can be made to work with the nested attributes stuff
but that's only in rails 2.3. You could change your views to not
submit parameters in this way or read up on nested attributes.

Fred

Hi Fred,

Thank you so much for the quick response. It was quite helpful. I
really wanted to test this out with out upgrading to the newer
version. I made the changes to view/params
submittal and the controller code and now it seems to be working. I
can see two saved records after a create event.

new.html.erb:

<% form_for( @user,
             :url => { :action => 'create',
                        :first_name => @user.first_name,
                        :last_name => @user.last_name,
      :home_phone => @user.home_phone,
      :work_phone => @user.work_phone,
      :cell_phone => @user.cell_phone,
      :email => @user.email,
      :profile_name => @user.profile_name,
      :active => @user.active,
      :address1 => @address.address1,
      :address2 => @address.address2,
      :city => @address.city,
      :state => @address.state,
      :country => @address.country
         }
          ) do | f |
%>

----- rest of the form fields ---

In the users_controller.rb, removed the previous build invocation and
put these lines of code to manually set the FK for the association.

def create

    Rails.logger.debug(params)
    @user = User.new
    @address = Address.new

    @user.first_name = params[:user][:first_name]
    @user.last_name = params[:user][:last_name]
    @user.profile_name = params[:user][:profile_name]
    @user.home_phone = params[:user][:home_phone]
    @user.work_phone = params[:user][:work_phone]
    @user.cell_phone = params[:user][:cell_phone]
    @user.email = params[:user][:email]
    @user.active = params[:user][:active]

    @address.address1 = params[:user][:address][:address1]
    @address.address2 = params[:user][:address][:address2]
    @address.city = params[:user][:address][:city]
    @address.state = params[:user][:address][:state]
    @address.zip = params[:user][:address][:zip]
    @address.country = params[:user][:address][:country]

    respond_to do |format|

        if @user.save
          @address.user_id = @user.id
          if @address.save
            flash[:notice] = 'User was successfully created.'

Thanks again for the excellent comments,

best regards
Vasu

Vasu Kottilil

Hi Fred,

Thank you so much for the quick response. It was quite helpful. I
really wanted to test this out with out upgrading to the newer
version. I made the changes to view/params
submittal and the controller code and now it seems to be working. I
can see two saved records after a create event.

I think you're making things a little too complicated

doing

@user = User.new(params[:user]) ## --> line 45
@address = @user.address.build(params[:address])

is fine, as long as the two sets of parameters aren't munged together.
(eg use fields_for @address to create a separate form builder)

Fred