Hi, I want to create a page which will show all the resigtered users. I have two tables one users and another one country table. Each user has a foreign key to country table. When I display the users information, I want to show the user name and the country name. How can I do it in ruby? Is there a document somewhere to handle database operations like this? Thanks --~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---
DBC User wrote:> Hi, > > I want to create a page which will show all the resigtered users. I > have two tables one users and another one country table. Each user has > a foreign key to country table. When I display the users information, > I want to show the user name and the country name. How can I do it in > ruby? Is there a document somewhere to handle database operations like > this? > > ThanksYou want to look at belongs_to and has_many of ActiveRecord. I don''t have a quick example but it is really easy. In the migration for the users table, add a integer column of country_id. You do not add anything to the country migration. Then in the User model, add a "belongs_to country" line. In the Country model, add a "has_many users" line. Then you can do something like: u = Users.find(id) puts u.country.name All the magic will happen. -- 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?hl=en -~----------~----~----~----~------~----~------~--~---
On Jul 21, 2:17 pm, Perry Smith <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> DBC User wrote: > > Hi, > > > I want to create a page which will show all the resigtered users. I > > have two tables one users and another one country table. Each user has > > a foreign key to country table. When I display the users information, > > I want to show the user name and the country name. How can I do it in > > ruby? Is there a document somewhere to handle database operations like > > this? > > > Thanks > > You want to look at belongs_to and has_many of ActiveRecord. I don''t > have a quick example but it is really easy. > > In the migration for the users table, add a integer column of > country_id. You do not add anything to the country migration. Then in > the User model, add a "belongs_to country" line. In the Country model, > add a "has_many users" line. > > Then you can do something like: > > u = Users.find(id) > puts u.country.name > > All the magic will happen. > > -- > Posted viahttp://www.ruby-forum.com/.Thanks and yes thats what I was looking for and got it working. --~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---