# Multiple has\_many objects in one form

**URL:** <https://www.ruby-forum.com/t/multiple-has-many-objects-in-one-form/136863>\
**Category:** Rails\
**Created:** [May 6, 2008, 10:54pm UTC](https://www.ruby-forum.com/t/multiple-has-many-objects-in-one-form/136863 "2008-05-06T22:54:05Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![dino\_d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@dino\_d](https://www.ruby-forum.com/u/dino_d)\
**Post date:** [May 6, 2008, 10:54pm UTC](https://www.ruby-forum.com/t/multiple-has-many-objects-in-one-form/136863/1 "2008-05-06T22:54:05Z")

</div>

Hi - I have to models, Question and Answers. A question has many  
answers. I want to set up a form where a user can submit 1 question  
and up to 10 answers. So, in the questions controller, I have new:

def new  
@question = Question.new  
@answers=[]  
i=0  
while i\<10  
@answers \<\< Answer.new  
i += 1  
end  
end

In the form, I’m trying something like:

\<% form\_for :question do |f|%\>  
Question: \<%=f.text\_field :question%\>

\<%for @answer in @answers %\>  
\<%fields\_for :answer do|a|%\>  
Answer: \<%=a.text\_field :answer%\>   
  
\<%end%\>  
\<%end%\>

\<%end%\>

The problem is, this doesn’t index the answers, the html just shows 10  
repeated answer[answer]. Is there a better way?

The second question I have is, how do I validate for at least 1  
answer?

Thanks,  
Dino
