Skip to content Skip to sidebar Skip to footer

Activerecord Syntax For Finding All Items With An Average Rating Of X Or Greater When Using A Join

I have two models in my rails application. Items and Reviews. Reviews belong to Item and Items has many Reviews. The review model looks like this: create_table 'reviews', :force =&

Solution 1:

You should put your condition in having instead of where. Meaning:

Item.joins(:reviews).group("reviews.reviewable_id").having("avg(reviews.rating) > ?", 80)

Post a Comment for "Activerecord Syntax For Finding All Items With An Average Rating Of X Or Greater When Using A Join"