ruby-on-rails – 在Rails3中使用多个范围参数

在我的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}"}
}

在我的控制台中我做

ErrorCode.latest_error_code(1,100)

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

multiple values for a block parameter (3 for 1)

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

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

提前致谢

解决方法

Active Record Query Interface Guide

Using a class method is the preferred way to accept arguments for scopes.

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

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

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...