# Inserting the parent Id to the child table

**URL:** <https://www.ruby-forum.com/t/inserting-the-parent-id-to-the-child-table/52714>\
**Category:** Rails\
**Created:** [January 18, 2006, 8:47am UTC](https://www.ruby-forum.com/t/inserting-the-parent-id-to-the-child-table/52714 "2006-01-18T08:47:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 18, 2006, 8:47am UTC](https://www.ruby-forum.com/t/inserting-the-parent-id-to-the-child-table/52714/1 "2006-01-18T08:47:30Z")

</div>

Hi All,  
I need your suggestions for the following. I am still learning RoR and  
pardon my ignorance.  
I have say two tables.  
Table A : students  
Table B : addresses

Table B has student\_id as the foreign key to Table A

## If I am displaying the list of students and wanted to get individual students addresses based on their id, is the only option is to get the id from students controller method list into the session and use that session value to populate the student\_id in the addresses table? ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++++++ List.rhtml (students)
 \<%= link\_to("Address" , :controller =\> "addresses", :action =\> "list", :id =\> student) %\>
## Addresses\_controller

def list  
@addresses = Address.find(:all, :conditions =\> [ “student\_id= (?)”,  
params[:id]], :limit =\> 5)  
@session[“student\_id”] = params[:id]  
End

def create  
@addresses = Address.new(params[:addresses])  
@addresses.student\_id=@session[“student\_id”]  
…  
end  
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Are there any other way to do this. Any help appreciated.  
Thanks in advance.

Silvy Mathews

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 18, 2006, 9:13am UTC](https://www.ruby-forum.com/t/inserting-the-parent-id-to-the-child-table/52714/2 "2006-01-18T09:13:19Z")

</div>

It looks like you haven’t yet gotten the Rails way of doing this stuff.  
Don’t think so much about the db, but think more about the models.

class Student \< ActiveRecord::Base  
has\_many :addresses  
…  
end

class Address \< ActiveRecord::Base  
belongs\_to :student  
…  
end

@student = Student.find(params[:id])  
@addrs = @student.addresses  
@address = @addrs.build(params[:address])

# and so on…

Check out the docs on ActiveRecord associations, specifically has\_many.

> **[ActiveRecord::Associations::ClassMethods](https://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html)**
>
> Associations are a set of macro-like class methods for tying objects together through foreign keys.

unknown wrote:

> Hi All,  
> I need your suggestions for the following. I am still learning RoR and  
> pardon my ignorance.  
> I have say two tables.  
> Table A : students  
> Table B : addresses
> 
> Table B has student\_id as the foreign key to Table A
> 
> ## If I am displaying the list of students and wanted to get individual students addresses based on their id, is the only option is to get the id from students controller method list into the session and use that session value to populate the student\_id in the addresses table? ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++++++ List.rhtml (students)
> \<%= link\_to("Address" , :controller =\> "addresses", :action =\> "list", :id =\> student) %\>
> ## Addresses\_controller
> 
> def list  
> @addresses = Address.find(:all, :conditions =\> [ “student\_id= (?)”,  
> params[:id]], :limit =\> 5)  
> @session[“student\_id”] = params[:id]  
> End
> 
> def create  
> @addresses = Address.new(params[:addresses])  
> @addresses.student\_id=@session[“student\_id”]  
> …  
> end  
> +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> 
> Are there any other way to do this. Any help appreciated.  
> Thanks in advance.
> 
> Silvy Mathews
