collection_select not doing what it should... any ideas?

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

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'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>

That's odd. Are you declaring your associations in the models? Also what version of Rails are you using?

-- 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

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.

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:

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 %>

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? :smiley:

Ha. I've done that before :smiley:

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: