所以我有一个检索记录的 SQL 语句:
def total_sales_category_csv
query1 = "SELECT categoryname, casecost, product_name..."
result1 = ActiveRecord::Base.connection.execute(query1)
query2 = "SELECT categoryname, foo, bar..."
result2 = ActiveRecord::Base.connection.execute(query2)
result3 = "SELECT SUM(inv_figures1.available) ..."
bottle_sum = ActiveRecord::Base.connection.execute(query_for_bottle_sum)
report = FasterCSV.generate do |csv|
csv << %w{Category casecost product_name Total Sales Avg}
#I'm stuck here
end
end
所以在我被卡住的部分,我如何ActiveRecord::Base.connection.execute
以面向对象的方式遍历我返回的记录?我可以做这样的事情:
report = FasterCSV.generate do |csv|
csv << %w{Category casecost product_name Total Sales Avg}
result1.each do |row|
csv << [result1.categoryname, result2[0].foo]
end
end
?