Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How can i DRY these scopes?

  scope :reputed, -> {
    where{['coalesce(rs_reputations.value, 0) > ? OR purchases.force_active = ?', LOWEST_VOTE, true]}

  scope :reputed_or_mine, ->(user) {
    where{['coalesce(rs_reputations.value, 0) > ? OR purchases.force_active = ? OR purchases.user_id = ?', LOWEST_VOTE, true,]}
share|improve this question
'coalesce(rs_reputations.value, 0) > ? OR purchases.force_active = true OR (? AND purchases.user_id = ?)',
(if then -1 else end)

This is semantically equivalent to both, but the AND in the third clause prevents it from having an effect when there is no

share|improve this answer
No thanks. It's looks uglier and contains errors. – ck3g Sep 24 '12 at 5:44
@ck3g, can you be more specific about the errors? – Mike Samuel Sep 24 '12 at 17:30
Sure. will throw error Undefined method id for NilClass or called id for nil... if user will not passed. Your solution is based on this. – ck3g Sep 24 '12 at 23:21
@ck3g, Understood. In that case, I'd declare the user argument with a default value that has an id of nil. – Mike Samuel Sep 25 '12 at 1:04
with OR in there, you don't get much other options. I don't support collecting in several steps and merging afterwards, because that's what a relational database is usually the best at. So I'm upvoting this solution. – Simon B. Jun 21 '13 at 6:59

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.