Tho*_*aum 5 activerecord ruby-on-rails
我试图在ActiveRecord 3中创建一个'OR'sql语句,我尝试了各种各样的变化,但无法弄清楚...
例如,我希望此查询包含多个"channel_ids",并让它返回任何频道ID的所有帖子.这适用于一个:
Post.where(:user => 'mike').where(:channel_id => 0).limit(20)
Run Code Online (Sandbox Code Playgroud)
但我无法弄清楚如何用倍数来做,我试过例如:
Post.where(:user => 'mike').where(:channel_id => ?, [0,1,2,3]).limit(20)
Run Code Online (Sandbox Code Playgroud)
但它不起作用.我怎样才能做到这一点?
试试这个:
Post.where("posts.user = ? OR posts.channel_id IN (?)", "mike", ids)
Run Code Online (Sandbox Code Playgroud)
使用Arel方法来执行此操作:
t = Post.arel_table
ids = [1,2,3]
Post.where(
t[:user].eq("mike").or(t[:channel_id].in(ids))
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2817 次 |
| 最近记录: |