I get the error ActiveRecord::AssociationTypeMismatch (Course(#23456280859460) expected, got String(#23757015656420)) when i submit an update with the code below. Any ideas on what is going on? ## /views/enrollment/edit.html.erb <% form_for(@enrollment) do |f| %> <%= f.collection_select :course, @courses, :id, :name %> <%= f.submit "Update" %> <% end %> ## /controllers/enrollment_controller.rb def edit @enrollment = Enrollment.find(params[:id]) @courses = Course.find(:all) end def update @enrollment = Enrollment.find(params[:id]) respond_to do |format| if @enrollment.update_attributes(params[:enrollment]) flash[:notice] = ''Enrollment was successfully updated.'' format.html { redirect_to(@enrollment) } format.xml { head :ok } else format.html { render :action => "edit" } format.xml { render :xml => @enrollment.errors, :status => :unprocessable_entity } end end end --~--~---------~--~----~------------~-------~--~----~ 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 Abbott
2008-Jun-02 22:06 UTC
Re: collection_select not doing what it should... any ideas?
Melvin, You need to reference the method, not the object in your collection select. So instead of using ":course": <pre><%= f.collection_select :course, @courses, :id, :name %></pre> It should be ":course_id": <pre><%= f.collection_select :course_id, @courses, :id, :name %></pre> Try that and see if it works. -- Josh N. Abbott Melvin Ram wrote:> I get the error ActiveRecord::AssociationTypeMismatch > (Course(#23456280859460) expected, got String(#23757015656420)) > > when i submit an update with the code below. Any ideas on what is > going on? > > ## /views/enrollment/edit.html.erb > > <% form_for(@enrollment) do |f| %> > > <%= f.collection_select :course, @courses, :id, :name %> > > <%= f.submit "Update" %> > > <% end %> > > ## /controllers/enrollment_controller.rb > > def edit > @enrollment = Enrollment.find(params[:id]) > @courses = Course.find(:all) > end > > def update > @enrollment = Enrollment.find(params[:id]) > > respond_to do |format| > if @enrollment.update_attributes(params[:enrollment]) > flash[:notice] = ''Enrollment was successfully updated.'' > format.html { redirect_to(@enrollment) } > format.xml { head :ok } > else > format.html { render :action => "edit" } > format.xml { render :xml => @enrollment.errors, :status > => :unprocessable_entity } > end > end > end-- 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 -~----------~----~----~----~------~----~------~--~---
mel ram
2008-Jun-02 22:16 UTC
Re: collection_select not doing what it should... any ideas?
I''ve tried that as well... and it actually does update the enrollment record... but instead of adding the id of the course, it inserts the course itself i.e #<Course:0x2aaaae913bf0> On Jun 2, 3:06 pm, Joshua Abbott <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Melvin, > > You need to reference the method, not the object in your collection > select. > > So instead of using ":course": > > <pre><%= f.collection_select :course, @courses, :id, :name %></pre> > > It should be ":course_id": > > <pre><%= f.collection_select :course_id, @courses, :id, :name %></pre> > > Try that and see if it works. > > -- Josh N. Abbott > > > > Melvin Ram wrote: > > I get the error ActiveRecord::AssociationTypeMismatch > > (Course(#23456280859460) expected, got String(#23757015656420)) > > > when i submit an update with the code below. Any ideas on what is > > going on? > > > ## /views/enrollment/edit.html.erb > > > <% form_for(@enrollment) do |f| %> > > > <%= f.collection_select :course, @courses, :id, :name %> > > > <%= f.submit "Update" %> > > > <% end %> > > > ## /controllers/enrollment_controller.rb > > > def edit > > @enrollment = Enrollment.find(params[:id]) > > @courses = Course.find(:all) > > end > > > def update > > @enrollment = Enrollment.find(params[:id]) > > > respond_to do |format| > > if @enrollment.update_attributes(params[:enrollment]) > > flash[:notice] = ''Enrollment was successfully updated.'' > > format.html { redirect_to(@enrollment) } > > format.xml { head :ok } > > else > > format.html { render :action => "edit" } > > format.xml { render :xml => @enrollment.errors, :status > > => :unprocessable_entity } > > end > > end > > end > > -- > Posted viahttp://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 -~----------~----~----~----~------~----~------~--~---
Joshua Abbott
2008-Jun-02 22:29 UTC
Re: collection_select not doing what it should... any ideas?
That''s odd. Are you declaring your associations in the models? Also what version of Rails are you using? -- Josh Melvin Ram wrote:> I''ve tried that as well... and it actually does update the enrollment > record... but instead of adding the id of the course, it inserts the > course itself i.e #<Course:0x2aaaae913bf0> > > On Jun 2, 3:06 pm, Joshua Abbott <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>-- 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 -~----------~----~----~----~------~----~------~--~---
mel ram
2008-Jun-02 22:36 UTC
Re: collection_select not doing what it should... any ideas?
rails -v => 2.1 class Enrollment < ActiveRecord::Base belongs_to :course belongs_to :user end class Course < ActiveRecord::Base has_many :lessons belongs_to :user has_many :enrollments end On Jun 2, 3:29 pm, Joshua Abbott <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> That''s odd. Are you declaring your associations in the models? Also what > version of Rails are you using? > > -- Josh > > Melvin Ram wrote: > > I''ve tried that as well... and it actually does update the enrollment > > record... but instead of adding the id of the course, it inserts the > > course itself i.e #<Course:0x2aaaae913bf0> > > > On Jun 2, 3:06 pm, Joshua Abbott <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > > -- > Posted viahttp://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 -~----------~----~----~----~------~----~------~--~---
Joshua Abbott
2008-Jun-02 23:07 UTC
Re: collection_select not doing what it should... any ideas?
Is the column type "int" in your database? It doesn''t seem like it could be if it can store #<Course:0x2aaaae913bf0>. I''m not saying that''s the solution to this problem, but it could be confusing Rails a bit. And you are no longer referencing :course, it''s now :course_id, huh? Let me know your course_id column type is and I''ll keep digging around. -- Josh Melvin Ram wrote:> rails -v => 2.1 > > class Enrollment < ActiveRecord::Base > belongs_to :course > belongs_to :user > end > > class Course < ActiveRecord::Base > has_many :lessons > belongs_to :user > has_many :enrollments > end > > > On Jun 2, 3:29 pm, Joshua Abbott <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>-- 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 -~----------~----~----~----~------~----~------~--~---
mel ram
2008-Jun-02 23:22 UTC
Re: collection_select not doing what it should... any ideas?
Thanks Josh for all your help! The column was created using migrations with the following code: create_table :enrollments do |t| t.references :course # other columns end Right now I''m creating a new bare-bones app to see if I can simplify the code... I''ll let you know if I can get it working in there. On Jun 2, 4:07 pm, Joshua Abbott <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Is the column type "int" in your database? It doesn''t seem like it could > be if it can store #<Course:0x2aaaae913bf0>. I''m not saying that''s the > solution to this problem, but it could be confusing Rails a bit. > > And you are no longer referencing :course, it''s now :course_id, huh? > > Let me know your course_id column type is and I''ll keep digging around. > > -- Josh > > > > Melvin Ram wrote: > > rails -v => 2.1 > > > class Enrollment < ActiveRecord::Base > > belongs_to :course > > belongs_to :user > > end > > > class Course < ActiveRecord::Base > > has_many :lessons > > belongs_to :user > > has_many :enrollments > > end > > > On Jun 2, 3:29 pm, Joshua Abbott <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > > -- > Posted viahttp://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 -~----------~----~----~----~------~----~------~--~---
Joshua Abbott
2008-Jun-02 23:30 UTC
Re: collection_select not doing what it should... any ideas?
Hmm... so even that''s right. Definitely let me where you end up with a bare-bones app. I''ve never heard of this happening, so I''ll be very curious in case I or someone I know happens to run into someday. -- Josh Melvin Ram wrote:> Thanks Josh for all your help! > > The column was created using migrations with the following code: > > create_table :enrollments do |t| > t.references :course > # other columns > end > > Right now I''m creating a new bare-bones app to see if I can simplify > the code... I''ll let you know if I can get it working in there. > > > > On Jun 2, 4:07 pm, Joshua Abbott <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>-- 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 -~----------~----~----~----~------~----~------~--~---
mel ram
2008-Jun-02 23:35 UTC
Re: collection_select not doing what it should... any ideas?
It works just fine in my bare bones app with the code below. Gotta comment out parts of my app to see what is messing things up. # erratas_controller.rb def edit @errata = Errata.find(params[:id]) @books = Book.find(:all) end # model/book.rb class Book < ActiveRecord::Base has_many :erratas end # models/errata.rb class Errata < ActiveRecord::Base belongs_to :book end # views/errata/edit.html.erb <% form_for(@errata) do |f| %> <p> <b>Book</b><br /> <%= f.collection_select :book_id, @books, :id, :name %> </p> <p> <%= f.submit "Update" %> </p> <% end %> # views/errata/show.html.erb Book ID: <%=h @errata.book_id %> On Jun 2, 4:30 pm, Joshua Abbott <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hmm... so even that''s right. Definitely let me where you end up with a > bare-bones app. I''ve never heard of this happening, so I''ll be very > curious in case I or someone I know happens to run into someday. > > -- Josh > > > > Melvin Ram wrote: > > Thanks Josh for all your help! > > > The column was created using migrations with the following code: > > > create_table :enrollments do |t| > > t.references :course > > # other columns > > end > > > Right now I''m creating a new bare-bones app to see if I can simplify > > the code... I''ll let you know if I can get it working in there. > > > On Jun 2, 4:07 pm, Joshua Abbott <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > > -- > Posted viahttp://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 -~----------~----~----~----~------~----~------~--~---
mel ram
2008-Jun-02 23:56 UTC
Re: collection_select not doing what it should... any ideas?
I was doing a stupid error. In my show.html.erb where it was outputting #<Course:0x2aaaae913bf0>, I was rendering @enrollment.course instead of @enrollment.course_id. So it was rendering the course object. Dumb huh? :D On Jun 2, 4:35 pm, mel ram <mel...-IA9i2KS8NFBpEKysQ+xqfPpXobYPEAuW@public.gmane.org> wrote:> It works just fine in my bare bones app with the code below. Gotta > comment out parts of my app to see what is messing things up. > > # erratas_controller.rb > > def edit > @errata = Errata.find(params[:id]) > @books = Book.find(:all) > end > > # model/book.rb > > class Book < ActiveRecord::Base > has_many :erratas > end > > # models/errata.rb > > class Errata < ActiveRecord::Base > belongs_to :book > end > > # views/errata/edit.html.erb > > <% form_for(@errata) do |f| %> > <p> > <b>Book</b><br /> > <%= f.collection_select :book_id, @books, :id, :name %> > </p> > > <p> > <%= f.submit "Update" %> > </p> > <% end %> > > # views/errata/show.html.erb > > Book ID: <%=h @errata.book_id %> > > On Jun 2, 4:30 pm, Joshua Abbott <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: > > > Hmm... so even that''s right. Definitely let me where you end up with a > > bare-bones app. I''ve never heard of this happening, so I''ll be very > > curious in case I or someone I know happens to run into someday. > > > -- Josh > > > Melvin Ram wrote: > > > Thanks Josh for all your help! > > > > The column was created using migrations with the following code: > > > > create_table :enrollments do |t| > > > t.references :course > > > # other columns > > > end > > > > Right now I''m creating a new bare-bones app to see if I can simplify > > > the code... I''ll let you know if I can get it working in there. > > > > On Jun 2, 4:07 pm, Joshua Abbott <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > > > -- > > Posted viahttp://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 -~----------~----~----~----~------~----~------~--~---
Joshua Abbott
2008-Jun-03 00:03 UTC
Re: collection_select not doing what it should... any ideas?
Ha. I''ve done that before :D I guess the red flag should''ve definitely been when you said you were saving #<Course:0x2aaaae913bf0> into an integer column. MySQL wouldn''t have let that happen because that''s clearly not an integer. Glad you got it sorted out! -- Josh Melvin Ram wrote:> I was doing a stupid error. > > In my show.html.erb where it was outputting #<Course:0x2aaaae913bf0>, > I was rendering @enrollment.course instead of @enrollment.course_id. > So it was rendering the course object. > > Dumb huh? :D-- 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 -~----------~----~----~----~------~----~------~--~---