开发者

Creating a polymorphic relationship from the subordinate

I have three models that have a polymorphic relationship between them: Category, Sector and Course. A course can belong to either a category or a sector, not to both.

In the course model I set it up as follows:

belongs_to :parent, :polymorphic => true
accepts_nested_attributes_for :parent

For both the category and sector model I have the following relationship defined:

has_many :courses, :as => :parent

Now in the course view I want to be able to set the appropriate parent for t开发者_如何学JAVAhe course I am editing through a select box. I have the next line in the _form.html.erb:

<%= f.collection_select( :parent_id, @parent_options, :id, :name, {}, { :multiple => false } ) %>

This does return the correct parent_id into the hash, within an array. When I look at the record in the database it always stores 1 as the parent_id on the courses table (the number of items in the array?) and does not store the appropriate name at all (which is not surprising, as it is not passed in through the parameters).

I understand this is wrong, but can't figure out what I should do.

The @parent_options instance variable gets loaded with all the possible categories and sectors in the controller like this:

@parent_options = Admin::Sector.where(:visible => true) + Admin::Category.where(:visible => true)

I have found a lot of info on working with polymorphic relationships on railscasts and on this site, but all assume that you want to add comments (courses in my example) from the article (category or sector), instead of the other way around.

Thanks for helping!

In HTML the following is present:

<select id="admin_course_parent_id" name="admin_course[parent_id][]"><option value="2">Verzekeraars</option>
<option value="2">Verkooptraining</option>
<option value="3">Specifieke Branches</option></select><select id="admin_course_parent_id" name="admin_course[parent_id][]"><option value="2">Verzekeraars</option>
<option value="2">Verkooptraining</option>
<option value="3">Specifieke Branches</option></select>

When I hit the update/save button this is the value of the params:

Parameters: {"utf8"=>"✓", "authenticity_token"=>"my token", "admin_course"=>{"name"=>"Afsluittechnieken", "position"=>"1", "permalink"=>"afsluiten", "visible"=>"1", "description"=>"Hier leer je afsluittechnieken", "parent_id"=>["3"]}, "commit"=>"Update Course", "id"=>"1"}

The "parent_id"=>["3"] stated the correct id of the selected parent, but it does not store the value into the database.

Thanks so far, I've come a long way. No more array's being send in as the parent_id. I've also managed to set the parent_type from a hidden field following an onchange action. One issue left though, how do I fill the value dynamically based on the selected value in the collection select box? I use the following statement now:

<%= f.collection_select( :parent_id, @parent_options, :id, :name, {}, { :onchange => "this.form.admin_course_parent_type.value = 'WHAT GOES HERE'"}) %>

Wondering about the WHAT GOES HERE part. I have to read out the class of the selected value somehow. Have tried things like #{':parent_id.class'}, but it gives Symbol as a result (not too surprising).

Currently the model accepts all kinds of input as parent_type, really hoping I won't run into trouble someday because of this. And I am wondering whether the select box will have the correct value selected next time I load the page on edit, but we'll see later on.


The "parent_id"=>["3"] stated the correct id of the selected parent, but it does not store the value into the database.

You have two selects rendered with the same name admin_course[parent_id][]. Thats why the selected value of parent_id is returned in array "parent_id"=>["3"]. This array cant be stored into parent_id field in DB (you can store only one parent_id by this relation: belongs_to :parent, :polymorphic => true).

Fix duplicating of select box and it should have name attribute without [] at the end to pass parent_id value properly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜