Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

There was a problem

You must be a member of this account.

This project is archived and is in readonly mode.

batches: each with named_scope doesn't seem to work

#2201

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

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

State: resolved
Milestone: none
Assigned to: Pratik Pratik
Importance: Low

Activity

  1. Carlos Antonio da Silva
    Carlos Antonio da Silva
    • Assigned user set to DHH

    I've made a little test adding the each method to the NON_DELEGATE_METHODS inside Scope class, so the method won't be delegated to the Array and will correctly be handled by the new each method in batches.

    I think this way should work, and like I said we still have the all.each method use when needed.

    March 10th, 2009 @ 11:01 PM

  2. Pratik
    Pratik
    • Assigned user changed from DHH to Pratik
    • Milestone cleared.

    March 11th, 2009 @ 12:34 AM

  3. Pratik
  4. Repository
    Repository
    • State changed from new to resolved

    (from [0b6f514947c38bf7af2b7e98905a49a94832e5f6]) Add NamedScope#find_each tests [#2201 batches: each with named_scope doesn't seem to work state:resolved] http://github.com/rails/rails/co...

    March 11th, 2009 @ 03:10 PM

  5. Carlos Antonio da Silva
    Carlos Antonio da Silva

    I've created the patch using git diff and not git format-patch.. sorry..

    I really liked changing method name from each to find_each. This makes things cleaner.

    March 11th, 2009 @ 04:41 PM

  6. Jeff Kreeftmeijer
    Jeff Kreeftmeijer
    • Tag cleared.
    • Importance changed from to Low

    Automatic cleanup of spam.

    November 7th, 2010 @ 04:54 PM