Rails —— 查找到处都有 id 的条件
我从我的模型中提取了这个查找条件,目前看起来像这样。
@major_set = Interest.find(:all, :conditions => {:id => 1..21})
我想添加一些我刚刚添加的更多个人 ID,例如 120...130。我尝试这样做...
@major_set = Interest.find(:all, :conditions => {:id => 1..21, 120..130})
但收到错误。 “语法错误,意外的'}',期待 tASSOC ...ns => {:id => 1..21, 122..130})”
我如何选择多组 id 以及某些单独的 id ids,即 ( :conditions => {:id => 1..21, 121..140, 144, 155 }??
I have this find condition pulling from my model that currently looks like this.
@major_set = Interest.find(:all, :conditions => {:id => 1..21})
I'd like to add some more individual ids that I've just added which are like 120...130. I tried to do...
@major_set = Interest.find(:all, :conditions => {:id => 1..21, 120..130})
but got the error. " syntax error, unexpected '}', expecting tASSOC ...ns => {:id => 1..21, 122..130})"
How can I select multiple sets of id's as well as maybe some individual ids, ie ( :conditions => {:id => 1..21, 121..140, 144, 155 }??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将范围转换为数组并将它们添加在一起。例如,
如果您想添加单独的 id,那么您只需将它们添加到数组中即可。例如
You can convert the ranges to arrays and add them together. E.g.
If you then want to add in individual ids then you can just add them to the array. E.g.
如果您输入
{:id =>; 1..21, 122..130}
在 irb 中你会得到一个错误,因为它不是有效的 ruby 语法。这被解释为哈希,其中第一个元素是:id => 1..21
,第二个丢失了它的密钥。为了使其成为有效的 ruby 表达式,您需要输入:
但我认为 ActiveRecord 不会接受该语法。所以你可能需要:
这适用于 MySQL。我不知道它是否适用于其他数据库。
If you type
{:id => 1..21, 122..130}
inside irb you will get an error because it is not valid ruby syntax. This is interpreted as a hash where the first element is:id => 1..21
and the second is missing its key.In order to make it a valid ruby expression, you would need to type:
But I don't think that ActiveRecord will accept that syntax. So you may need to:
This works in MySQL. I don't know if it will work in other databases.