I have a question and I appreciate if anyone could help me please. I am working on a project, that project has a category and sub-category tables and the association between these two table are below
class Category < ApplicationRecord
has_many :sub_categories, dependent: :destroy
end
class SubCategory < ApplicationRecord
belongs_to :category, optional: true
end
And there is a user table, user can select multiple categories and it's sub-categories and store at another database table. I have created UserCategory model and associated the user model with that model to store category ids
class User < ApplicationRecord
has_many :user_categories, dependent: :destroy
has_many :categories, through: :user_categories
accepts_nested_attributes_for :user_categories, :allow_destroy => true
end
class UserCategory < ApplicationRecord
belongs_to :user, optional: true
belongs_to :category, optional: true
end
Now my question is that how can I store sleeted subcategory ids also with it's parent selected category ids? because I want to show individual user selected categories and sub categories at view page..