views:

323

answers:

0
I have a recursive relationship between models:

Test
--> Questions
-------> (has_one) Remediation 
------------> (has_many) Remediation_Questions
-----------------> (has_one) Question

Basically the idea is each question has a remediation (some link or block of text) associated with it. Each Remediation then has a set of remediation questions (which each has a question) as follow up questions to that remediation. The problem is rails is not building the form correctly (everything works except updating the remediation questions / answers):

################### Controller: GET /tests/1/edit   def edit
    @test = Test.find(params[:id])
    @test.questions.build
    @test.questions.each do |q|
      1.upto(Question::MaxAnswers-q.answers.count) do |qi|
        q.answers.build
      end
      q.build_remediation if q.remediation.nil?
      1.upto(Remediation::MaxQuestions-q.remediation.remediation_questions.count) do |rqi|
        q.remediation.remediation_questions.build
      end
      q.remediation.remediation_questions.each do |rq|
        rq.build_question
        1.upto(Question::MaxAnswers-rq.question.answers.count) do |rqis|
          rq.question.answers.build
        end
      end
    end   end


#################### View: 

<% form_for(@test) do |f| %>   <%= f.error_messages %>

  <p>
    <%= f.label :name %>
    <%= f.text_field :name %>   </p>

  <div id="questions">
    <ol>
      <% f.fields_for :questions do |q| %>
        <li>
          <%= q.label :question, "Question:" %>
          <%= q.text_field :question %>
          <ol>
            <% q.fields_for :answers do |a| %>
            <li>
              <%= a.label :answer, "Answer:" %>
              <%= a.text_field :answer %>
              <%= a.check_box :correct %>
            </li>
            <% end %>
          </ol>
          <% q.fields_for :remediation do |r| %>
            <%= r.label :body, "Remediation:" %><br />
            <%= r.text_area :body, :size => "60x4" %>
            <ol>
********* THIS IS THE PART THAT IS NOT WORKING ********
            <% r.fields_for :remediation_questions do |rqs| %>
            <% rqs.fields_for :questions do |rq| %>
              <li>
              <%= rq.label :question, "Remediation Question:" %>
              <%= rq.text_field :question %>
                <ol>
                <% rq.fields_for :answers do |rqa| %>
                  <li>
                  <%= rqa.label :answer, "Answer:" %>
                  <%= rqa.text_field :answer %>
                  <%= rqa.check_box :correct %>
                  </li>
                <% end %>
                </ol>
              <% end %>
              <% end %>
              </li>
            </ol>
          <% end %>
********* THIS IS THE PART THAT IS NOT WORKING *******
        </li>
      <% end %>
    </ol>   </div>

  <p>
    <%= f.submit 'Save' %>   </p> <% end %>


################ MODELS:

class Test < ActiveRecord::Base   validates_presence_of :name   has_many :test_questions   has_many :questions, :through => :test_questions   accepts_nested_attributes_for :questions,
    :reject_if => proc { |attrs| attrs['question'].blank? } end

class Question < ActiveRecord::Base   MaxAnswers = 4   validates_presence_of :question   has_many :test_questions   has_many :tests, :through => :test_questions   has_many :answers   has_one :remediation   has_one :remediation_question   accepts_nested_attributes_for :answers,
    :reject_if => proc { |attrs| attrs['correct'].to_i.zero? && attrs['answer'].blank? }   accepts_nested_attributes_for :remediation,
    :reject_if => proc { |attrs| attrs['body'].blank? } end

class Remediation < ActiveRecord::Base MaxQuestions = 4   belongs_to :question   has_many :remediation_questions   accepts_nested_attributes_for :remediation_questions,
    :reject_if => proc { |attrs| attrs['question_id'].blank? } end

class RemediationQuestion < ActiveRecord::Base   belongs_to :remediation   belongs_to :question   accepts_nested_attributes_for :question,
    :reject_if => proc { |attrs| attrs['question'].blank? } end