# Foreign key in views

**URL:** <https://www.ruby-forum.com/t/foreign-key-in-views/183424>\
**Category:** Rails\
**Created:** [February 13, 2010, 6:57pm UTC](https://www.ruby-forum.com/t/foreign-key-in-views/183424 "2010-02-13T18:57:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![andkjaer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ce73a5/32.png) [@andkjaer](https://www.ruby-forum.com/u/andkjaer)\
**Post date:** [February 13, 2010, 6:57pm UTC](https://www.ruby-forum.com/t/foreign-key-in-views/183424/1 "2010-02-13T18:57:21Z")

</div>

Hi i have the following code:

class Color \< ActiveRecord::Base  
has\_many :products  
end

class Product \< ActiveRecord::Base  
belongs\_to :color  
end

Products index view:

\<% @products.each do |product| %\>

 \<%=h product.color\_id %\> \<%= link\_to 'Show', product %\> \<%= link\_to 'Edit', edit\_product\_path(product) %\> \<%= link\_to 'Destroy', product, :confirm =\> 'Are you sure?', :method =\> :delete %\> \<% end %\>

How do I get this:

\<%=h product.color\_id %\> to display the  
name of the color instead of the id?

---

<div class="post-metadata">

**Author:** ![andkjaer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ce73a5/32.png) [@andkjaer](https://www.ruby-forum.com/u/andkjaer)\
**Post date:** [February 13, 2010, 7:09pm UTC](https://www.ruby-forum.com/t/foreign-key-in-views/183424/2 "2010-02-13T19:09:03Z")

</div>

On 13 February 2010 17:56, andkjaer [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> How do I get this: \<%=h product.color\_id %\> to display the  
> name of the color instead of the id?

\<%=h product.color.name %\>
