# Making an child before the parent is commited to DB

**URL:** <https://www.ruby-forum.com/t/making-an-child-before-the-parent-is-commited-to-db/124965>\
**Category:** Rails\
**Created:** [January 4, 2008, 3:47am UTC](https://www.ruby-forum.com/t/making-an-child-before-the-parent-is-commited-to-db/124965 "2008-01-04T03:47:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![galeal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8c91f0/32.png) [@galeal](https://www.ruby-forum.com/u/galeal)\
**Post date:** [January 4, 2008, 3:47am UTC](https://www.ruby-forum.com/t/making-an-child-before-the-parent-is-commited-to-db/124965/1 "2008-01-04T03:47:26Z")

</div>

Say I have a model something like this  
Parent has\_many kids  
Kid belongs\_to Parent.

Now I have view for say a new Parent. I also have provided an AJAX form  
so that they can fill in all the children they have as well.

Whats the best way to go about adding a child to a parent at this stage.  
Nothing has been committed to the database. And I can’t add a child to  
the database without having a parent. But the parent has not been  
created yet.

Thanks in advance for help! 🙂

---

<div class="post-metadata">

**Author:** ![galeal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8c91f0/32.png) [@galeal](https://www.ruby-forum.com/u/galeal)\
**Post date:** [January 4, 2008, 4:36am UTC](https://www.ruby-forum.com/t/making-an-child-before-the-parent-is-commited-to-db/124965/2 "2008-01-04T04:36:53Z")

</div>

Take a look at ActiveRecord transactions.

If your params from the controller are :parent and :kids in your model  
method you would do a transaction:

def self.create\_family(params)  
Parent.transaction do  
parent = Parent.create(params[:parent])  
params[:kids].each do |kid|  
parent.kids.create(kid)  
end  
end  
end

If one of the creates fails, they all get rolled back.

John.

On Jan 3, 9:47 pm, Luke G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![galeal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8c91f0/32.png) [@galeal](https://www.ruby-forum.com/u/galeal)\
**Post date:** [January 4, 2008, 4:54am UTC](https://www.ruby-forum.com/t/making-an-child-before-the-parent-is-commited-to-db/124965/3 "2008-01-04T04:54:16Z")

</div>

Cheers for that, I think that’ll work.

Another question:  
I have bunch of text\_fields tags that get pushed onto the form via Ajax  
every time the user hits “add new child” now, if I wanted to add 4 kids  
to this family. Then I would have 4 text\_fields.

Is it possible to group the text\_fields in the view so that I can  
iterate through them in the code-behind?

---

<div class="post-metadata">

**Author:** ![galeal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8c91f0/32.png) [@galeal](https://www.ruby-forum.com/u/galeal)\
**Post date:** [January 4, 2008, 1:26pm UTC](https://www.ruby-forum.com/t/making-an-child-before-the-parent-is-commited-to-db/124965/4 "2008-01-04T13:26:56Z")

</div>

Have you seen the Railscast on this subject, I think it more or less  
describes what you’re trying to achieve.

> **[\#75 Complex Forms Part 3 - RailsCasts](http://railscasts.com/episodes/75-complex-forms-part-3)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

On 4 Jan, 03:54, Luke G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![galeal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8c91f0/32.png) [@galeal](https://www.ruby-forum.com/u/galeal)\
**Post date:** [January 4, 2008, 9:21pm UTC](https://www.ruby-forum.com/t/making-an-child-before-the-parent-is-commited-to-db/124965/5 "2008-01-04T21:21:36Z")

</div>

Thanks for sharing that wonderful resource. It looks exactly like the  
thing I’m after!
