0

为什么无论是通过浏览器还是通过控制台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`

当我在 Microsoft SQL Server 中执行此操作时,它会加载一堆记录。但是,在 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.execute("select * from orders where id = 987;"). 但是,当我这样做时,在控制台中Order.find(987)会返回一条记录。

4

1 回答 1

0

所以解决方案是改变 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)
于 2013-07-01T15:17:51.137 回答