所以我有一個SQL語句,我檢索記錄:ActiveRecord :: Base.connection.execute + CSV:如何遍歷返回的列?
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
?