views:

23

answers:

1

Hello,

I have two tables 'users' and 'loads' in the models I have defined that 'loads' belongs to a user and that a user have many loads

Now when I want to show details for a load I use this in my controller:

@load = Load.find(params[:id])

and then in my view output data like:

<%=h @load.user_id %>
<%=h @load.date %>
.
.
.

What I would like is to output data from both tables so in this case in my view I instead of 'user_id' I would output the name of the user from the 'users' table.

All ideas are welcome.

+2  A: 

As you have defined that load belongs to a user you can just put

<%= h @load.user.name %>

And to avoid querying the DB one more time, change your find to

@load = Load.find(params[:id], :include => :user)
neutrino
Thank you, it works.
Adnan