在Rails3中使用多个范围参数

sam*_*207 8 activerecord scope ruby-on-rails ruby-on-rails-3 rails-activerecord

在我的Rails3应用程序中,我有AR范围,需要3个参数

例如:我正在尝试在两个代码值之间获取给定模块的错误详细信息

#select * from error_codes where error_module_id=1 and code >0 and code < 100

scope :latest_error_code, lambda{ |module_id, min, max|
    {:conditions => "error_module_id=#{module_id} and code >= #{min} and code <= #{max}"}
} 
Run Code Online (Sandbox Code Playgroud)

在我的控制台中我做

ErrorCode.latest_error_code(1,0,100)
Run Code Online (Sandbox Code Playgroud)

但是当我尝试执行此操作时,我收到以下错误

multiple values for a block parameter (3 for 1)
Run Code Online (Sandbox Code Playgroud)

当我做一些护目镜时,似乎AR范围内部支持多个参数

1 - 是真的吗?(AR doent支持范围的多个参数)2 - 还有其他选择吗?3 - 我在这里做错了吗?

提前致谢

mu *_*ort 10

Active Record查询界面指南:

使用类方法是接受范围参数的首选方法.

所以你可能想要更像这样的东西:

def self.latest_error_code(module_id, min, max)
    where(
        'error_module_id = :module_id and code between :min and :max',
        :module_id => module_id, :min => min, :max => max
    )
} 
Run Code Online (Sandbox Code Playgroud)