Rails Newb: Foreign Key Views?

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

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.

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 forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs