微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

原文地址:https://www.jb51.cc/ruby/270340.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐