I have Many-to-Many associations (this has been simplified for your convenience)
class Product
has_many :categorizations
has_many :categories, through: :categorization
end
class Category
has_many :categorizations
has_many :products, through: :categorization
end
I want to list the first 5 products of each category
But I can’t find a way to set the limit on enabled product. Below is my current request:
@categories = Category.includes(:products).all
The only solution I found is to add a condition to the model, for example:
class Category
...
has_many :products, include: product, limit: 5
end
Any suggestion? Thanks
source
share