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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Al Evans
2006-Aug-26 16:39 UTC
Re: can i translate what name_ID from a belongs_to relations
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(<whatever>) town_name = profile.town.name --Al Evans -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
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(<whatever>) > town_name = profile.town.name > > --Al Evans-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
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.. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Jeff Dean
2006-Aug-27 23:50 UTC
Re: can i translate what name_ID from a belongs_to relations
Can you post the relevant code from your two models and migrations? On 8/26/06, koloa <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > 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.. > > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Max Muermann
2006-Aug-28 00:22 UTC
Re: can i translate what name_ID from a belongs_to relations
> 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 received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
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 Muermann 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-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Max Muermann
2006-Aug-28 02:26 UTC
Re: can i translate what name_ID from a belongs_to relations
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 On 8/28/06, koloa <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > > 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 Muermann 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 > > > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---