本文介绍了我怎样才能使ActiveRecord的一个“或”声明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在ActiveRecord的3创造或sql语句,我尝试了所有种类的变体,但不能看着办吧......
I am trying to create an 'OR' sql statement in ActiveRecord 3, I tried all kinds of variations but can't figure it out...
例如我想这个查询包含多个'channel_ids,并使其返回所有文章任何通道ID的。此工程一:
For example I want this query to include multiple 'channel_ids' and have it return all posts for any of the channel IDs. This works for one:
Post.where(:user => 'mike').where(:channel_id => 0).limit(20)
但我无法弄清楚如何与倍数做到这一点,我已经试过,例如:
but I cant figure out how to do it with multiples, I've tried for example:
Post.where(:user => 'mike').where(:channel_id => ?, [0,1,2,3]).limit(20)
但没有奏效。我怎样才能使这项工作?
but it didn't work. How can I make this work?
推荐答案
使用阿雷尔的方法做这样的:
t = Post.arel_table
ids = [1,2,3]
Post.where(
t[:user].eq("mike").or(t[:channel_id].in(ids))
)
这篇关于我怎样才能使ActiveRecord的一个“或”声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!