Rails 3:如何获取 id 不在给定列表中的所有帖子?

发布于 2024-11-09 02:40:27 字数 228 浏览 1 评论 0原文

要获取 publisher_id 等于 10、16 或 17 的所有帖子,我这样做:

Post.where(:publisher_id => [10, 16, 17])

如何获取 publisher_id 不等于 的所有帖子10、16 或 17(即除这三个之外的所有可能的 ID)?

To get all posts with publisher_id equals to 10, 16, or 17, I do:

Post.where(:publisher_id => [10, 16, 17])

How would I get all posts with publisher_id not equals to 10, 16, or 17 (i.e. all possible ids besides those three) ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

淡淡離愁欲言轉身 2024-11-16 02:40:27

只需执行:

Post.where(["publisher_id NOT IN (?)", [10, 16, 17]])

Just perform a :

Post.where(["publisher_id NOT IN (?)", [10, 16, 17]])
我的鱼塘能养鲲 2024-11-16 02:40:27

在 Rails 4 中,我们可以像下面这样做,

Post.where.not(:publisher_id => [10, 16, 17])

它将生成如下 SQL

SELECT "posts".* FROM "posts"  WHERE ("posts"."publisher_id" NOT IN (10, 16, 17))

in rails 4 we can do like below

Post.where.not(:publisher_id => [10, 16, 17])

it will generate SQL like below

SELECT "posts".* FROM "posts"  WHERE ("posts"."publisher_id" NOT IN (10, 16, 17))
调妓 2024-11-16 02:40:27

未经测试,但应该像(使用metawhere gem):

Post.where( :id.not_eq => [10,16,17] )

Untested, but should be like (using metawhere gem):

Post.where( :id.not_eq => [10,16,17] )
北城挽邺 2024-11-16 02:40:27

使用 Rails 3 中撒有 Arel 的“纯”ActiveRecord 语法,您可以执行以下操作:

Post.where( Post.arel_table[:publisher_id].not_in([10, 16, 17]) )

Using "pure" ActiveRecord syntax sprinkled with Arel using Rails 3 you can do something like this:

Post.where( Post.arel_table[:publisher_id].not_in([10, 16, 17]) )
浊酒尽余欢 2024-11-16 02:40:27

我使用过的巧妙解决方案:

ids = #however you get the IDS
Post.where(["id not in (?)", [0,*ids])
  • 0 的存在意味着它始终有一个元素(假设没有 ID 为 0),
  • ID 成为 splat 意味着它始终是一个数组。

Neat solution I've used:

ids = #however you get the IDS
Post.where(["id not in (?)", [0,*ids])
  • The presence of the 0 means it always has one element in (assuming nothing has an ID of 0)
  • ID becoming a splat means it'll always be an array.
谈场末日恋爱 2024-11-16 02:40:27

此页面上的每个答案都是错误的,因为这些答案都不能处理所有数组情况,特别是只有一个元素的数组

下面是一个使用此页面上任何“所谓”解决方案都会失败的示例:

@ids = [1]
Post.where("publisher_id NOT IN (?)", @ids)
#ERROR
Post.where("publisher_id NOT IN (?)", [4])
#ERROR
#...etc

#ALSO
@ids = []
Post.where("publisher_id NOT IN (?)", @ids)
#ERROR
Post.where("publisher_id NOT IN (?)", [])
#ERROR
#...etc

#The problem here is that when the array only has one item, only that element is 
#returned, NOT an array, like we had specified

#Part of the sql that is generated looks like:
#...WHERE (publisher_id NOT IN 166)

#It should be:
#...WHERE (publisher_id NOT IN (166))

此页面上唯一实际上处于正确轨道并处理这个非常重要的情况的答案是@都铎康斯坦丁的。但问题是他实际上并没有展示一种使用他的方法来解决OP发布的真实抽象示例问题的“方式”(而不仅仅是使用硬编码的数字)。

这是我的解决方案,用于在给定要排除的 id 数组的情况下动态查找不在 Activerecord 关联中的 id,该解决方案将与 n 个元素的数组一起使用(...包括 n=1 和 n=0)

@ids = [166]
@attribute = "publisher_id"
@predicate = "NOT IN"
@ids = "(" + @ids.join(",") + ")"
if @ids == "()"
  #Empty array, just set @ids, @attribute, and @predicate to nil
  @ids = @attribute = @predicate = nil
end

#Finally, make the query
Post.where( [@attribute, @predicate, @ids].join(" ") ) 

#Part of the sql that is generated looks like:
#...WHERE (publisher_id NOT IN (166))
#CORRECT!

#If we had set @ids = []     (empty array)
#Then the if statement sets everything to nil, and then
#rails removes the blank "  " space in the where clause automatically and does
#the query as if all records should be returned, which
#logically makes sense!

Every single answer on this page is wrong because none of these answers take care of ALL array cases, Especially arrays that have only one element.

Here is an example that will FAIL using any of the 'so called' solutions on this page:

@ids = [1]
Post.where("publisher_id NOT IN (?)", @ids)
#ERROR
Post.where("publisher_id NOT IN (?)", [4])
#ERROR
#...etc

#ALSO
@ids = []
Post.where("publisher_id NOT IN (?)", @ids)
#ERROR
Post.where("publisher_id NOT IN (?)", [])
#ERROR
#...etc

#The problem here is that when the array only has one item, only that element is 
#returned, NOT an array, like we had specified

#Part of the sql that is generated looks like:
#...WHERE (publisher_id NOT IN 166)

#It should be:
#...WHERE (publisher_id NOT IN (166))

The only answer on this page that is actually on the right track and takes care of this very important case is @Tudor Constantin's. But the problem is he didn't actually show a 'way' of using his methodology to solve the real abstract example question the OP posted (not just using the hard-coded numbers).

here is my solution to dynamically find the ids not in an Activerecord association given an array of ids to exclude, that will work with an array of n elements (...including n=1 and n=0)

@ids = [166]
@attribute = "publisher_id"
@predicate = "NOT IN"
@ids = "(" + @ids.join(",") + ")"
if @ids == "()"
  #Empty array, just set @ids, @attribute, and @predicate to nil
  @ids = @attribute = @predicate = nil
end

#Finally, make the query
Post.where( [@attribute, @predicate, @ids].join(" ") ) 

#Part of the sql that is generated looks like:
#...WHERE (publisher_id NOT IN (166))
#CORRECT!

#If we had set @ids = []     (empty array)
#Then the if statement sets everything to nil, and then
#rails removes the blank "  " space in the where clause automatically and does
#the query as if all records should be returned, which
#logically makes sense!
烟酒忠诚 2024-11-16 02:40:27
Post.where(" id NOT IN ( 10, 16, 17) ")
Post.where(" id NOT IN ( 10, 16, 17) ")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文