# Want to show content of foreign key in rhtml-- Newbie

**URL:** <https://www.ruby-forum.com/t/want-to-show-content-of-foreign-key-in-rhtml-newbie/108701>\
**Category:** Rails\
**Created:** [July 6, 2007, 5:38am UTC](https://www.ruby-forum.com/t/want-to-show-content-of-foreign-key-in-rhtml-newbie/108701 "2007-07-06T05:38:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 6, 2007, 5:38am UTC](https://www.ruby-forum.com/t/want-to-show-content-of-foreign-key-in-rhtml-newbie/108701/1 "2007-07-06T05:38:01Z")

</div>

Hi I have a scaffold CRUD app going and have managed to get my foreign  
key to populate a select dropdown list in the edit/new \_form.rhtml  
It seemed logical to have a foreign key with a select list.  
Unfortunately, I can’t figure out how to get the content of that same  
foreign key to appear in show or list actions.

My tables are:

artworks

categories

In the artworks table there is a column named category\_id and it  
belongs\_to :category, :foreign\_key =\> ‘category\_name’

In the categories table there is a column named category\_name

Any tips or help here? Do I need to do something in my category.rb or  
artwork.rb models? or in my admin\_controller.rb controller?

The default scaffold template show.rhtml uses:  
for columns in Artwork.content\_columns

(I’m assuming this is the same as:  
Artwork.content\_columns.each do |column| … end )

I checked out the content\_columns method in the API and says that it  
strips out an array item that ends with \_id but that seems to be the  
logical choice for a foreign key!  
So, if I change the schema to use a different name for the foreign  
key, will it work? Or am I way lost. (I feel lost)

Please be gentle and verbose!

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 6, 2007, 10:52pm UTC](https://www.ruby-forum.com/t/want-to-show-content-of-foreign-key-in-rhtml-newbie/108701/2 "2007-07-06T22:52:40Z")

</div>

Wow, this is a pretty unresponsive list here.  
I’m glad Ruby-Talk is not like this.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 7, 2007, 12:02am UTC](https://www.ruby-forum.com/t/want-to-show-content-of-foreign-key-in-rhtml-newbie/108701/3 "2007-07-07T00:02:46Z")

</div>

On Jul 5, 8:36 pm, “removed\_email\_address@domain.invalid”  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> categories
> 
> In the artworks table there is a column named category\_id and it  
> belongs\_to :category, :foreign\_key =\> ‘category\_name’

I think category\_id is your foreign key there, just delete the  
foreign\_key arg… you don’t need it at all since AR will default to  
category\_id in this case… and I suspect it is causing you trouble.

> In the categories table there is a column named category\_name
> 
> Any tips or help here? Do I need to do something in my category.rb or  
> artwork.rb models? or in my admin\_controller.rb controller?

don’t think so

> The default scaffold template show.rhtml uses:  
> for columns in Artwork.content\_columns
> 
> (I’m assuming this is the same as:  
> Artwork.content\_columns.each do |column| … end )

They’re actually not exactly the same, the variables are scoped  
differently but in this instance they likely work the same.

> I checked out the content\_columns method in the API and says that it  
> strips out an array item that ends with \_id but that seems to be the  
> logical choice for a foreign key!  
> So, if I change the schema to use a different name for the foreign  
> key, will it work? Or am I way lost. (I feel lost)
> 
> Please be gentle and verbose!

try using column\_names (Artwork.column\_names) instead.

good luck!  
Tim

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 8, 2007, 7:21pm UTC](https://www.ruby-forum.com/t/want-to-show-content-of-foreign-key-in-rhtml-newbie/108701/4 "2007-07-08T19:21:59Z")

</div>

Thanks for the help. I found my answers by working through the  
tutorial at OnLamp. It answered my questions while being simple and  
focused enough to prevent other things compounding my questions. The  
Agile book has a lot in it, but the tutorial there is just a little  
too kitchen-sink to grasp some aspects well.

Turns out, there were indeed some automagic naming conventions I  
should have used, but didn’t and that threw me off.

Rails is certainly different from many Ruby programs. I’m starting to  
see that Rails does more for me if I do a lot more the way it expects  
things.
