如何PSS一个前$ P $不符合的ActiveRecord / Rails的查询?不符合、PSS、Rails、ActiveRecord

2023-09-08 15:39:58 作者:久愛不厭@

只是为了更新这个,因为它似乎有很多人来此,如果您使用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>)
 
Rails Active Record Associations 笔记 一

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

  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]