# Missing foreign key fields in scaffold views

**URL:** <https://www.ruby-forum.com/t/missing-foreign-key-fields-in-scaffold-views/61175>\
**Category:** Rails\
**Created:** [January 13, 2006, 7:23am UTC](https://www.ruby-forum.com/t/missing-foreign-key-fields-in-scaffold-views/61175 "2006-01-13T07:23:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![john\_old](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9f8e36/32.png) [@john\_old](https://www.ruby-forum.com/u/john_old)\
**Post date:** [January 13, 2006, 7:23am UTC](https://www.ruby-forum.com/t/missing-foreign-key-fields-in-scaffold-views/61175/1 "2006-01-13T07:23:35Z")

</div>

Is there any reason that foreign key fields do not show in any of the  
scaffold views ?

project table:  
id serial primary key,  
category\_id integer,  
user\_id integer,  
name varchar,  
morestuff varchar  
foreign key (category\_id) references categories(id),  
foreign key (user\_id) references users(id)

scaffold generated views only show name, morestuff fields

---

<div class="post-metadata">

**Author:** ![john\_old](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9f8e36/32.png) [@john\_old](https://www.ruby-forum.com/u/john_old)\
**Post date:** [January 13, 2006, 2:36pm UTC](https://www.ruby-forum.com/t/missing-foreign-key-fields-in-scaffold-views/61175/2 "2006-01-13T14:36:55Z")

</div>

John:

I don’t know the reason, but if it puts your mind at rest, you are  
doing nothing wrong. This is the way it is with scaffolding. It is  
very easy to add foreign key fields to the existing scaffolding. Just  
follow the pattern laid out by the fields that are there and use  
something like collection\_select to get a drop down to work with the  
linked tables.

bruce

---

<div class="post-metadata">

**Author:** ![john\_old](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9f8e36/32.png) [@john\_old](https://www.ruby-forum.com/u/john_old)\
**Post date:** [January 13, 2006, 5:51pm UTC](https://www.ruby-forum.com/t/missing-foreign-key-fields-in-scaffold-views/61175/3 "2006-01-13T17:51:31Z")

</div>

John John wrote:

> Is there any reason that foreign key fields do not show in any of the  
> scaffold views ?
> 
> project table:  
> id serial primary key,  
> category\_id integer,  
> user\_id integer,  
> name varchar,  
> morestuff varchar  
> foreign key (category\_id) references categories(id),  
> foreign key (user\_id) references users(id)
> 
> scaffold generated views only show name, morestuff fields

From what I read, scaffold causes rails to hide anything named id or  
ending with \_id.

If you want, say, dropdown menus from lookup tables ( I needed these.  
Took a long time to figure out how to do ) try this:

\<%= options\_from\_collection\_for\_select(Yourmodel.find(:all, :order =\>  
“id”), :id, :lookupField, selected\_value= someIDhere) %\>

someIDhere in my cases was the foreign key field in the parent table.  
This allowed me to get it to select the right entry for that record.

options\_from\_collection\_for\_select is in the rails API. You’ll be  
spending a LOT of time sifting through that if you plan to stick it out  
in rails. Good luck.

---

<div class="post-metadata">

**Author:** ![john\_old](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9f8e36/32.png) [@john\_old](https://www.ruby-forum.com/u/john_old)\
**Post date:** [January 13, 2006, 9:02pm UTC](https://www.ruby-forum.com/t/missing-foreign-key-fields-in-scaffold-views/61175/4 "2006-01-13T21:02:01Z")

</div>

Thank you. It would be helpful if someone could write an article on all  
the Rails extras that are available - I think some of us are missing out  
on a lot of features.

---

<div class="post-metadata">

**Author:** ![john\_old](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9f8e36/32.png) [@john\_old](https://www.ruby-forum.com/u/john_old)\
**Post date:** [January 13, 2006, 8:22pm UTC](https://www.ruby-forum.com/t/missing-foreign-key-fields-in-scaffold-views/61175/5 "2006-01-13T20:22:25Z")

</div>

On 1/12/06, John T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> foreign key (user\_id) references users(id)
> 
> scaffold generated views only show name, morestuff fields

If you want the user and category for the project scaffolded with a  
select box, you can look into the Scaffolding Extensions plugin.
