# Foreign keys not displayed for user input in create function

**URL:** <https://www.ruby-forum.com/t/foreign-keys-not-displayed-for-user-input-in-create-function/130448>\
**Category:** Rails\
**Created:** [February 28, 2008, 10:55am UTC](https://www.ruby-forum.com/t/foreign-keys-not-displayed-for-user-input-in-create-function/130448 "2008-02-28T10:55:14Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Skywalker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/71e660/32.png) [@Skywalker](https://www.ruby-forum.com/u/Skywalker)\
**Post date:** [February 28, 2008, 10:55am UTC](https://www.ruby-forum.com/t/foreign-keys-not-displayed-for-user-input-in-create-function/130448/1 "2008-02-28T10:55:14Z")

</div>

Newbie to Ruby - would appreciate it enormously if somebody could help  
me with this.

In my create function, for a specific table, my foreign keys in that  
table are not visible for selection. However, at the time of writing  
to the database, they take one or the other values on these foreing  
keys. I carried out the validates\_presence\_of on these foreign keys  
and that started generating errors but it still did not show the  
foreign keys.

Example

Exam |---------\< Exam\_Subjects

> --------| Subjects  
> Exam\_id Exam\_id (foreign-  
> key) Subject\_id  
> Exam\_Name Subject\_id (foreign-  
> key) Subject\_Name  
> Percent\_of\_questions

Difficulty\_level\_of\_subjects

After generating the scaffolding, when I go to exam\_subject/create  
screen, it only displays text-boxes to input Percent\_of\_questions and  
Difficulty\_level\_of\_subjects. Its doesnt show Subject\_id or Exam\_id  
for input and takes default values to fill it.

What I need is drop downs for Exam\_Name and Subject\_Name.

I tried using the validates\_presence\_of Exam\_id which actually  
resulted in generating an error but still no way of beinbg able to  
show the name fields for input.

I was going to write a code but think there must be some ruby method I  
can use to show all foreign keys for input? Is there one? Am I missing  
something?

---

<div class="post-metadata">

**Author:** ![Skywalker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/71e660/32.png) [@Skywalker](https://www.ruby-forum.com/u/Skywalker)\
**Post date:** [February 28, 2008, 1:19pm UTC](https://www.ruby-forum.com/t/foreign-keys-not-displayed-for-user-input-in-create-function/130448/2 "2008-02-28T13:19:54Z")

</div>

Hi Sky,

You need to add something like this to your form assuming you are  
using form\_for:-

\<%= f.select(‘exam\_id’, @exams.collect {|exam| [exam.name, exam.id]})  
%\>

You’ll initialise exams instance variable in your controller (@exams =  
Exam.find(:all)). You need to do something similar for the the Subject  
drop down. I hope that answers your question?

Jabbslad
