AssociationTypeMismatch: Genre expected, got String

Hi all

I have the following 2 models:

class CompactDisc < ActiveRecord::Base
  belongs_to :genre
end

class Genre < ActiveRecord::Base
  has_many :compact_discs
end

I have created a select box for my form so I can choose a genre for
every compact disc:

<p>
  <b>Genre</b><br />
  <%= f.select :genre, Genre.find(:all).collect{|p| [p.name, p.id]},
:selected => @compact_disc.genre.id %>
</p>

But when submitting the form, I get the following error:

ActiveRecord::AssociationTypeMismatch in Compact discsController#update
Genre expected, got String

What's happening?! Did I miss anything?

Thanks a lot for help
Josh

Hi Josh,
Joshua Muheim wrote:

But when submitting the form, I get the following error:

ActiveRecord::AssociationTypeMismatch in
Compact discsController#update Genre expected,
got String

What's happening?! Did I miss anything?

What's your update method look like?

Bill

# PUT /compact_discs/1
  # PUT /compact_discs/1.xml
  def update
    @compact_disc = CompactDisc.find(params[:id])

    respond_to do |format|
      if @compact_disc.update_attributes(params[:compact_disc])
        flash[:notice] = 'CompactDisc was successfully updated.'
        format.html { redirect_to(@compact_disc) }
        format.xml { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml { render :xml => @compact_disc.errors, :status =>
:unprocessable_entity }
      end
    end
  end

It's the original method that was created using script/generate
scaffold...

Hi all

I have the following 2 models:

class CompactDisc < ActiveRecord::Base
belongs_to :genre
end

class Genre < ActiveRecord::Base
has_many :compact_discs
end

I have created a select box for my form so I can choose a genre for
every compact disc:

<p>
<b>Genre</b><br />
<%= f.select :genre, Genre.find(:all).collect{|p| [p.name, p.id]},
:selected => @compact_disc.genre.id %>
</p>

I believe this should be:

<%= f.select :genre_id, Genre.find(:all).collect{|p| [p.id, p.name]},
:selected => @compact_disc.genre.id %>

The selection box sends the id of Genre and display a string for the user to make a selection. Do tail -f log/development.log, look at the params hash to see what is passed to the server. It looks like you have String that is supposed to be Genre.

Look at the line number that is related to your app in the stack trace.

Hi Josh,

Joshua Muheim wrote:

It's the original method that was created using script/generate
scaffold...

That's pretty much what I figured. The scaffolded method didn't anticipate
you were going to do a 'nested' save. It doesn't know how to translate the
params strings into a Genera object via the update_attributes method.
You'll need to help it along.

Best regards,
Bill

Christopher Kintner wrote:

Bill Walton wrote:

The scaffolded method didn't
anticipate
you were going to do a 'nested' save. It doesn't know how to translate
the
params strings into a Genera object via the update_attributes method.
You'll need to help it along.

Best regards,
Bill

Thanks, but I can't remember that I had to do this ever before?! Is this
a new "feature" in Rails (didn't use Rails for some months now, and now
I updated to the newest version)...? Looks like a big step back to
me...?!

Joshua Muheim wrote:

Bill Walton wrote:

The scaffolded method didn't
anticipate
you were going to do a 'nested' save. It doesn't know how to translate
the
params strings into a Genera object via the update_attributes method.
You'll need to help it along.

Best regards,
Bill

Thanks, but I can't remember that I had to do this ever before?! Is this
a new "feature" in Rails (didn't use Rails for some months now, and now
I updated to the newest version)...? Looks like a big step back to
me...?!

Found the problem: I had f.select(:genre) in the view instead of
f.select(:genre_id)... Thanks anyway, folks!