Forum: Ruby on Rails To get the id value from parent table to child table.

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Balaji R. (Guest)
on 2008-12-08 13:57
(Received via mailing list)
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
databases and display the bugs

parent table id is compare to child table is resortid
using save page
  child table id=parent table id

To give any idea and any link
Ar C. (Guest)
on 2008-12-08 16:28
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

class resort_contact
  belongs_to :resort_basic

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:

There are tons of examples out there that don't have to be repeated
Balaji R. (Guest)
on 2008-12-09 06:56
(Received via mailing list)
  Im using the 5 tables+5 views+one model+one i can't
understand  and give any idea

Ar C. (Guest)
on 2008-12-09 19:35
Balaji Rajagopalan wrote:
> Hi
>   Im using the 5 tables+5 views+one model+one 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
This topic is locked and can not be replied to.