Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>For Rails 2.x, you could use the following named scope to simulate OR:</p> <pre><code> __or_fn = lambda do |*scopes| where = [] joins = [] includes = [] # for some reason, flatten is actually executing the scope scopes = scopes[0] if scopes.size == 1 scopes.each do |s| s = s.proxy_options begin where &lt;&lt; merge_conditions(s[:conditions]) rescue NoMethodError where &lt;&lt; scopes[0].first.class.merge_conditions(s[:conditions]) end joins &lt;&lt; s[:joins] unless s[:joins].nil? includes &lt;&lt; s[:include] unless s[:include].nil? end scoped = self scoped = scoped.includes(includes.uniq.flatten) unless includes.blank? scoped = scoped.joins(joins.uniq.flatten) unless joins.blank? scoped.where(where.join(" OR ")) end named_scope :or, __or_fn </code></pre> <p>Let's use this function using your example above.</p> <pre><code>q1 = Annotation.body_equals('?') q2 = Annotation.body_like('[?]') Annotation.or(q1,q2) </code></pre> <p>The above code executes only one query. <code>q1</code> and <code>q2</code> do not hold the results of the query, rather, their class is <code>ActiveRecord::NamedScope::Scope</code>.</p> <p>The <code>or</code> named_scope combines these queries and joins the conditions with an OR.</p> <p>You could also nest ORs, like in this contrived example:</p> <pre><code>rabbits = Animal.rabbits #&lt;Animal id: 1 ...&gt; puppies = Animal.puppies #&lt;Animal id: 2 ...&gt; snakes = Animal.snakes #&lt;Animal id: 3 ...&gt; lizards = Animal.lizards #&lt;Animal id: 4 ...&gt; Animal.or(rabbits, puppies) [#&lt;Animal id: 1 ...&gt;, #&lt;Animal id: 2 ...&gt;] Animal.or(rabbits, puppies, snakes) [#&lt;Animal id: 1 ...&gt;, #&lt;Animal id: 2 ...&gt;, #&lt;Animal id: 3 ...&gt;] </code></pre> <p>Because <code>or</code> returns a <code>ActiveRecord::NamedScope::Scope</code> itself, we can go really crazy:</p> <pre><code># now let's get crazy or1 = Animal.or(rabbits, puppies) or2 = Animal.or(snakes, lizards) Animal.or(or1, or2) [#&lt;Animal id: 1 ...&gt;, #&lt;Animal id: 2 ...&gt;, #&lt;Animal id: 3 ...&gt;, #&lt;Animal id: 4...&gt;] </code></pre> <p>I believe that most of these examples would work fine using <code>scope</code>s in Rails 3, although I have not tried.</p> <p>A Bit of shameless self-promotion - This functionality is available in the <a href="http://github.com/gammons/fake_arel" rel="nofollow">fake_arel gem</a>.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload