DBC User wrote:
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
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)
All the magic will happen.