Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 3 这是最好的方法吗;不在「;在ActiveRecord3.1中?_Ruby On Rails 3_Activerecord - Fatal编程技术网

Ruby on rails 3 这是最好的方法吗;不在「;在ActiveRecord3.1中?

Ruby on rails 3 这是最好的方法吗;不在「;在ActiveRecord3.1中?,ruby-on-rails-3,activerecord,Ruby On Rails 3,Activerecord,我真的很喜欢能够: Product.where(:id => [40,41,42]) 它在(40,41,42)中生成一个nicewhere id 然而,我只能想办法做相反的事情: Product.where("id not in (?)", [40,41,42]) 有没有更干净的方法 谢谢。不,你就是这样做的。没有比这更简单的方法了,至少是香草槟榔。您可能会在其中找到一些可以做到这一点的方法。您可以这样做: Product.all.delete_if{|x| [41,42,43].in

我真的很喜欢能够:

Product.where(:id => [40,41,42])
它在(40,41,42)中生成一个nice
where id

然而,我只能想办法做相反的事情:

Product.where("id not in (?)", [40,41,42])
有没有更干净的方法


谢谢。

不,你就是这样做的。没有比这更简单的方法了,至少是香草槟榔。您可能会在其中找到一些可以做到这一点的方法。

您可以这样做:

Product.all.delete_if{|x| [41,42,43].include? x.id }

==>包含所有产品的阵列,不包括具有给定ID的产品。

我就是这么想的。我可能会看看我是否能为咧嘴笑和咯咯笑而修补一些东西。:-)感谢+1提供了一个简洁的替代方案,但我实际上认为
不在(?)
中更干净。此解决方案将使服务器崩溃。当
products
表有1000行时,在应用
delete\u if
之前,所有行都会加载到内存中。应该避免在客户端过滤数据库行。