1

I have a weird rails database/form problem.

I got 1 table with Courses, and 1 table Prerequisites, which has 2 columns that both hold Course_ids (columns are called "course_a_id" and "course_b_id").

In the Course model I have this:

:has_and_belongs_to_many(:prerequisites,
:join_table => "prerequisites",
:foreign_key => "course_a_id",
:association_foreign_key => "course_b_id",
:class_name => "Course")

From this SO answer

This works if I put the prerequisites in by Console, something like this:

Course.find(3).prerequisites = [Course.find(1), Course.find(2)]

This form field however does not let me put prereqs in the DB:

<%= f.label :prerequisite, "Prerequisites" %>
<%= f.collection_select(:prerequisites, Course.all, :id, :name,
    {:multiple => true}, :multiple => "multiple") %>

Gives this error after posting with one option selected with value 1:

"Course(#-631146998) expected, got String(#77208170)" 

and in the parameter dump:

"prerequisites"=>["",
"1"]}

I have no clue how that "" ended up in the params and can't figure out another way to create the form field. I think my model is set up correctly.

Community
  • 1
  • 1
Yoast
  • 11
  • 1
  • 1
    Try this `<%= f.collection_select(:prerequisite_ids, Course.all, :id, :name, {}, {:multiple => true}) %>` and in your `Course` Model add `:prerequisite_ids` to `attr_accessible` – Manoj Monga Feb 19 '13 at 12:24

0 Answers0