Search code examples
sqlruby-on-railsrubyruby-on-rails-3squeel

Is it possible to generate SQL `OR` clauses by using scope methods?


I am using Ruby on Rails 3.2.2 and the Squeel gem. I would like to know if there is a way to use more that one scope method so to generate SQL queries with OR clauses related to those scope method SQL conditions. That is, I have:

class Article < ActiveRecord::Base
  def self.scope_method_1
    ... # scope_method_1 SQL conditions
  end

  def self.scope_method_2
    ... # scope_method_2 SQL conditions
  end

  def self.scope_method_combination
    # I am looking for something like
    #
    # where{scope_method_1 | scope_method_2} # Note: The '|' means the SQL 'OR' clause in the Squeel context
    #
    # so to generate the following SQL query:
    #
    # SELECT FROM articles WHERE <scope_method_1 SQL conditions> OR <scope_method_2 SQL conditions>
  end
end

Is it possible to generate SQL OR clauses with scope methods (by using or not the Squeel gem)?


Solution

  • We have been discussing this on the squeel issue tracker and the basic answer seems to be no. At least not with scopes; but if you convert the scopes into sifters you can.