Reference other table

Hi Guys,
Another (hopefully) simple problem, but I’m finding little help on any
tutorials.

I currently have 2 tables, one with the users of a system and another
with the details of the program they are on, the program is referenced
in the users table with program_id.

Rails obviously hides this information in the list view as it is an id,
but i actually want it to cross reference the programs table and display
the program name (rather than the id, or nothing).

I’ve set the model up so that each user belongs to a program, and each
program has many users, and the edit page gives an option list with the
names of the programs and saves the id as you’d expect

I’ve seen one example that says simply writing program.name will do this
but I’ve had no joy… can someone shed some light/make it blindingly
obvious what I’m missing? thanks.

in case what I’ve written makes absolutely no sense, the offending
application can be found here:
http://spotter.derbyproject.com/users/list
as you can see, the users program is not displayed, but the edit page
allows the edit. I need to display the name of the program in the list.

cheers,
Lee

Rails obviously hides this information in the list view as it is an
id,
but i actually want it to cross reference the programs table and display
the program name (rather than the id, or nothing).

I’ve seen one example that says simply writing program.name will do this
but I’ve had no joy… can someone shed some light/make it blindingly
obvious what I’m missing? thanks.

Sounds (and looks) like you used the standard scaffolding to create
the view…

Use user.programm.name (or whatever the name of the loop variable is
instead of “user”)

Also, i would suggest to use eager loading to keep that line of code
above from repeatedly hitting the Database to fetch the programm name:

def index
@users = User.find :all, :include => :programm
end

Thanks Thorsten,

You’re right, I’m using standard scaffold code at this point,
attempting to work from it.
Have a couple of questions, at present, the loop writing the show
table looks like this

<% for column in User.content_columns %> <% end %>

<% for user in @users %>

<% for column in User.content_columns %> <% end %>
<td><%= link_to 'Show', :action => 'show', :id => user %></td>
<td><%= link_to 'Edit', :action => 'edit', :id => user %></td>
<td><%= link_to 'Destroy', { :action => 'destroy', :id =>

user }, :confirm => ‘Are you sure?’, :method => :post %>

<% end %>
<%= column.human_name %>
<%=h user.send(column.name) %>

So to display the program name as discussed would be as simple as an
if statement checking if it has come to the program column yet? or do
i need to hardcode the table columns?

I’m sorry these are pretty simple questions, I’m used to more
conventional JSP/CF scripting and ruby is a bit left-field for me!

Thanks!
Lee.

I wasn’t too clear up there, Thorsten was quite right and the code
worked (thanks!)
I’m just looking for the best way to go about implementing it, ideally
I don’t want to loose the way the app currently gets the column names
dynamically without me having to hard code them.

Cheers,
Lee.