225

我希望有一个不涉及的简单解决方案find_by_sql,如果没有,那么我想这将不得不工作。

我发现这篇文章引用了这个:

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

这与

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

我想知道是否有办法解决NOT IN这个问题,例如:

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

16 回答 16

340

导轨 4+:

Article.where.not(title: ['Rails 3', 'Rails 5']) 

导轨 3:

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

actions数组在哪里:[1,2,3,4,5]

于 2011-07-25T13:51:36.197 回答
154

仅供参考,在 Rails 4 中,您可以使用not语法:

Article.where.not(title: ['Rails 3', 'Rails 5'])
于 2013-08-16T00:21:38.720 回答
50

您可以尝试以下方法:

Topic.find(:all, :conditions => ['forum_id not in (?)', @forums.map(&:id)])

你可能需要做@forums.map(&:id).join(','). 如果它是可枚举的,我不记得 Rails 是否会将参数放入 CSV 列表中。

你也可以这样做:

# in topic.rb
named_scope :not_in_forums, lambda { |forums| { :conditions => ['forum_id not in (?)', forums.select(&:id).join(',')] }

# in your controller 
Topic.not_in_forums(@forums)
于 2010-11-29T19:51:51.993 回答
50

使用阿雷尔:

topics=Topic.arel_table
Topic.where(topics[:forum_id].not_in(@forum_ids))

或者,如果愿意:

topics=Topic.arel_table
Topic.where(topics[:forum_id].in(@forum_ids).not)

并且由于rails 4在:

topics=Topic.arel_table
Topic.where.not(topics[:forum_id].in(@forum_ids))

请注意,最终您不希望 forum_ids 成为 ids 列表,而是一个子查询,如果是这样,那么您应该在获取主题之前执行以下操作:

@forum_ids = Forum.where(/*whatever conditions are desirable*/).select(:id)

通过这种方式,您可以在单个查询中获得所有内容:例如:

select * from topic 
where forum_id in (select id 
                   from forum 
                   where /*whatever conditions are desirable*/)

另请注意,最终您不想这样做,而是加入 - 可能更有效。

于 2011-04-14T17:02:18.517 回答
22

要扩展@Trung Lê 答案,在 Rails 4 中,您可以执行以下操作:

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

你可以更进一步。如果您需要首先过滤仅已发布的主题,然后过滤掉您不想要的 id,您可以这样做:

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

Rails 4 让它变得如此简单!

于 2014-01-22T16:08:29.177 回答
12

如果@forums为空,则接受的解决方案失败。要解决这个问题,我必须这样做

Topic.find(:all, :conditions => ['forum_id not in (?)', (@forums.empty? ? '' : @forums.map(&:id))])

或者,如果使用 Rails 3+:

Topic.where( 'forum_id not in (?)', (@forums.empty? ? '' : @forums.map(&:id)) ).all
于 2011-10-05T19:46:21.737 回答
4

上面的大多数答案应该足以满足您的需求,但如果您正在做更多此类谓词和复杂组合,请查看Squeel。您将能够执行以下操作:

Topic.where{{forum_id.not_in => @forums.map(&:id)}}
Topic.where{forum_id.not_in @forums.map(&:id)} 
Topic.where{forum_id << @forums.map(&:id)}
于 2011-09-14T11:01:44.137 回答
2

你可能想看看 Ernie Miller 的meta_where 插件。您的 SQL 语句:

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

...可以这样表达:

Topic.where(:forum_id.nin => @forum_ids)

Railscasts 的 Ryan Bates 创建了一个很好的截屏视频来解释 MetaWhere

不确定这是否是您要查找的内容,但在我看来,它肯定比嵌入式 SQL 查询更好。

于 2011-04-18T18:04:26.573 回答
2

原始帖子特别提到了使用数字 ID,但我来这里是为了寻找用字符串数组执行 NOT IN 的语法。

ActiveRecord 也会很好地为您处理:

Thing.where(['state NOT IN (?)', %w{state1 state2}])
于 2013-06-20T11:42:41.613 回答
1

这些论坛 id 能否以务实的方式制定?例如,您能以某种方式找到这些论坛吗?如果是这种情况,您应该做类似的事情

Topic.all(:joins => "left join forums on (forums.id = topics.forum_id and some_condition)", :conditions => "forums.id is null")

这将比执行 SQL 更有效not in

于 2010-11-29T20:24:18.477 回答
1

这种方式优化了可读性,但在数据库查询方面效率不高:

# Retrieve all topics, then use array subtraction to
# find the ones not in our list
Topic.all - @forums.map(&:id)
于 2012-01-28T07:31:20.380 回答
0

您可以在您的条件下使用 sql:

Topic.find(:all, :conditions => [ "forum_id NOT IN (?)", @forums.map(&:id)])
于 2010-11-29T19:58:26.007 回答
0

搭载 jonnii:

Topic.find(:all, :conditions => ['forum_id not in (?)', @forums.pluck(:id)])

使用 pluck 而不是映射元素

通过railsconf 2012 发现 10 件你不知道 rails 可以做的事情

于 2012-10-15T16:37:50.570 回答
0

当您查询一个空白数组时,将“<< 0”添加到 where 块中的数组中,这样它就不会返回“NULL”并中断查询。

Topic.where('id not in (?)',actions << 0)

如果操作可以是空数组或空白数组。

于 2013-11-29T19:45:17.547 回答
0

这是一个更复杂的“不在”查询,在使用 squeel 的 rails 4 中使用子查询。与等效的 sql 相比,当然非常慢,但是,嘿,它可以工作。

    scope :translations_not_in_english, ->(calmapp_version_id, language_iso_code){
      join_to_cavs_tls_arr(calmapp_version_id).
      joins_to_tl_arr.
      where{ tl1.iso_code == 'en' }.
      where{ cavtl1.calmapp_version_id == my{calmapp_version_id}}.
      where{ dot_key_code << (Translation.
        join_to_cavs_tls_arr(calmapp_version_id).
        joins_to_tl_arr.    
        where{ tl1.iso_code == my{language_iso_code} }.
        select{ "dot_key_code" }.all)}
    }

作用域中的前 2 个方法是声明别名 cavtl1 和 tl1 的其他作用域。<< 是 squeel 中的 not in 运算符。

希望这可以帮助某人。

于 2015-08-10T04:09:42.537 回答
0

如果有人想使用两个或更多条件,您可以这样做:

your_array = [1,2,3,4]
your_string = "SOMETHING"

YourModel.where('variable1 NOT IN (?) AND variable2=(?)',Array.wrap(your_array),your_string)

于 2020-11-11T15:12:30.793 回答