I have a simple product and category models along with product_category models that establishes presence in the product category tree.
Product is always placed to the deepest possible categories.
It is intended to call
root_category = Category.find(1)
root_category.products
and basically get all products that have a category assigned.
What happens is that I can only get products from one of the descendant category if said category is selected instead of root_category
using
category.self_and_descendants.includes(:products)
How can I make it that I call category.products
that does basically the above?
== app/models/product.rb ==
class Product < ActiveRecord::Base
has_many :product_categories
has_many :categories, through: :product_categories
end
== app/models/product_category.rb ==
class ProductCategory < ActiveRecord::Base
belongs_to :product
belongs_to :category
end
== app/models/category.rb ==
class Category < ActiveRecord::Base
has_many :product_categories
has_many :products, through: :product_categories
end