Rails Forms Not Working With Recursive Nested Models?

I have a recursive relationship between models (Question model appears
on multiple levels of the hierarchy):

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

The problem is rails is not building the form correctly (everything
works except updating the remediation questions / answers). The only
reason i think this might be happening is that the question model is
used recursively.

################### 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 %>

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

    <% f.fields_for :questions do |q| %>
  1. <%= q.label :question, "Question:" %> <%= q.text_field :question %>
      <% q.fields_for :answers do |a| %>
    1. <%= a.label :answer, "Answer:" %> <%= a.text_field :answer %> <%= a.check_box :correct %>
    2. <% end %>
    <% q.fields_for :remediation do |r| %> <%= r.label :body, "Remediation:" %>
    <%= r.text_area :body, :size => "60x4" %>
      ********* THIS IS THE PART THAT IS NOT WORKING ******** <% r.fields_for :remediation_questions do |rqs| %> ********* WHEN IT SEES QUESTIONS AGAIN IT BONKS ******* <% rqs.fields_for :questions do |rq| %>
    1. <%= rq.label :question, "Remediation Question:" %> <%= rq.text_field :question %>
        <% rq.fields_for :answers do |rqa| %>
      1. <%= rqa.label :answer, "Answer:" %> <%= rqa.text_field :answer %> <%= rqa.check_box :correct %>
      2. <% end %>
      <% end %> <% end %>
    <% end %> ********* THIS IS THE PART THAT IS NOT WORKING *******
  2. <% end %>

<%= f.submit 'Save' %>

<% 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

Any idea how to resolve this? Its not displaying the second level
questions, nor updating them. However in the show file the models work
perfectly.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs