I''m having, what appears to me to be, a bit of a complicated
select/collection_select helper problem.
Product belongs to SubCategory, which belongs to Category.
The form I''m using to edit a product has a collection_select that
draws from the product''s sub_category method:
<% @sub_categories = SubCategory.find(:all, :conditions => [
"category_id = ?", cat ]) %>
<%= collection_select(''product'',
''sub_category_id'', @sub_categories,
''id'', ''name'') %>
cat is a local variable passed to the partial that is drawn from
@product.sub_category.category.id
This collection_select automatically selects the product''s
sub_category.
I also need a select list that gives all the categories and that
automatically pre-selects the correct category for that product.
Because the product belongs_to SubCategory, and SubCategory belongs to
Category, I can''t work out how to reference the category''s id
as the
method in the collection_select helper. I''ve tried a few things,
including:
<% @categories = Category.find(:all) %>
<%= collection_select(''sub_category'',
''category_id'', @categories,
''id'', ''name'') %>
That, however, doesn''t pre-select the product''s category.
I''m sure I must be able to reference the category from the product
object, even though it''s once removed via the sub_category method.
I''ve tried putting:
<% @categories = Category.find(:all) %>
<%= collection_select(''product'',
''sub_category_category_id'',
@categories, ''id'', ''name'') %>
and similar things but it throws up an unknown method error.
To recap:
I want a drop-down list of categories, with the current product''s
category pre-selected. However, the product belongs_to a sub_category,
which in turn belongs_to a category. The product does not have a
direct belongs_to relationship with the category.
Oh, and I only plan on using the category selection list to update
sub_category selection list with the correct sub_categories through an
observe_field helper.
Any thoughts anyone?
Thanks :)
--
Matthew Revell
www.understated.co.uk