This project is archived and is in readonly mode.

#2201 ✓resolved
Carlos Antonio da Silva

batches: each with named_scope doesn't seem to work

Reported by Carlos Antonio da Silva | March 10th, 2009 @ 07:38 PM

I've been testing the new find_in_batches and each methods with named_scopes, and verified that the each method actually does not seem to work correctly. While find_in_batches uses the named_scope in the right way, the each method instead is executing the query and using the each method from Enumerable.

To demonstrate it:


class Person < ActiveRecord::Base

  named_scope :all_active, :conditions => { :active => true }

  def process_something
    # Process
  end
end

# This works as expected
Person.all_active.find_in_batches do |people_batch|
  people_batch.each { |person| person.process_something  }
end

# This doesn't work
Person.all_active.each do |person|
  person.process_something
end

I guess the each method should work fine as find_in_batches, and for those who want to iterate over the array of records from named_scope call, could use something like:


Person.all_active.all.each do |person|
  person.process_something
end

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Referenced by

Pages