2013-07-01 100 views
0

为什么ActiveRecord总是返回-1无论是通过浏览器还是通过控制台?Rails 2 + Microsoft SQL Server:ActiveRecord返回-1,但SQL Server返回记录?

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)时,它会返回一条记录。

+0

is category_bottles_volume_totalsales set to -1? – usha

+0

nope,该变量在该行上创建 – Edmund

+0

请在最后添加您的问题 – usha

回答

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)