I have the following associations setup:
class Category < ApplicationRecord
has_many :child_categories
end
class ChildCategory < ApplicationRecord
belongs_to :category
has_many :subcategories
end
class Subcategory < ApplicationRecord
belongs_to :child_category
has_many :child_subcategories
end
class ChildSubcategory < ApplicationRecord
belongs_to :subcategory
end
An example of the above structure is: Apparel(category) - Clothing(child category) - Men(subcategory) - Tshirts(child subcategory).
I have a simple form
where I create a product and I would like to associate that product with a child subcategory from a collection grouped_select input
. Basically this input will be multileveled, for example: Clothing(cant select this) under that Men(cant select this) and after that Tshirts(I will be able to select this and associate a product with a child subcategory).
I'm kind of stuck on how to populate the collection grouped_select input
, I can only get it to show the child categories and subcategories with the following. Any ideas how I can show the child subcategories as well?
@categories = ChildCategory.where(id: params[:category])
<%= f.input :category_id, collection: @categories.order(:name), as: :grouped_select, group_method: :subcategories, include_blank: false, include_hidden: false %>