Forum: Ruby on Rails Rails Newb: Foreign Key Views?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Dan H. (Guest)
on 2006-01-05 18:44
(Received via mailing list)
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
Tom M. (Guest)
on 2006-01-05 18:44
(Received via mailing list)
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.
Dan H. (Guest)
on 2006-01-06 03:40
(Received via mailing list)
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
This topic is locked and can not be replied to.