# Get name from table has many , belong to relationship

**URL:** <https://www.ruby-forum.com/t/get-name-from-table-has-many-belong-to-relationship/174348>\
**Category:** Rails\
**Created:** [August 25, 2009, 12:54pm UTC](https://www.ruby-forum.com/t/get-name-from-table-has-many-belong-to-relationship/174348 "2009-08-25T12:54:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![karthik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9fc348/32.png) [@karthik](https://www.ruby-forum.com/u/karthik)\
**Post date:** [August 25, 2009, 12:54pm UTC](https://www.ruby-forum.com/t/get-name-from-table-has-many-belong-to-relationship/174348/1 "2009-08-25T12:54:09Z")

</div>

Hi Guys

I have a table with

Relationship

has many  
belong to

Employee table

employee id addressid  
12 12  
13 13

i need to access addressid data from address table

please guide me

–  
Karthik.k  
Mobile - +91-9894991640

---

<div class="post-metadata">

**Author:** ![karthik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9fc348/32.png) [@karthik](https://www.ruby-forum.com/u/karthik)\
**Post date:** [August 25, 2009, 1:24pm UTC](https://www.ruby-forum.com/t/get-name-from-table-has-many-belong-to-relationship/174348/2 "2009-08-25T13:24:00Z")

</div>

Hi Karthik,  
Firstly I should say you should be more specific than that, the info is  
not  
very clear.  
Secondly I will try to help you based on what I understand of your  
question.

Say you create a new employee this way :  
@employee = Employee.new(params[:employee])

You could do this in the controller create method :  
@employee.address = Address.find() #find the address you want.

Now I will also advice you, in the new.html.erb form where your filling  
the  
Employee info, you could have a select drop down with Address data and  
depending on what you select, you would then automatically assign the  
address.

example:)

_employee\_controller.rb_

def new  
@addresses = Address.all  
end

_employee/new.html.erb_

\<%= f.select :address\_id, @addresses.collect {|address| [address.text,  
address.id]}

the above will generate the needed select drop down for selecting the  
address, the values would be the id whereas the text of the options  
would be  
address.text assuming that text field is the field of address which  
holds  
the address string.

Hope it helps.

Thanks & Regards,  
Dhruva S…

Stephen  
Leacock[http://www.brainyquote.com/quotes/authors/s/stephen\_leacock.html](http://www.brainyquote.com/quotes/authors/s/stephen_leacock.html)

- “I detest life-insurance agents: they always argue that I shall some  
day  
die, which is not so.”

---

<div class="post-metadata">

**Author:** ![karthik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9fc348/32.png) [@karthik](https://www.ruby-forum.com/u/karthik)\
**Post date:** [August 25, 2009, 2:14pm UTC](https://www.ruby-forum.com/t/get-name-from-table-has-many-belong-to-relationship/174348/3 "2009-08-25T14:14:17Z")

</div>

I’d argue for a person table (employees are people too), an address  
table, and a full join table. A person can have many addresses, and an  
address can have many people. People move, and you may want to keep  
history on a person’s past addresses (especially if there’s a payroll or  
tax implication).

Person  
id, first\_name, last\_name, etc, etc  
has\_many :residences  
has\_many :addresses, :through =\> :residences

Address  
id, line\_1, line\_2, city, state, postal\_code, etc, etc  
has\_many :residences  
has\_many :people, :through =\> :residences

Residences  
person\_id  
address\_id  
additional fields, perhaps start\_date, end\_date, and anything else that  
might be related to an instance of this person at this address.  
belongs\_to :person  
belongs\_to :address

Maintenance of residences becomes a bit more involved (do you delete the  
residence, or just end it - which gets to that residence history  
question), but the flexibility might pay off later…

Showing field values is as simple as

@person.addresses.each do |address|  
address.city  
address.state  
address.postal\_code

or

@address.people.each do |person|  
person.first\_name  
person.last\_name
