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:

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

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 M. 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 Josh,

Joshua M. 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

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.

On Wed, Jul 30, 2008 at 6:10 AM, Joshua M. <
[email protected]> wrote:

has_many :compact_discs
end


http://www.rubyplus.org/
Free Ruby and Rails Screencasts

Christopher Kintner wrote:

On Wed, Jul 30, 2008 at 8:10 AM, Joshua M.
[email protected] wrote:

has_many :compact_discs
end

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

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

I believe this should be:

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

I’m not at home right now, so I can’t check this, but according to the
API my version should be correct…?

http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#M001752

Bill W. 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…?!

On Wed, Jul 30, 2008 at 8:10 AM, Joshua M.
[email protected] wrote:

has_many :compact_discs
end

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

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

I believe this should be:

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

Joshua M. wrote:

Bill W. 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!

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