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”:
<%= f.collection_select :course, @courses, :id, :name %>
It should be “:course_id”:
<%= f.collection_select :course_id, @courses, :id, :name %>
Try that and see if it works.
– Josh N. Abbott
Melvin R. 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
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 A. [email protected]
That’s odd. Are you declaring your associations in the models? Also what
version of Rails are you using?
– Josh
Melvin R. 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 A. [email protected]
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 A. [email protected]
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 A. [email protected]
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 R. 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 A. [email protected]
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 R. 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 A. [email protected]
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| %>
Book
<%= f.collection_select :book_id, @books, :id, :name %>
<%= f.submit "Update" %>
<% end %>
views/errata/show.html.erb
Book ID: <%=h @errata.book_id %>
On Jun 2, 4:30 pm, Joshua A. [email protected]
Ha. I’ve done that before 
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 R. 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? 
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? 