# Can i translate what name\_ID from a belongs\_to relationship?

**URL:** <https://www.ruby-forum.com/t/can-i-translate-what-name-id-from-a-belongs-to-relationship/66530>\
**Category:** Rails\
**Created:** [August 26, 2006, 4:39pm UTC](https://www.ruby-forum.com/t/can-i-translate-what-name-id-from-a-belongs-to-relationship/66530 "2006-08-26T16:39:07Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![koloa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/4bbf92/32.png) [@koloa](https://www.ruby-forum.com/u/koloa)\
**Post date:** [August 26, 2006, 4:39pm UTC](https://www.ruby-forum.com/t/can-i-translate-what-name-id-from-a-belongs-to-relationship/66530/1 "2006-08-26T16:39:07Z")

</div>

hi. simple question. i have a table that contains a town\_id column. is  
there a way to get what town name it is from a town\_id? my table  
‘profiles’ belongs\_to table ‘town’, and town has\_many profiles. or do i  
just execute a find\_by\_sql on town\_id number and use the string name it  
returns?

thanks

---

<div class="post-metadata">

**Author:** ![koloa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/4bbf92/32.png) [@koloa](https://www.ruby-forum.com/u/koloa)\
**Post date:** [August 26, 2006, 6:39pm UTC](https://www.ruby-forum.com/t/can-i-translate-what-name-id-from-a-belongs-to-relationship/66530/2 "2006-08-26T18:39:12Z")

</div>

koloa wrote:

> hi. simple question. i have a table that contains a town\_id column. is  
> there a way to get what town name it is from a town\_id? my table  
> ‘profiles’ belongs\_to table ‘town’, and town has\_many profiles. or do i  
> just execute a find\_by\_sql on town\_id number and use the string name it  
> returns?

You should be able to say

profile = Profile.find()  
town\_name = profile.town.name

–Al Evans

---

<div class="post-metadata">

**Author:** ![koloa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/4bbf92/32.png) [@koloa](https://www.ruby-forum.com/u/koloa)\
**Post date:** [August 27, 2006, 12:25am UTC](https://www.ruby-forum.com/t/can-i-translate-what-name-id-from-a-belongs-to-relationship/66530/3 "2006-08-27T00:25:33Z")

</div>

hi thanks for the reply.

i tried it but its a no go. i also tried appending id2name, to\_s, and  
to\_i but no avail. i even just tried printing out the town\_id but that  
also doesnt work?!

basically my

@profile = Profiles.get\_details

where get\_details returns hash of all columns for the profile from the  
Profiles table

profile should contain all the columns in the profile table, this  
includes town\_id which belongs to town table

but when i try printing out town\_id with this line  
logger.debug @profiles[0][‘doctype\_id’]

, i get ‘String can’t be coerced into Fixnum’

Al Evans wrote:

> koloa wrote:
> 
> > hi. simple question. i have a table that contains a town\_id column. is  
> > there a way to get what town name it is from a town\_id? my table  
> > ‘profiles’ belongs\_to table ‘town’, and town has\_many profiles. or do i  
> > just execute a find\_by\_sql on town\_id number and use the string name it  
> > returns?
> 
> You should be able to say
> 
> profile = Profile.find()  
> town\_name = profile.town.name
> 
> –Al Evans

---

<div class="post-metadata">

**Author:** ![koloa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/4bbf92/32.png) [@koloa](https://www.ruby-forum.com/u/koloa)\
**Post date:** [August 27, 2006, 12:31am UTC](https://www.ruby-forum.com/t/can-i-translate-what-name-id-from-a-belongs-to-relationship/66530/4 "2006-08-27T00:31:07Z")

</div>

closer now:

@profiles[0][‘doctype\_id’].to\_s it prints the id number, now i am  
working out trying to see if i can translate the id to the actual name  
that the id matches to…

---

<div class="post-metadata">

**Author:** ![koloa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/4bbf92/32.png) [@koloa](https://www.ruby-forum.com/u/koloa)\
**Post date:** [August 28, 2006, 1:59am UTC](https://www.ruby-forum.com/t/can-i-translate-what-name-id-from-a-belongs-to-relationship/66530/5 "2006-08-28T01:59:34Z")

</div>

Can you post the relevant code from your two models and migrations?

---

<div class="post-metadata">

**Author:** ![koloa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/4bbf92/32.png) [@koloa](https://www.ruby-forum.com/u/koloa)\
**Post date:** [August 28, 2006, 2:31am UTC](https://www.ruby-forum.com/t/can-i-translate-what-name-id-from-a-belongs-to-relationship/66530/6 "2006-08-28T02:31:41Z")

</div>

> basically my
> 
> @profile = Profiles.get\_details
> 
> where get\_details returns hash of all columns for the profile from the  
> Profiles table

Out of interest - why are you doing this instead of using the fields  
provided by ActiveRecord? The idea behind AR is that you can do  
Profiles.find(:all) and then for each profile use profile.name or even  
traverse relationships like profile.town.name.

Cheers,  
Max

---

<div class="post-metadata">

**Author:** ![koloa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/4bbf92/32.png) [@koloa](https://www.ruby-forum.com/u/koloa)\
**Post date:** [August 28, 2006, 4:10am UTC](https://www.ruby-forum.com/t/can-i-translate-what-name-id-from-a-belongs-to-relationship/66530/7 "2006-08-28T04:10:39Z")

</div>

hello,  
basically i have a search process that displays all doctors available in  
a given town. when the user clicks on a doctors name, i just wanted to  
display the doctors town/county from where his/her office is located.  
after sometime, i think i just will kepp that information seperate.

Max M. wrote:

> > basically my
> > 
> > @profile = Profiles.get\_details
> > 
> > where get\_details returns hash of all columns for the profile from the  
> > Profiles table
> 
> Out of interest - why are you doing this instead of using the fields  
> provided by ActiveRecord? The idea behind AR is that you can do  
> Profiles.find(:all) and then for each profile use profile.name or even  
> traverse relationships like profile.town.name.
> 
> Cheers,  
> Max

---

<div class="post-metadata">

**Author:** ![koloa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/4bbf92/32.png) [@koloa](https://www.ruby-forum.com/u/koloa)\
**Post date:** [August 28, 2006, 4:35am UTC](https://www.ruby-forum.com/t/can-i-translate-what-name-id-from-a-belongs-to-relationship/66530/8 "2006-08-28T04:35:48Z")

</div>

You can achieve this as well by passing a :select parameter to the  
find method of AR:

Doctor.find(:all, :select=\>“doctors.name, addresses.city,  
addresses.state”, :include=\>‘address’)

Doesn’t break your domain model this way.

Cheers,  
Max
