Get the value of a field pointed by a foreign key

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…

example:

table categories:
id, name

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

where recipes.category_id is a fk for categories.id

i’ve the list view for the recipes where i want to display something
like:

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
instead
of the category_id? (infact i have recipe.category_id

thanks for your help.

I think you need to look into Associations, which manage the table
relationships in your models.

On 4/16/07, Pingala P. [email protected] wrote:

in my views list.rhtml i’ve something like this:
recipe.category_id} %>

Posted via http://www.ruby-forum.com/.


http://www.web-buddha.co.uk