[LRUG] ActiveRecord::Relation#as_batches - like find_each but honours order, limit, offset
Daniel Barlow
dan at telent.net
Sat May 5 09:44:53 PDT 2012
An almost[*]-a-gem announcement, and a question:
The internet confirms that I'm not the only person who's attempted to
use find_each to get only part of a result set, and been disappointed
to find that it doesn't support order, limit or offset - any or all of
which might be useful if you want to consume a large but unknown
number of records without potentially instantiating several million
ActiveRecord::Base objects at once. So I wrote something that does.
ar-as-batches : https://github.com/telent/ar-as-batches/ lets you do e.g.
User.where(country_id:
44).order(:joined_at).offset(200).as_batches do |user|
user.party_all_night!
end
and it works much as you'd expect
And now the question: to test it I create an in-memory sqlite database
and tell activerecord to log its sql queries to a Logger wrapping a
StringIO, then afterwards I match regexps on the string to see what
queries were actually sent. I cannot help feeling that there must be
a Better Way: is there, and what?
https://github.com/telent/ar-as-batches/blob/master/test/as-batches-test.rb
This is the first time I've really done anything with ActiveRecord
innards, so all suggestions welcome
-dan
[*] "almost" meaning there's a gemspec in the repo and you can include
it with a :git entry in your Gemfile
--
dan at telent.net
http://ww.telent.net
More information about the Chat
mailing list