user3298823
user3298823

Reputation: 302

Confusing regarding rails query

So I have this query

Vote.group(:photo_id).order('count_all desc').limit(10).count

that fetches all the votes and groups them by their photo_id and totals up the votes for those ids. I would like to add another constraint to it to only grab the ones with a vote count greater than X. I know I have to add a .where function somewhere but I'm not entirely sure where to put it or how to add in the new constraint...

Upvotes: 0

Views: 29

Answers (1)

Philip Hallstrom
Philip Hallstrom

Reputation: 19879

You want having (SQL keyword as well as Rails method):

http://api.rubyonrails.org/classes/ActiveRecord/QueryMethods.html#method-i-having

Vote.group(:photo_id).having('COUNT(*) > ?', x)…

Upvotes: 1

Related Questions