Can i translate what name_ID from a belongs_to relationship?

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

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

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

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…

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

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

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

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