Skip to content

Instantly share code, notes, and snippets.

@jaturken
Created March 7, 2013 16:42
Show Gist options
  • Save jaturken/5109499 to your computer and use it in GitHub Desktop.
Save jaturken/5109499 to your computer and use it in GitHub Desktop.
UsersToPayQuery
class UsersToPayQuery
def self.find
# Select users who have no errors in groups, likes and shares, have wmr and have balanse to pay.
# A bit strange indentation. But it's easier to read this SQL in console with this indendation.
User.where("
NOT EXISTS(
SELECT id FROM shares
WHERE shares.error = 1
AND shares.declined = false
AND shares.user_id = users.id)
").where("
NOT EXISTS(
SELECT id FROM likes WHERE
likes.error = 1
AND likes.declined = false
AND likes.user_id = users.id)
").where("
NOT EXISTS(
SELECT id FROM groups WHERE
groups.error = 1
AND groups.declined = false
AND groups.user_id = users.id)
").where("wmr <>''").where("balance >= #{ AppConfig.payout_min_amount }")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment