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
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.
On Jul 21, 2:17 pm, Perry S. [email protected]
wrote:
Thanks
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.