如何指定在rails3 AR查询界面在多个值 [英] How to specify multiple values in where with AR query interface in rails3

查看:114
本文介绍了如何指定在rails3 AR查询界面在多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每对Active Record的查询接口导轨导向的第2.2节这里

Per section 2.2 of rails guide on Active Record query interface here:

这似乎表明,我可以通过一个字符串指定条件(S),值则数组,应该在某个时候被替换,而AREL正在修建。所以,我有一份声明会产生我的条件字符串,它可以是一个不同数量的属性与AND或它们之间或链接在一起,而我传递一个数组作为第二ARG到那里的方法,我也得到:

which seems to indicate that I can pass a string specifying the condition(s), then an array of values that should be substituted at some point while the arel is being built. So I've got a statement that generates my conditions string, which can be a varying number of attributes chained together with either AND or OR between them, and I pass in an array as the second arg to the where method, and I get:

的ActiveRecord :: preparedStatementInvalid:错号码绑定变量(1 5)

ActiveRecord::PreparedStatementInvalid: wrong number of bind variables (1 for 5)

这使我相信我这样做不正确。但是,我没有找到如何正确地做任何事情。要重申这个问题的另一种方式,我需要传递一个字符串,其中的方法,如table.attribute的=?AND table.attribute1 =?OR table.attribute1 =?有数目不详的这些条件AND或OR起来,再通过一些东西,我认为将是一个数组,将用于替代在第一个参数条件的字符串值,第二个参数。这是正确的做法,或者,我只是缺少一些其他巨大的概念地方,我来这一切错了吗?我倒是觉得,不知怎的,这已成为可能,总之只是产生一个原始的SQL字符串。

which leads me to believe I'm doing this incorrectly. However, I'm not finding anything on how to do it correctly. To restate the problem another way, I need to pass in a string to the where method such as "table.attribute = ? AND table.attribute1 = ? OR table.attribute1 = ?" with an unknown number of these conditions anded or ored together, and then pass something, what I thought would be an array as the second argument that would be used to substitute the values in the first argument conditions string. Is this the correct approach, or, I'm just missing some other huge concept somewhere and I'm coming at this all wrong? I'd think that somehow, this has to be possible, short of just generating a raw sql string.

推荐答案

听起来你正在做的事情是这样的:

Sounds like you're doing something like this:

Model.where("attribute = ? OR attribute2 = ?", [value, value])

而你需要做的:

Whereas you need to do this:

# notice the lack of an array as the last argument
Model.where("attribute = ? OR attribute2 = ?", value, value)

看一看<一href="http://guides.rubyonrails.org/active_record_querying.html#array-conditions">http://guides.rubyonrails.org/active_record_querying.html#array-conditions关于如何工作的更多细节。

Have a look at http://guides.rubyonrails.org/active_record_querying.html#array-conditions for more details on how this works.

这篇关于如何指定在rails3 AR查询界面在多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆