views:

107

answers:

3

I am working on a Rails application which uses categories for items.

My category model is self-joined so that categories can be nested:

class Category < ActiveRecord::Base
  has_many :items

  # Self Join (categories can have subcategories)
  has_many   :subcategories, :class_name => "Category", :foreign_key => "parent_id"
  belongs_to :parent,        :class_name => "Category"
  ...
end

I have a form which allows a user to create an item which currently lists all categories in a select, but they are all listed together:

<%= f.label :category_id %>
<%= select :item, :category_id, Category.all.collect {|c| [ c.title, c.id ]} %>

So the select looks something like this:

Category1
Category2
Category3BelongsTo2
Category4BelongsTo1

But what I want is:

Category1
  - Category4BelongsTo1
Category2
  - Category3BelongsTo2

Is there a helper for this (which would be awesome!)? If not, how could I accomplish this?

Thanks!

A: 

Maybe grouped_options_for_select would help you

Tadas Tamosauskas
I still want to be able to select the parent categories.
James
+2  A: 

You might want to look at acts_as_nested_set, or awesome_nested_set

zetetic
+1  A: 

awesome_nested_set made this a piece of cake.

After installing, I added lft and rgt to the categories table and removed the self join. Then rebuilt the categories table using Category.rebuild!. Then the select can be easily populated like this:

<%= f.select :parent_id, nested_set_options(Category, @category) {|c| "#{'-' * c.level} #{c.title}" } %>
James