Forum: Ruby on Rails get the value of a field pointed by a foreign key

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Cdd7dfeca927834e8dad7a54684990ff?d=identicon&s=25 Pingala Pingala (pingala)
on 2007-04-16 16:48
Hello everybody,

new to Ruby on Rails...
i'm following the tutorial at

but i'm having some issues...

now what i want to know is how-to display in a list, the value of a
field that is pointed by the fk..


table categories:
id, name

table recipes:
id, category_id, title, description, ..

where recipes.category_id is a fk for

i've the list view for the recipes where i want to display something

Recipe Title                             Category
Smoked salmon on rocket leaves           Starters

now how can do that?

in my views list.rhtml i've something like this:
<% for recipe in @recipes %>
      <%= link_to %Q{#{recipe.title}}, :action => 'show', :id => recipe
        <%= link_to '(del)', { :action => 'destroy', :id => recipe },
:confirm => 'Are you sure?', :post => true %>
      <%= link_to %Q{#{XXXXXXXXXX}}, {:action => 'list', :category_id =>
recipe.category_id} %>
<% end %>

what am i supposed to put in XXXXXXXXXX ? to see the category name
of the category_id? (infact i have recipe.category_id

thanks for your help.
8125afaa6ade422d5d733d7a05222935?d=identicon&s=25 Dave Goodchild (Guest)
on 2007-04-16 16:50
(Received via mailing list)
I think you need to look into Associations, which manage the table
relationships in your models.

On 4/16/07, Pingala Pingala <> wrote:
> in my views list.rhtml i've something like this:
> recipe.category_id} %>
> --
> Posted via
> >

This topic is locked and can not be replied to.