# Reference other table

**URL:** <https://www.ruby-forum.com/t/reference-other-table/134714>\
**Category:** Rails\
**Created:** [April 14, 2008, 12:58pm UTC](https://www.ruby-forum.com/t/reference-other-table/134714 "2008-04-14T12:58:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lfeistel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ecc23a/32.png) [@lfeistel](https://www.ruby-forum.com/u/lfeistel)\
**Post date:** [April 14, 2008, 12:58pm UTC](https://www.ruby-forum.com/t/reference-other-table/134714/1 "2008-04-14T12:58:49Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![lfeistel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ecc23a/32.png) [@lfeistel](https://www.ruby-forum.com/u/lfeistel)\
**Post date:** [April 14, 2008, 1:11pm UTC](https://www.ruby-forum.com/t/reference-other-table/134714/2 "2008-04-14T13:11:00Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![lfeistel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ecc23a/32.png) [@lfeistel](https://www.ruby-forum.com/u/lfeistel)\
**Post date:** [April 14, 2008, 8:35pm UTC](https://www.ruby-forum.com/t/reference-other-table/134714/3 "2008-04-14T20:35:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![lfeistel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ecc23a/32.png) [@lfeistel](https://www.ruby-forum.com/u/lfeistel)\
**Post date:** [April 14, 2008, 9:02pm UTC](https://www.ruby-forum.com/t/reference-other-table/134714/4 "2008-04-14T21:02:01Z")

</div>

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.
