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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Christopher Kintner
2008-Jul-30 20:35 UTC
Re: AssociationTypeMismatch: Genre expected, got String
On Wed, Jul 30, 2008 at 8:10 AM, Joshua Muheim <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > 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 %>> 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 > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Muheim < rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > 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 >-- http://www.rubyplus.org/ Free Ruby and Rails Screencasts --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Joshua Muheim
2008-Jul-31 08:00 UTC
Re: AssociationTypeMismatch: Genre expected, got String
Christopher Kintner wrote:> On Wed, Jul 30, 2008 at 8:10 AM, Joshua Muheim > <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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: >> >> <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 %>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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---