title: Rails model with conditional scope depending on database
body: <p>I'm implementing a scope for a model based on <a href="http://railscasts.com/episodes/189-embedded-association" rel="nofollow">this railscast</a>. The condition in the scope uses the binary AND operator <code>&amp;</code>, like this:</p>
  
  <pre><code>scope :with_role, lambda { |role| 
    {:conditions =&gt; "roles_mask &amp; #{2**ROLES.index(role.to_s)} &gt; 0"} 
  }
  </code></pre>
  
  <p>Since the database I'm currently targetting is Oracle, which uses the <code>BITAND</code> function instead of the <code>&amp;</code> operator, I've rewritten the condition like this:</p>
  
  <pre><code>{:conditions =&gt; "BITAND(roles_mask, #{2**ROLES.index(role.to_s)}) &gt; 0"}
  </code></pre>
  
  <p>My problem is, I would like to keep my code as database agnostic as possible, since in the future we are planning on targetting other databases. My current solution has been to check whether I'm using Oracle and define the scope accordingly, like this (<code>using_oracle</code> is a boolean that I calculate elsewhere):</p>
  
  <pre><code>if using_oracle 
    scope :with_role, lambda { |role| 
      {:conditions =&gt; "BITAND(roles_mask, #{2**ROLES.index(role.to_s)}) &gt; 0"}
    }
  else
    scope :with_role, lambda { |role| 
      {:conditions =&gt; "roles_mask &amp; #{2**ROLES.index(role.to_s)} &gt; 0"}
    }
  end
  </code></pre>
  
  <p>This works, but doesn't strike me as particularly elegant or ruby/rails-like. Could anyone kindly suggest better alternatives?</p>