activerecord GROUPed BY in batches

We are all familiar with activerecord .find_in_batches. The main idea behind it is not allowing to load thousands of ActiveRecord objects in memory which would cause tear down the server. I looked at this method and I looked at `.group` which applies GROUP BY clause. Just for fun, I thought what if I receive these “grouped_by” data in batches, something like

 scope.grouped_by_in_batches(group_by_column) do |batch, value|
  batch is an array same as we get in case of `find_in_batches`
  value is the grouped value of the column
 end

Hence I worked on this idea, made a pull request which obliviously did not work, as this was not a common use case, thats all fine. I agree to it. Even I never got to use it till date. However in attempt to do so, I really enjoyed writing test cases for it, getting interacted with a couple of ruby/rails expertise. Feeling happy about it.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s