ROR accepts_nested_attributes_for one to many relationship with selection

Posted by bilash.saha on Stack Overflow See other posts from Stack Overflow or by bilash.saha
Published on 2012-04-14T05:18:24Z Indexed on 2012/04/14 5:28 UTC
Read the original article Hit count: 260

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 ?

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about ruby-on-rails-3