This is the category model. A category can belong to another category.
class Category < ActiveRecord::Base
attr_accessible :title, :parent_id
has_and_belongs_to_many :products, :join_table => :products_categories
belongs_to :parent, :foreign_key => "parent_id", :class_name => "Category"
has_many :categories, :foreign_key => "parent_id", :class_name => "Category"
end
This is the product model:
class Product < ActiveRecord::Base
attr_accessible :comment, location_id, :category_ids
has_and_belongs_to_many :categories, :join_table => :products_categories
belongs_to :location
end
In the Active Admin form for a product I want to hierarchically order the checkboxes based on their parent_id e.g.
- Category 1 [ ]
- Category 2 [ ]
- Category 3 [ ]
- Category 6 [ ]
- Category 4 [ ]
- Category 5 [ ]
- Category 7 [ ]
Below is as far as I've got with the form:
ActiveAdmin.register Product do
form do |f|
f.inputs "Product" do
f.input :comment
f.input :categories, :as => :check_boxes
f.input :location
end
f.buttons
end
end
Currently the form pulls in the checkboxes and saves the data correctly but I'm not sure where to begin with grouping them. I looked through the documentation but couldn't see anything obvious.