# Rails Newb: Foreign Key Views?

**URL:** <https://www.ruby-forum.com/t/rails-newb-foreign-key-views/56234>\
**Category:** Rails\
**Created:** [January 5, 2006, 5:44pm UTC](https://www.ruby-forum.com/t/rails-newb-foreign-key-views/56234 "2006-01-05T17:44:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![danhobbs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a5b964/32.png) [@danhobbs](https://www.ruby-forum.com/u/danhobbs)\
**Post date:** [January 5, 2006, 5:44pm UTC](https://www.ruby-forum.com/t/rails-newb-foreign-key-views/56234/1 "2006-01-05T17:44:30Z")

</div>

I’m a total rails newbie, but I’m learning fast. I have a question that  
I can’t seem to find an answer for:

What is the best way of coding views that represent foreign key  
relationships?

For example, while developing, I’ve created an “articles” table. I  
create some scaffolding code and modify it all to look nice. I then  
realise, I need an article\_categories related table.

I make the change in the database, modify the models with the  
“belongs\_to” and “has\_many”, but what is the best way to update my views  
(edit/new in particular)?

I want to have a drop-down list when editing or creating an article that  
allows me to select a category.

There is an example in the Agile book, but that refers to an array. I  
also found an example in the onlamp tutorials, but that doesn’t use the  
“\_form.rhtml” file, which I think would work better.

Is there a recommended way to do this?

Thanks,  
Dan

---

<div class="post-metadata">

**Author:** ![danhobbs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a5b964/32.png) [@danhobbs](https://www.ruby-forum.com/u/danhobbs)\
**Post date:** [January 5, 2006, 5:44pm UTC](https://www.ruby-forum.com/t/rails-newb-foreign-key-views/56234/2 "2006-01-05T17:44:43Z")

</div>

I have sellers, which sell time in units of time ( i.e. minutes,  
quarter hours, or hours )

Sellers need to set their units when they sign up.

In app/helpers/sellers\_helper.rb I have:

def get\_units  
Unit.find(:all,  
:order =\> ‘seconds’).map {|u| [u.name, u.id]}  
end

(in this case I need them all, not just those related to the  
seller, so it’s not a related join, but it certainly could be.  
If it were, I’d create a method in the Seller class to return  
related units…)

in app/views/sellers/\_form.rhtml I have:

\<%= text\_field ‘seller’,‘rate’ %\> per \<%= select ‘seller’,‘unit\_id’,  
get\_units %\>

I think that’s pretty clean.

–  
– Tom M.

---

<div class="post-metadata">

**Author:** ![danhobbs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a5b964/32.png) [@danhobbs](https://www.ruby-forum.com/u/danhobbs)\
**Post date:** [January 6, 2006, 2:40am UTC](https://www.ruby-forum.com/t/rails-newb-foreign-key-views/56234/3 "2006-01-06T02:40:11Z")

</div>

Hi Tom,

I was looking for something elegant and that does look like a good way  
of doing it.

If anyone has any other ideas, please pass them on!

Thanks,  
Dan
