0

I was trying to add my custom implementation for ActiveRecordss find_in_batches method. First, I tried to monkeypatch the corresponding module:

module ActiveRecord
  module Batches
    def find_in_batches2
    end
  end
end

Task.find_in_batches2 do |group|
end

But ruby said:

NoMethodError: undefined method `find_in_batches2' for Task (call 'Task.connection' to establish a connection):Class
/home/yuri/.gem/ruby/2.1.5/gems/activerecord-4.2.0/lib/active_record/dynamic_matchers.rb:26:in `method_missing'
/srv/http/tm/Rakefile:15:in `<top (required)>'
/home/yuri/.gem/ruby/2.1.5/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
/home/yuri/.gem/ruby/2.1.5/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `block in load'
/home/yuri/.gem/ruby/2.1.5/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency'
/home/yuri/.gem/ruby/2.1.5/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
/home/yuri/.gem/ruby/2.1.5/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
/home/yuri/.gem/ruby/2.1.5/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `block in load'
/home/yuri/.gem/ruby/2.1.5/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency'
/home/yuri/.gem/ruby/2.1.5/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
-e:1:in `<main>'

Then I tried following this answer, to no avail. What am I doing wrong and why is it so hard?

Community
  • 1
  • 1
x-yuri
  • 16,722
  • 15
  • 114
  • 161
  • You have not described your intentions or any logic to follow. I have found altering ActiveRecord to be a daunting task at times but without any concept of what you are intending to do we cannot help very much – engineersmnky Feb 20 '15 at 15:16
  • @engineersmnky I want to iterate over records in batches in descending order if that's what you'd like to know. – x-yuri Feb 20 '15 at 15:29

1 Answers1

1

Here is the solution:

ActiveRecord::Batches.module_eval do
  def find_in_batches2
  end
end

ActiveRecord::Querying.module_eval do
  delegate :find_in_batches2, :to => :all
end

Don't forget to implement find_in_batches2.

intale
  • 831
  • 5
  • 7
  • Can you elaborate on what's wrong with my solution, and what exactly does your code, why is it as it is? – x-yuri Feb 25 '15 at 06:25