# Creating multiple child objects from the same form

**URL:** <https://www.ruby-forum.com/t/creating-multiple-child-objects-from-the-same-form/52698>\
**Category:** Rails\
**Created:** [January 18, 2006, 2:21am UTC](https://www.ruby-forum.com/t/creating-multiple-child-objects-from-the-same-form/52698 "2006-01-18T02:21:39Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![derdewey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c89c15/32.png) [@derdewey](https://www.ruby-forum.com/u/derdewey)\
**Post date:** [January 18, 2006, 2:21am UTC](https://www.ruby-forum.com/t/creating-multiple-child-objects-from-the-same-form/52698/1 "2006-01-18T02:21:39Z")

</div>

I’ve been digging into rails, but this one question has been a two day  
stumper. I’ve got a question/answer model that I’m trying to work out.  
Each question has multiple answers (they’re more like choices, it  
doesn’t matter whether they’re right or wrong), and I’d like to have the  
question creation form also have multiple fields in which to put the  
answer choices. But with my code, I can only do one successful child  
creation. The log only shows that one answer is being passed in.

Here’s the code as of right now:

############ models  
class Answer \< ActiveRecord::Base  
belongs\_to :question  
validates\_presence\_of :answer, :question\_id  
end

class Question \< ActiveRecord::Base  
validates\_presence\_of :question  
has\_many :answers  
end

############ question controller  
def create  
@question = Question.new(params[:question])

```
if @question.save
  logger.debug(@question.inspect)
  params[:question_answer].each_value do |answer|
    @question.answers << Answer.new(:answer => answer)
  end

  flash[:notice] = 'Question was successfully created.'
  redirect_to :action => 'list'
else
  render :action => 'new'
end

```

end

############# question’s \_form  
\<%= error\_messages\_for ‘question’ %\>

Question  
\<%= text\_area 'question', 'question', {:cols=\>"80", :rows=\>"5"} %\>

Answer  
\<%= text\_area "question\_answer", 'answer[]', {:cols=\>"80", :rows=\>"5"} %\>

Answer  
\<%= text\_area "question\_answer", 'answer[]', {:cols=\>"80", :rows=\>"5"} %\>

---

<div class="post-metadata">

**Author:** ![derdewey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c89c15/32.png) [@derdewey](https://www.ruby-forum.com/u/derdewey)\
**Post date:** [January 18, 2006, 2:39am UTC](https://www.ruby-forum.com/t/creating-multiple-child-objects-from-the-same-form/52698/2 "2006-01-18T02:39:56Z")

</div>

The reason you’re only getting one answer field back on the post is  
because  
the names of the text areas are the same. Change the second one to  
name=“question\_answer\_2” and you will see it in the params array.

---

<div class="post-metadata">

**Author:** ![derdewey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c89c15/32.png) [@derdewey](https://www.ruby-forum.com/u/derdewey)\
**Post date:** [January 18, 2006, 3:01am UTC](https://www.ruby-forum.com/t/creating-multiple-child-objects-from-the-same-form/52698/3 "2006-01-18T03:01:04Z")

</div>

Andrew S. wrote:

> The reason you’re only getting one answer field back on the post is  
> because  
> the names of the text areas are the same. Change the second one to  
> name=“question\_answer\_2” and you will see it in the params array.

Thanks for the tip, I’ve tried what you just offered and it still only  
put in the first answer, the second answer was not input.

################ New \_form.rhtml

\<%= error\_messages\_for ‘question’ %\>

Question  
\<%= text\_area 'question', 'question', {:cols=\>"80", :rows=\>"5"} %\>

Answer  
\<%= text\_area "question\_answer", 'answer[]', {:cols=\>"80", :rows=\>"5"} %\>

Answer  
\<%= text\_area "question\_answer\_2", 'answer[]', {:cols=\>"80", :rows=\>"5"} %\>

---

<div class="post-metadata">

**Author:** ![derdewey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c89c15/32.png) [@derdewey](https://www.ruby-forum.com/u/derdewey)\
**Post date:** [January 18, 2006, 3:10am UTC](https://www.ruby-forum.com/t/creating-multiple-child-objects-from-the-same-form/52698/4 "2006-01-18T03:10:10Z")

</div>

You’re not going to be able to get the multiple values with:

params[:question\_answer].each\_value do |answer|

you’re going to need to explicitly get each question\_answer:

e.g.  
a1 = params[:question\_answer\_1]  
a2 = params[:question\_answer\_2]

---

<div class="post-metadata">

**Author:** ![derdewey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c89c15/32.png) [@derdewey](https://www.ruby-forum.com/u/derdewey)\
**Post date:** [January 18, 2006, 3:12am UTC](https://www.ruby-forum.com/t/creating-multiple-child-objects-from-the-same-form/52698/5 "2006-01-18T03:12:57Z")

</div>

Andrew S. wrote:

> You’re not going to be able to get the multiple values with:
> 
> params[:question\_answer].each\_value do |answer|
> 
> you’re going to need to explicitly get each question\_answer:
> 
> e.g.  
> a1 = params[:question\_answer\_1]  
> a2 = params[:question\_answer\_2]

Isn’t there a better way? Because there will be an arbitrary number of  
answers, so hard coding that in would not work. There isn’t anyway to  
pass all the values into a hash from the form?

---

<div class="post-metadata">

**Author:** ![derdewey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c89c15/32.png) [@derdewey](https://www.ruby-forum.com/u/derdewey)\
**Post date:** [January 18, 2006, 3:16am UTC](https://www.ruby-forum.com/t/creating-multiple-child-objects-from-the-same-form/52698/6 "2006-01-18T03:16:12Z")

</div>

you could do something like:

@params.each{|key,value|  
if key.downcase =~ /^question\_answer/  
@question.answers \<\< Answer.new(:answer =\> value)  
end  
}

---

<div class="post-metadata">

**Author:** ![derdewey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c89c15/32.png) [@derdewey](https://www.ruby-forum.com/u/derdewey)\
**Post date:** [January 18, 2006, 3:31am UTC](https://www.ruby-forum.com/t/creating-multiple-child-objects-from-the-same-form/52698/7 "2006-01-18T03:31:19Z")

</div>

Thanks Will, I did not know that. 🙂

---

<div class="post-metadata">

**Author:** ![derdewey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c89c15/32.png) [@derdewey](https://www.ruby-forum.com/u/derdewey)\
**Post date:** [January 18, 2006, 3:22am UTC](https://www.ruby-forum.com/t/creating-multiple-child-objects-from-the-same-form/52698/8 "2006-01-18T03:22:14Z")

</div>

If you name each field “question\_answer[]”, the value  
params[:question\_answer] will be an array of the values entered.

You should probably add the HTML option :name =\> “question\_answer[]”  
to each tag. Let me know how that works for you.

Andrew’s solution of using regexes to parse all the params is also  
workable, but I think storing them all into an array to begin with is  
a little more elegant.

-Will

---

<div class="post-metadata">

**Author:** ![derdewey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c89c15/32.png) [@derdewey](https://www.ruby-forum.com/u/derdewey)\
**Post date:** [January 18, 2006, 3:33am UTC](https://www.ruby-forum.com/t/creating-multiple-child-objects-from-the-same-form/52698/9 "2006-01-18T03:33:05Z")

</div>

Andrew S. wrote:

> you could do something like:
> 
> @params.each{|key,value|  
> if key.downcase =~ /^question\_answer/  
> @question.answers \<\< Answer.new(:answer =\> value)  
> end  
> }

Wow, that’s a slick way of doing it!

Now it inserts all the values at the same time, only catch is that it  
includes the data type. How can I tell what else is inside of the  
‘value’ object?

— !ruby/hash:HashWithIndifferentAccess answer: W

---

<div class="post-metadata">

**Author:** ![derdewey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c89c15/32.png) [@derdewey](https://www.ruby-forum.com/u/derdewey)\
**Post date:** [January 18, 2006, 3:49am UTC](https://www.ruby-forum.com/t/creating-multiple-child-objects-from-the-same-form/52698/10 "2006-01-18T03:49:27Z")

</div>

I used the regex in the past for my pages. For instance, I may have  
checkboxes for departments on a company division edit page. I would  
append  
the department\_id to the name of the check box (department\_1), then flip  
through with the regex, split the key to get the id and add departments  
to  
the division.

By using the array as you pointed out, I would have to query to find the  
department by the name or description (or whatever) that was shown to  
the  
user.

Or, am I missing something completely obvious.

---

<div class="post-metadata">

**Author:** ![derdewey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c89c15/32.png) [@derdewey](https://www.ruby-forum.com/u/derdewey)\
**Post date:** [January 18, 2006, 3:58am UTC](https://www.ruby-forum.com/t/creating-multiple-child-objects-from-the-same-form/52698/11 "2006-01-18T03:58:30Z")

</div>

You could also do fields with names:

question\_answer[answer1], question\_answer[answer2], etc…

This makes params[:question\_answer] a hash w/ “answer1” as the key and  
the value in the “answer1” field as the value, “answer2” as the key  
and the value in “answer2” as the field value… does that help at  
all?

-Will

---

<div class="post-metadata">

**Author:** ![derdewey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c89c15/32.png) [@derdewey](https://www.ruby-forum.com/u/derdewey)\
**Post date:** [January 18, 2006, 4:02am UTC](https://www.ruby-forum.com/t/creating-multiple-child-objects-from-the-same-form/52698/12 "2006-01-18T04:02:04Z")

</div>

Andrew S. wrote:

> I used the regex in the past for my pages. For instance, I may have  
> checkboxes for departments on a company division edit page. I would  
> append  
> the department\_id to the name of the check box (department\_1), then flip  
> through with the regex, split the key to get the id and add departments  
> to  
> the division.
> 
> By using the array as you pointed out, I would have to query to find the  
> department by the name or description (or whatever) that was shown to  
> the  
> user.
> 
> Or, am I missing something completely obvious.

I had the idea that you could pass in values using a [] at the end of  
the variable name and it would then let you treat the variable as a  
hash. Guess not. Which is a shame, because that could be very handy!

---

<div class="post-metadata">

**Author:** ![derdewey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c89c15/32.png) [@derdewey](https://www.ruby-forum.com/u/derdewey)\
**Post date:** [January 18, 2006, 4:04am UTC](https://www.ruby-forum.com/t/creating-multiple-child-objects-from-the-same-form/52698/13 "2006-01-18T04:04:32Z")

</div>

Yeah, that makes sense. Not that I was having trouble with the regex,  
but I  
always like to find cleaner implementations. Especially since, compared  
to  
JAVA, I’m really new to Ruby.

thanks again… 🙂

---

<div class="post-metadata">

**Author:** ![derdewey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c89c15/32.png) [@derdewey](https://www.ruby-forum.com/u/derdewey)\
**Post date:** [January 18, 2006, 4:07am UTC](https://www.ruby-forum.com/t/creating-multiple-child-objects-from-the-same-form/52698/14 "2006-01-18T04:07:06Z")

</div>

Will B. wrote:

> You could also do fields with names:
> 
> question\_answer[answer1], question\_answer[answer2], etc…
> 
> This makes params[:question\_answer] a hash w/ “answer1” as the key and  
> the value in the “answer1” field as the value, “answer2” as the key  
> and the value in “answer2” as the field value… does that help at  
> all?
> 
> -Will

Yea, I had tried that and rails spit out that it wasn’t allowed as an  
instance name. This is what I just tried now: \<%= text\_area  
“question\_answer[1]”, ‘answer’, {:cols=\>“80”, :rows=\>“5”} %\> and rails  
said “`@question\_answer[1]’ is not allowed as an instance variable  
name”.

---

<div class="post-metadata">

**Author:** ![derdewey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c89c15/32.png) [@derdewey](https://www.ruby-forum.com/u/derdewey)\
**Post date:** [January 18, 2006, 4:19am UTC](https://www.ruby-forum.com/t/creating-multiple-child-objects-from-the-same-form/52698/15 "2006-01-18T04:19:36Z")

</div>

Ok try doing this:

\<%= text\_area “1”, ‘answer’, :prefix =\> ‘question\_answer’, {:cols =\>  
“80”, :rows =\> “5”} %\>

-Will

---

<div class="post-metadata">

**Author:** ![derdewey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c89c15/32.png) [@derdewey](https://www.ruby-forum.com/u/derdewey)\
**Post date:** [January 18, 2006, 4:15am UTC](https://www.ruby-forum.com/t/creating-multiple-child-objects-from-the-same-form/52698/16 "2006-01-18T04:15:08Z")

</div>

Any ideas on how to deal with “— !ruby/hash:HashWithIndifferentAccess  
answer: WHAT” being inserted instead of just “WHAT”?

I tried @question.answers \<\< Answer.new(:answer =\> value[answer]) which  
threw an error about being the incorrect method. Then I tried  
“@question.answers \<\< Answer.new(:answer =\> value.type)” so it would put  
into the table what kind of object it was, but then ruby said that it  
couldn’t dump anonymous class Class.

What’s a good way to see what the variables are and what they contain?

################### what’s in the controller  
@params.each{|key,value|  
if key.downcase =~ /^question\_answer/  
@question.answers \<\< Answer.new(:answer =\> value)  
end  
}

---

<div class="post-metadata">

**Author:** ![derdewey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c89c15/32.png) [@derdewey](https://www.ruby-forum.com/u/derdewey)\
**Post date:** [January 18, 2006, 4:22am UTC](https://www.ruby-forum.com/t/creating-multiple-child-objects-from-the-same-form/52698/17 "2006-01-18T04:22:37Z")

</div>

The value is from |key,value| is just going to be a string.

You can use

value.inspect  
value.class  
or just: value  
in a log statement to see different attributes of the object. of  
course,  
inspect varies on the output depending on what type of object it is.

---

<div class="post-metadata">

**Author:** ![derdewey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c89c15/32.png) [@derdewey](https://www.ruby-forum.com/u/derdewey)\
**Post date:** [January 18, 2006, 5:01am UTC](https://www.ruby-forum.com/t/creating-multiple-child-objects-from-the-same-form/52698/18 "2006-01-18T05:01:55Z")

</div>

Sorry to barge into your thread 🙂

However one thing about this approach is that supposing one of your  
answers fail validation, you will only get a rather cryptic error  
message like “Answer is invalid”, with no other explanatory text or any  
text fields highlighted (using the default scaffolds that is).

This will happen even if you did provide the appropriate error messages  
in your Answers class! Is there any way to sort of “bubble” the error  
messages from the child object up to the main form?

Thanks.

---

<div class="post-metadata">

**Author:** ![derdewey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c89c15/32.png) [@derdewey](https://www.ruby-forum.com/u/derdewey)\
**Post date:** [January 18, 2006, 4:27am UTC](https://www.ruby-forum.com/t/creating-multiple-child-objects-from-the-same-form/52698/19 "2006-01-18T04:27:44Z")

</div>

Andrew S. wrote:

> The value is from |key,value| is just going to be a string.
> 
> You can use
> 
> value.inspect  
> value.class  
> or just: value  
> in a log statement to see different attributes of the object. of  
> course,  
> inspect varies on the output depending on what type of object it is.

Ah, so type wasn’t working but inspect does the trick! Then I figured  
out that it needed the string “answer” as the key. So with  
Answer.new(:answer =\> value[“answer”]) my little question/answer system  
is in good shape!

---

<div class="post-metadata">

**Author:** ![derdewey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c89c15/32.png) [@derdewey](https://www.ruby-forum.com/u/derdewey)\
**Post date:** [January 18, 2006, 5:18am UTC](https://www.ruby-forum.com/t/creating-multiple-child-objects-from-the-same-form/52698/20 "2006-01-18T05:18:03Z")

</div>

Xavier L. wrote:

> This solution isn’t as elegant as I at first thought. I’m trying to get  
> the edit method to work, and it seems like it’s repeating what I did for  
> the create method. And I thought ruby was all about DRY 🙂
> 
> The model is pretty simple, but I don’t know how it will react when it  
> fails validation. There has to be a better way, right?

I hope there is a better way. 😕

Surely there is someway to hack the \_form partial into doing something  
useful for objects that has\_many instances of a child subclass… right?

_crickets chirp_

[Next page](https://www.ruby-forum.com/t/creating-multiple-child-objects-from-the-same-form/52698.md?page=2)
