ruby on rails - Using a joins query with multiple conditions -
i have query supposed fetch category names based on conditions. every category has number of subcategories, , every subcategory has providers. want display categories subcategories number of providers positive. doing this:
controller
@categories = category.with_providers
model:
category.joins(subcategories: [:service_providers]).uniq
based on task need show categories service_providers count more 0 and there @ least 1 provider published: true property.
my legacy problem here categories not relate service_providers.
categories have subcategories subcategories have service providers
this problem solved establishing through connection on category provider relation, since have spent time trying write correct query on initial question, know answer it.
you don't need check condition service_providers count more 0
, since joins
applies inner join
. need is:
category.joins(subcategories: [:service_providers]).where(service_providers: {published: true})
Comments
Post a Comment