ROR accepts_nested_attributes_for one to many relationship with selection
- by bilash.saha
I have two models Doctors and Questions like the follwong:
Doctor Model
class Doctor < ActiveRecord::Base
has_many :questions
has_many :brands
accepts_nested_attributes_for :questions
end
Question model
class Question < ActiveRecord::Base
belongs_to :discipline
belongs_to :doctor
belongs_to :brand
end
Now you can clearly see that Doctor has many questions and brands and question belongs to doctor and brand.I want to add previously saved question to a doctor from doctors edit page.
I want to remove them as well.How can I proceed ?
I tried like :
<%= form.fields_for :questions, question,:child_index => (question.new_record? ? "index_to_replace_with_js" : nil) do |question_form| %>
<table>
<tr>
<td>
<div class="label">Select Question</div>
<%= question_form.collection_select :id, Question.all, :id, :title ,{:include_blank => true } %>
</td>
</tr>
</table>
but this doesnot works for me.Can you give me a solution with proper example ?