2013-05-01 41 views
0

我有一个rails范围语句,我想要2 where子句。 (当我完成它的工作时,我会清理硬编码)。Rails范围与2 where子句

这是在我的工作单模型:

scope :laborok, where("wostatus_id NOT IN (?)", [231, 230, 8466, 8467, 232]) and where("maxsynch = (?)", "N") 

的逻辑是,如果工作单状态(wostatus.id)不是一个上的值和workorder.maxsynch等于“N”

回答

3

你可以链,其中()方法:

scope :laborok, where("wostatus_id NOT IN (?)", [1]).where("maxsynch = (?)", "N") 

艳记了,我换成你的ID阵列[1]! 这里是你的ID的数组代码:

scope :laborok, where("wostatus_id NOT IN (?)", [231, 230, 8466, 8467, 232]). 
        where("maxsynch = (?)", "N") 
+0

谢谢 - 它工作的很棒! – Reddirt 2013-05-01 17:27:54

+0

我喜欢链接 - 它对我来说更具可读性。 – Reddirt 2013-05-01 17:28:57

0

什么投入字符串被简单地跟着你的数据库适配器过去了,在取代的参数值,因此,最简单的形式是只使用AND关键字:

scope :laborok, where(["wostatus_id NOT IN (?) AND maxsynch = (?)", [231, 230, 8466, 8467, 232], "N"]) 

使用多个?where串,第一个参数是传递到您的DB适配器字符串,而后续的参数代入的字符串,所提供的顺序。

链接的作品好,以及如果它在你的具体情况更具可读性:

scope :laborok, where("wostatus_id NOT IN (?)", [231, 230, 8466, 8467, 232]). 
       where("maxsynch = (?)", "N") 

许多ActiveRecord Query Interface methods可以以这种方式进行链接。

1

为什么链接?

class Workorder < ActiveRecord::Base 
    scope :laborok, lambda do |ids, maxsynch| 
     where("wostatus_id NOT IN (?) AND maxsynch = ?", ids, maxsynch) 
    end 
end