如何PSS一个前$ P $不符合的ActiveRecord / Rails的查询? [英] How to express a NOT IN query with ActiveRecord/Rails?

查看:221
本文介绍了如何PSS一个前$ P $不符合的ActiveRecord / Rails的查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是为了更新这个,因为它似乎有很多人来此,如果您使用Rails 4看看答案由忠Lê`和VinniVidiVicci。

  Topic.where.not(forum_id:@ forums.map(安培;:ID))

Topic.where(公布:真).where.not(forum_id:@ forums.map(安培;:ID))
 

我希望有一个简单的解决方案,不涉及的find_by_sql ,如果没有的话我估计,将有工作。

我发现这篇文章它引用这样的:

  Topic.find(:所有,:条件=> {:forum_id => @ forums.map(安培;:ID)})
 

这是相同的

  SELECT * FROM主题WHERE forum_id IN(小于@forum IDS>)
 

我想知道是否有一种方法做。在与,如:

  SELECT * FROM主题WHERE forum_id NOT IN(小于@forum IDS>)
 

解决方案

我使用的是这样的:

  Topic.where('身份证NOT IN(?)',行动)
 

其中,动作是一个包含有: [1,2,3,4,5]

Just to update this since it seems a lot of people come to this, if you are using Rails 4 look at the answers by Trung Lê` and VinniVidiVicci.

Topic.where.not(forum_id:@forums.map(&:id))

Topic.where(published:true).where.not(forum_id:@forums.map(&:id))

I'm hoping there is a easy solution that doesn't involve find_by_sql, if not then I guess that will have to work.

I found this article which references this:

Topic.find(:all, :conditions => { :forum_id => @forums.map(&:id) })

which is the same as

SELECT * FROM topics WHERE forum_id IN (<@forum ids>)

I am wondering if there is a way to do NOT IN with that, like:

SELECT * FROM topics WHERE forum_id NOT IN (<@forum ids>)

解决方案

I'm using this:

Topic.where('id NOT IN (?)',actions)

Where actions is an array with: [1,2,3,4,5]

这篇关于如何PSS一个前$ P $不符合的ActiveRecord / Rails的查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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