# Data insertion in multiple tables

**URL:** <https://www.ruby-forum.com/t/data-insertion-in-multiple-tables/51109>\
**Category:** Rails\
**Created:** [December 26, 2005, 5:04pm UTC](https://www.ruby-forum.com/t/data-insertion-in-multiple-tables/51109 "2005-12-26T17:04:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sung\_Soo\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e495f1/32.png) [@Sung\_Soo\_K](https://www.ruby-forum.com/u/Sung_Soo_K)\
**Post date:** [December 26, 2005, 5:04pm UTC](https://www.ruby-forum.com/t/data-insertion-in-multiple-tables/51109/1 "2005-12-26T17:04:08Z")

</div>

I have two tables like:  
Table A:  
id (autoincrement)  
name

Table B  
id  
id\_of\_A  
desc

I want to add a record to table A and based on the id of A, I want to  
add a  
record to table B.  
I don’t think there is any SQL command that support this (at least in  
MySQL  
4.1, so I use LAST\_INSERT\_ID()).  
(Is there any other way? or MySQL 5 support any special SQL command?)

But I need this functionality to start my project.  
Does Active Record support this functionality? or any other way to walk  
around?  
Or do I have to choose other databases than MySQL?

---

<div class="post-metadata">

**Author:** ![Sung\_Soo\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e495f1/32.png) [@Sung\_Soo\_K](https://www.ruby-forum.com/u/Sung_Soo_K)\
**Post date:** [December 26, 2005, 5:31pm UTC](https://www.ruby-forum.com/t/data-insertion-in-multiple-tables/51109/2 "2005-12-26T17:31:13Z")

</div>

Sung Soo K. wrote:

> I want to add a record to table A and based on the id of A, I want to add  
> a record to table B.  
> I don’t think there is any SQL command that support this (at least in  
> MySQL 4.1, so I use LAST\_INSERT\_ID()).  
> (Is there any other way? or MySQL 5 support any special SQL command?)
> 
> But I need this functionality to start my project.  
> Does Active Record support this functionality? or any other way to walk  
> around?  
> Or do I have to choose other databases than MySQL?  
> Hmm … let try this. I assume A and B has one to many relation, So A  
> can  
> have many B. The definition of A above is ok, but B has to change a bit,  
> say,

Table B  
id  
a\_id  
desc

That rails naming convention.  
Now, in model for A, add  
has\_many :bs # that pural of b

and in model for B, add  
belongs\_to :a

Then in say A controller, method create, do this

def create  
…  
new\_a = A.new params[:a] #well … depend on your view,  
new\_a.save  
# at this point new\_a has an id  
new\_b = B.new params[:b]  
new\_b.a = a # assing the a\_id,  
new\_b.save  
…  
end

That should work.  
tom

---

<div class="post-metadata">

**Author:** ![Sung\_Soo\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e495f1/32.png) [@Sung\_Soo\_K](https://www.ruby-forum.com/u/Sung_Soo_K)\
**Post date:** [December 26, 2005, 6:04pm UTC](https://www.ruby-forum.com/t/data-insertion-in-multiple-tables/51109/3 "2005-12-26T18:04:27Z")

</div>

If you setup your relationships correctly AR will handle it  
automatically for you.

e.g. a.b.create(“new description”)

is all you need and the a\_id in the B table will be set automatically  
and saved with your new item.

Chris Nolan.ca  
[http://kekova.ca/](http://kekova.ca/)

---

<div class="post-metadata">

**Author:** ![Sung\_Soo\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e495f1/32.png) [@Sung\_Soo\_K](https://www.ruby-forum.com/u/Sung_Soo_K)\
**Post date:** [December 26, 2005, 9:35pm UTC](https://www.ruby-forum.com/t/data-insertion-in-multiple-tables/51109/4 "2005-12-26T21:35:30Z")

</div>

I’m happy to hear that there is a solution.  
Thanks a lot.
