# To get the id value from parent table to child table

**URL:** <https://www.ruby-forum.com/t/to-get-the-id-value-from-parent-table-to-child-table/196858>\
**Category:** Rails\
**Created:** [December 8, 2008, 12:57pm UTC](https://www.ruby-forum.com/t/to-get-the-id-value-from-parent-table-to-child-table/196858 "2008-12-08T12:57:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rbalaji](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19b73/32.png) [@rbalaji](https://www.ruby-forum.com/u/rbalaji)\
**Post date:** [December 8, 2008, 12:57pm UTC](https://www.ruby-forum.com/t/to-get-the-id-value-from-parent-table-to-child-table/196858/1 "2008-12-08T12:57:03Z")

</div>

Hi  
Im using 5 tables and one parent table and 4 child tables

To get the id value from parent table to child table.To store the values  
in  
databases and display the bugs

parent table id is compare to child table is resortid  
code  
using save page  
…  
@resort\_contact.resortid=@resort\_basic.id  
child table id=parent table id

To give any idea and any link  
Thanks

---

<div class="post-metadata">

**Author:** ![rbalaji](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19b73/32.png) [@rbalaji](https://www.ruby-forum.com/u/rbalaji)\
**Post date:** [December 8, 2008, 3:28pm UTC](https://www.ruby-forum.com/t/to-get-the-id-value-from-parent-table-to-child-table/196858/2 "2008-12-08T15:28:28Z")

</div>

Basic vanilla Rails…

Your parent table (resort\_basic?) should have an ID column.

All your child tables (like resort\_contact?) should include a field  
named resort\_basic\_id of type integer.

class resort\_basic  
has\_many :resort\_contacts  
etc, etc  
end

class resort\_contact  
belongs\_to :resort\_basic  
end

You can:  
 → use a hidden field on the resort\_contact form to store the  
resort\_basic\_id, setting this value in the resort\_contact controller’s  
new method if the vlaue is known, then retrieve that from the params in  
the create method

→ or the resort\_contact form may include a droplist field where you  
choose the resort\_basic ID as part of creating the resort contact

→ or some other more advanced methods, but the nature of your question  
leads me to believe that you should stick to the basics for now.

As for links:

[http://api.rubyonrails.org/](http://api.rubyonrails.org/)  
or

> **[Google](https://www.google.com)**
>
> Happy Fourth of July 2023! #GoogleDoodle

There are tons of examples out there that don’t have to be repeated  
here.

---

<div class="post-metadata">

**Author:** ![rbalaji](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19b73/32.png) [@rbalaji](https://www.ruby-forum.com/u/rbalaji)\
**Post date:** [December 9, 2008, 5:56am UTC](https://www.ruby-forum.com/t/to-get-the-id-value-from-parent-table-to-child-table/196858/3 "2008-12-09T05:56:28Z")

</div>

Hi  
Im using the 5 tables+5 views+one model+one controller.so i can’t  
understand and give any idea

balaji

---

<div class="post-metadata">

**Author:** ![rbalaji](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19b73/32.png) [@rbalaji](https://www.ruby-forum.com/u/rbalaji)\
**Post date:** [December 9, 2008, 6:35pm UTC](https://www.ruby-forum.com/t/to-get-the-id-value-from-parent-table-to-child-table/196858/4 "2008-12-09T18:35:12Z")

</div>

Balaji Rajagopalan wrote:

> Hi  
> Im using the 5 tables+5 views+one model+one controller.so i can’t  
> understand and give any idea
> 
> balaji

Generally for Rails, 1 db table == 1 Rails model.

If you’re trying to do this with a single model, I assume that you’ll  
have to:

1. construct that model so it contains equivalents for all the fields  
from all the tables.
2. Pass this model from view to view, editing some fields, and keeping  
the rest as hidden fields on each form. Like form1 shows fields 1 and 2,  
hides fields 3 through 10. Form 2 shows fields 3 and 4, hides 1, 2, 5  
through 10, etc.
3. the controller has to know which form submitted so it knows what to  
do next… go to the next form, or try to save the data. A variable  
stored in a hidden field should suffice for that, @current\_step or  
something like that.
4. When you finally submit from view 5 and go to save, extract the  
fields for the parent table and save that record. Re-read that record to  
get its id, and put that in the parent id field for each of the sets of  
fields you have to save to your child tables. And wrap it all in a  
transaction so you can rollback if the third or fourth model fails  
validation.
