Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.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 非组合表示法_Ruby On Rails_Ruby_Arel_Squeel - Fatal编程技术网

Ruby on rails 非组合表示法

Ruby on rails 非组合表示法,ruby-on-rails,ruby,arel,squeel,Ruby On Rails,Ruby,Arel,Squeel,我想使用squel来构建如下查询: SELECT * from `table` WHERE (`field1`, `field2`) NOT IN ( (1,"value_a"), (2,"value_b"), (3,"value_a"), ... ) 我想知道是否有任何方法可以通过IN或NOT IN语句中的将多个字段与数组进行比较 类似这样的东西(我知道示例不起作用)将是表达我的意思的好方法: array = [[1,"value_a"], [2,"value_b"], [3

我想使用squel来构建如下查询:

SELECT * from `table`
WHERE (`field1`, `field2`)
NOT IN (
  (1,"value_a"),
  (2,"value_b"),
  (3,"value_a"),
  ...
)
我想知道是否有任何方法可以通过
IN
NOT IN
语句中的
将多个字段与数组进行比较

类似这样的东西(我知道示例不起作用)将是表达我的意思的好方法:

array = [[1,"value_a"], [2,"value_b"], [3, "value_a"]]
Table.where{ (field1 & filed2).not_in array }
这样的事情可能吗

更新 我知道如何使用多个
…&(a!=b)&(c!=d)&…
,但这不是我问的问题。

也许是这样

Product.where{
  (title != 'foo') & (description != 'bar') |
  (title != 'baz') & (description != 'xyz')
}
更新 如果已经有一个包含要排除的值的数组,则可以创建如下范围:

scope :without_title_and_description, lambda{|array| 
  where{array.map{|c1,c2| (title != c1) & (description != c2)}.inject(:|)} 
}
并使用它:

array = [[1,"value_a"], [2,"value_b"], [3, "value_a"]]
Product.without_title_and_description(array)
产生:

  Product Load (1.0ms)  SELECT "products".* FROM "products" WHERE (((("products"
."title" != 1 AND "products"."description" != 'value_a') OR ("products"."title"
!= 2 AND "products"."description" != 'value_b')) OR ("products"."title" != 3 AND
 "products"."description" != 'value_a')))

主要的问题是,虽然Arel(以及它的squel)支持一些相当复杂的查询,但这一级别的查询复杂性并没有被涵盖。考虑到这一点,除了使用此功能扩展Arel本身之外,返回SQL并手动构造所需的查询是唯一的解决方案

existing_compound_ids = [[1,"value_a"],[2,"value_b"],[3, "value_a"]]

compound_collection = existing_compound_ids.inject([]) do |aftermath, member|
  aftermath << "('#{ member[0] }','#{ member[1] }')"
end.join(",")

Table.where("(`tables`.`field1`, `tables`.field2) NOT IN (#{compound_collection})")

我希望看到更好或更优雅的解决方案,但我还没有找到。

没有,因为这将调用:
选择产品。*从产品中(((products.title!=“foo”和products.description!=“bar”)或(products.title!=“baz”和products.description!=“xyz”)
关闭时(在获得正确结果时)这不是我想要达到的。@Krule Yeap,这不是相同的方法,但它给出了相同的结果。如果您有一个带有值的数组,并且希望使用它,请查看我的更新。感谢您的努力,但是,结果虽然重要,但从来都不是问题。我正在尝试优化一个大数据集上的查询,这个解决方案就是我已经拥有的。
SELECT `tables`.* from `tables` WHERE ((field1, field2)
NOT IN (('1','value_a'),('2','value_b'),('3','value_a')))