# Overriding a single Rails core file (batches.rb)

**URL:** <https://www.ruby-forum.com/t/overriding-a-single-rails-core-file-batches-rb/176127>\
**Category:** Rails\
**Created:** [September 25, 2009, 1:49am UTC](https://www.ruby-forum.com/t/overriding-a-single-rails-core-file-batches-rb/176127 "2009-09-25T01:49:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sebastian\_von\_Conra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@Sebastian\_von\_Conra](https://www.ruby-forum.com/u/Sebastian_von_Conra)\
**Post date:** [September 25, 2009, 1:49am UTC](https://www.ruby-forum.com/t/overriding-a-single-rails-core-file-batches-rb/176127/1 "2009-09-25T01:49:36Z")

</div>

Hi everyone,

I’m trying to find a way to implement a diff I found into my  
application ([Tickets - Ruby on Rails - rails](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

---

<div class="post-metadata">

**Author:** ![Sebastian\_von\_Conra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@Sebastian\_von\_Conra](https://www.ruby-forum.com/u/Sebastian_von_Conra)\
**Post date:** [September 25, 2009, 3:14am UTC](https://www.ruby-forum.com/t/overriding-a-single-rails-core-file-batches-rb/176127/2 "2009-09-25T03:14:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Sebastian\_von\_Conra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@Sebastian\_von\_Conra](https://www.ruby-forum.com/u/Sebastian_von_Conra)\
**Post date:** [September 25, 2009, 3:07am UTC](https://www.ruby-forum.com/t/overriding-a-single-rails-core-file-batches-rb/176127/3 "2009-09-25T03:07:28Z")

</div>

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

On Thu, Sep 24, 2009 at 4:47 PM, Sebastian von Conrad

---

<div class="post-metadata">

**Author:** ![Sebastian\_von\_Conra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@Sebastian\_von\_Conra](https://www.ruby-forum.com/u/Sebastian_von_Conra)\
**Post date:** [September 25, 2009, 4:03am UTC](https://www.ruby-forum.com/t/overriding-a-single-rails-core-file-batches-rb/176127/4 "2009-09-25T04:03:04Z")

</div>

Nevermind, figured it out.

Thanks again!

- Sebastian

On Sep 25, 10:12 am, Sebastian von Conrad
