1

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 %>

enter image description here

Dev
  • 437
  • 6
  • 25
  • I think it's not possible do to that, https://stackoverflow.com/questions/1037732/nesting-optgroups-in-a-dropdownlist-select, maybe you should try different approach or use dual combobox, or custom javascript component. – buncis May 16 '20 at 17:36

0 Answers0