My votes
table looks like this:
id: integer
vote: boolean
voteable_id: integer
voteable_type: string
voter_id: integer
voter_type: string
The vote
column determines whether the row represents an "up vote" (vote = true
) or a "down vote" (vote = false
).
voteable_type
is the class of the thing being voted on, voteable_id
is the id of the thing being voted on, voter_type
is the class of the voter, and voter_id
is the id of the voter.
What I need is a query to get the top n posts
in descending order by "vote score", where "vote score" is defined as (the number of up votes the post has) - (the number of down votes the post has).
Bonus points if your solution doesn't require me to resort to find_by_sql()
(I'm working in Rails)
More bonus points if your solution works the same way in SQLite and PostgreSQL (though it's more important that it work in PostgreSQL).