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

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

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

哪个是一样的

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

我想知道是否有一种方法可以不这样做,比如:

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

当前回答

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

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

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

其他回答

上面的大多数答案应该足以满足您的需求,但如果您正在进行更多此类谓词和复杂组合,请查看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)}

你可以尝试这样做:

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)

要扩展@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让它变得更简单!

当你查询一个空数组时,在where块中添加"<< 0"到数组中,这样它就不会返回"NULL"并中断查询。

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

If actions可以是空数组或空数组。

你可能想看看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查询更好。