ruby on rails 3 - Creating a polymorphic relationship from the subordinate -
ruby on rails 3 - Creating a polymorphic relationship from the subordinate -
i have 3 models have polymorphic relationship between them: category, sector , course. course of study can belong either category or sector, not both.
in course of study model set follows:
belongs_to :parent, :polymorphic => true accepts_nested_attributes_for :parent
for both category , sector model have next relationship defined:
has_many :courses, :as => :parent
now in course of study view want able set appropriate parent course of study editing through select box. have next line in _form.html.erb:
<%= f.collection_select( :parent_id, @parent_options, :id, :name, {}, { :multiple => false } ) %>
this homecoming right parent_id hash, within array. when @ record in database stores 1 parent_id on courses table (the number of items in array?) , not store appropriate name @ (which not surprising, not passed in through parameters). understand wrong, can't figure out should do.
the @parent_options instance variable gets loaded possible categories , sectors in controller this:
@parent_options = admin::sector.where(:visible => true) + admin::category.where(:visible => true)
i have found lot of info on working polymorphic relationships on railscasts , on site, assume want add together comments (courses in example) article (category or sector), instead of other way around.
thanks helping!
in html next 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 nail update/save button value of 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 right id of selected parent, not store value database.
thanks far, i've come long way. no more array's beingness send in parent_id. i've managed set parent_type hidden field next onchange action. 1 issue left though, how fill value dynamically based on selected value in collection select box? utilize next statement now:
<%= f.collection_select( :parent_id, @parent_options, :id, :name, {}, { :onchange => "this.form.admin_course_parent_type.value = 'what goes here'"}) %>
wondering goes here part. have read out class of selected value somehow. have tried things #{':parent_id.class'}, gives symbol result (not surprising).
currently model accepts kinds of input parent_type, hoping won't run problem someday because of this. , wondering whether select box have right value selected next time load page on edit, we'll see later on.
the "parent_id"=>["3"] stated right id of selected parent, not store value database.
you have 2 selects rendered same name admin_course[parent_id][]
. thats why selected value of parent_id returned in array "parent_id"=>["3"]
. array cant stored parent_id field in db (you can store 1 parent_id relation: belongs_to :parent, :polymorphic => true
).
fix duplicating of select box , should have name attribute without []
@ end pass parent_id value properly.
ruby-on-rails-3 polymorphism
Comments
Post a Comment