RAILS - SCOPE FILTER THROUGH 2 TABLES
Hi, I'm new to Rails and I'm using Translate to post here.
I have the scenario below: Ata_Public -> Ata_Object -> Products
How can I set up a filter to select all Ata_publics through the category_id inside Products?
scope :filter_category, -> (params) {
params[:category_id].present? ?
joins(:ata_objects)
.joins(:products)
.where("ata_objects.products.category_id = ?", params[:category_id])
:
all
}
I'm doing it this way, but I don't have the expected result. What should my scope look like?
Relationships are right.
clarifying, inside the Ata_Public model, I'm creating this scope: filter_category, to pass the ID and list only the minutes that have the product with this category_id.
https://i.stack.imgur.com/V0I0s.png
CodePudding user response:
This should work:
scope :filter_category, -> (params) {
joins(ata_objects: :products).where(
products: { category_id: params[:category_id] }
) if params[:category_id].present?
}