3

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)?

Backo
  • 18,291
  • 27
  • 103
  • 170
  • Would it not suffice to write a scope_method_3 with the OR sql statement written into it? – Trip Jul 12 '12 at 00:34
  • 1
    @Trip - I think the `scope_method_3` you are thinking about could not follow the DRY (Don't Repeat Yourself) principle. – Backo Jul 12 '12 at 00:36

1 Answers1

1

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.

DGM
  • 26,629
  • 7
  • 58
  • 79
  • 1
    I agree, I think it has to do with keeping track of table names when doing joins, scopes just don't provide enough info to keep the algebra straight... – DGM Jul 12 '12 at 18:48