Overriding a single Rails core file (batches.rb)

Hi everyone,

I'm trying to find a way to implement a diff I found into my
application (https://rails.lighthouseapp.com/projects/8994/tickets/
2137-allow-find_batches-to-use-order-limit-and-offset). It is
imperative that I use find_in_batches as well as passing an :order
option to the function. Certain rows need to be processed before
others, and because of the size of the table I can't do them all at
once.

Now, my question is, how could I do this with the least amount of
fuss? I don't want to edit the batches.rb in my Rails installation, as
I then would have to do the same on servers and other workstations. I
also don't particularly want to freeze Rails. I've tried just slapping
a new batches.rb into /vendor/rails/activerecord/lib/active_record/,
hoping that that would override just that file, but it doesn't seem to
work.

Any help would be appreciated!

Best regards,
Sebastian

Sebastian,

Since it's an ActiveRecord module that gets included into AR::Base,
you either need to:

1) (Sneaky) Load your own Batches module when AR wants to autoload it
(active_record.rb:51) - override Module#autoload to 'listen' for the
Batches module, and substitute your version (located in /lib or
something) instead. You'll have to do this before Rails loads AR,
though, so put it in /config/preinitializer.rb.

2) (More sane) Monkey-patch the changed methods into AR::B through a
file you include in /lib, /config/initializers or through your own
plugin. You would probably be overriding #find_in_batches,
#sort_batch_rows! and #select_batch_ids.

3) Freeze rails, replace the file. This is the simplest, and least
error-prone, though upgrading and patching Rails may be trickier.

Colin

Colin,

Thanks!

I will most likely go with option 2. Can I just copy-paste the
modified methods as per the diff into any of those directories
(obviously with "module ActiveRecord" and "module Batches" at the
top)? Do I need to adhere to a specific naming scheme for the file?

Apologies for all the questions. I'm still (re-)learning Rails.

Best regards,
Sebastian

Nevermind, figured it out.

Thanks again!

- Sebastian