In ruby ββon rails, is it possible to perform a sum query with a group using the find_each batch-batch?
I am loading data from my database and I am doing a sum calculation using a group.
ElectricityReading.sum(:electricity_value, :group => "electricity_timestamp", :having => ["electricity_timestamp = '2010-02-14 23:30:00'"])
My datasets are extremely large, 100k up, so I was wondering if find_each could be used to batch update to help with memory overhead.
I can write the usage limitation and dispensing offsets manually, but I would like to avoid this if the code already exists.
+2
a source to share
1 answer
From http://railsforum.com/viewtopic.php?pid=88198#p88198
@categories = Categories.find(:all, :joins => :animals,
:select => "categories.*, SUM(animals.weight) as weight_sum",
:group => "categories.id")
# ATTENTION: weight_sum is now a temporary attribute of the categories returned!
# and the animals are NOT eager-loaded`
<% @categories.each do |c| %>
Category: <%= c.name %><br />
Sum of Weight in this category: <%= c.weight_sum %><br />
<% end %>
It's not ActiveRecord.sum, but it should do the trick.
0
a source to share