# Rails Forms Not Working With Recursive Nested Models?

**URL:** <https://www.ruby-forum.com/t/rails-forms-not-working-with-recursive-nested-models/173713>\
**Category:** Rails\
**Created:** [August 13, 2009, 5:50pm UTC](https://www.ruby-forum.com/t/rails-forms-not-working-with-recursive-nested-models/173713 "2009-08-13T17:50:38Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![scrasher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b5a626/32.png) [@scrasher](https://www.ruby-forum.com/u/scrasher)\
**Post date:** [August 13, 2009, 5:50pm UTC](https://www.ruby-forum.com/t/rails-forms-not-working-with-recursive-nested-models/173713/1 "2009-08-13T17:50:38Z")

</div>

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

 \<% end %\> \*\*\*\*\*\*\*\*\* THIS IS THE PART THAT IS NOT WORKING \*\*\*\*\*\*\* 
 \<% 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.
