Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.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 Rails2+MicrosoftSQLServer:ActiveRecord返回-1但SQLServer返回记录?_Ruby On Rails - Fatal编程技术网

Ruby on rails Rails2+MicrosoftSQLServer:ActiveRecord返回-1但SQLServer返回记录?

Ruby on rails Rails2+MicrosoftSQLServer:ActiveRecord返回-1但SQLServer返回记录?,ruby-on-rails,Ruby On Rails,为什么ActiveRecord总是通过浏览器或控制台返回-1 SQL语句: SELECT category.categoryname, sum(lineitems.qty) as totalSales, sum(lineitems.qty*size.sizeship) as volume, sum(lineitems.qty*lineitems.purcprice) AS totmerchandise FROM category, products, orders, shipmen

为什么ActiveRecord总是通过浏览器或控制台返回-1

SQL语句:

SELECT category.categoryname,  sum(lineitems.qty) as totalSales,  
sum(lineitems.qty*size.sizeship) as volume,  
sum(lineitems.qty*lineitems.purcprice) AS totmerchandise  FROM category, products, 
orders, shipments, lineitems, size  WHERE 
category.categoryid = products.categoryid  AND products.productid = 
lineitems.productid  AND lineitems.posshipid = shipments.posshipid  AND 
shipments.posorderid = orders.posorderid  AND size.sizeid = products.size  AND 
category.categoryid NOT IN (77,79)  AND orders.orderstatus in 
(1,4,5)  AND orders.ordercomplete = 1  AND numbotincase > 0  AND orders.date >= 
'20130501'  AND orders.date < '20130531'  GROUP BY 
category.categoryname  ORDER BY category.categoryname`
当我在MicrosoftSQLServer中执行此操作时,它会加载一组记录。 但是,在Rails中,当我尝试这样做时:

query_for_category_bottles_volume_totalsales = "SELECT category.categoryname,  
sum(lineitems.qty) as totalSales,  sum(lineitems.qty*size.sizeship) as volume,  
sum(lineitems.qty*lineitems.purcprice) AS totmerchandise  FROM category, products, 
orders, shipments, lineitems, size  WHERE category.categoryid = products.categoryid  
AND products.productid = lineitems.productid  AND lineitems.posshipid = 
shipments.posshipid  AND shipments.posorderid = orders.posorderid  AND 
size.sizeid = products.size  AND category.categoryid NOT IN (77,79)  AND 
orders.orderstatus in (1,4,5)  AND orders.ordercomplete = 1  AND numbotincase > 0  
AND orders.date >= '20130501'  AND orders.date < '20130531'  GROUP BY 
category.categoryname  ORDER BY category.categoryname"

category_bottles_volume_totalsales = ActiveRecord::Base.connection.execute(query_for_category_bottles_volume_totalsales)

这种情况不仅发生在这个查询中,而且也发生在一些简单的查询中:ActiveRecord::Base.connection.executeselect*from orders,其中id=987;。但是,在控制台中,当我执行Order.find987时,它会返回一条记录

因此,解决方案是改变ActiveRecord进行查询的方式

  database = ActiveRecord::Base.connection

  query_for_category_bottle_casecost_case_price = "SELECT ..."

  category_bottle_casecost_case_price = database.select_all(query_for_category_bottle_casecost_case_price)

类别\瓶子\数量\总销售额是否设置为-1?不,该变量是在该行上创建的。请在末尾添加您的问题