2012-07-12 47 views
3

我正在使用Ruby on Rails 3.2.2和Squeel gem。我想知道是否有一種方法可以使用多個範圍方法來生成與這些範圍方法SQL條件相關的OR子句的SQL查詢。也就是說,我有:是否可以通過使用範圍方法生成SQL`OR`子句?

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 

是否有可能生成SQL OR條款與範圍的方法(使用或不Squeel寶石)?

+0

寫一個scope_method_3寫入OR sql語句會不夠嗎? – Trip 2012-07-12 00:34:34

+1

@Trip - 我認爲你正在考慮的'scope_method_3'不能遵循DRY(不要重複自己)的原則。 – Backo 2012-07-12 00:36:11

回答

1

我們一直在squeel issue tracker上討論這個,基本答案似乎是否定的。至少不適用於示波器;但如果你將範圍轉換成篩子,你可以。

+0

在Squeel語句中使用作用域將會非常有幫助和強大... – Backo 2012-07-12 11:26:23

+1

我同意,我認爲它在做連接時跟蹤表名稱有關,範圍僅僅不提供足夠的信息來保持代數直... – DGM 2012-07-12 18:48:30

相關問題