I have:
Product
=> has_many :reviews
Review
has a field recommendation:boolean
I need to retrieve the Product that has most Review.recommendation:true
What would be the optimal approach here?
The product must also be present in the given array of categories. I am trying something like this:
Product.joins(:reviews).where('category_id IN (?)', categories_ids).where(reviews: {recommendation: true}).group(:id)
Thanks.
This ended up being my approach:
res = Product.joins(:reviews).where('category_id IN (?)', categories_ids).where(reviews: {recommendation: true}).group(:id).count
result = res.sort_by {|k,v| v}
id = result.last[0]
Product.find(id)