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.

This project is archived and is in readonly mode.

find_each doesn't respect default_scope

#2709

default_scope doesn't have any influence on find_each.

example in model -

class Person < ActiveRecord::Base
  default_scope :order => 'name'
end

this works

Person.all.each do |p|
...
end

this is out of order

Person.find_each do |p|
...
end

Reported by trevor · May 25th, 2009 @ 04:51 AM

State: invalid
Milestone: 2.x
Assigned to: nobody
Importance: none

Activity

  1. Daniel Guettler
    Daniel Guettler

    find_each is calling find_in_batches which doesn't support any order option but uses table_name.primary_key for the order statement.
    From the doc:

          # It's not possible to set the order. That is automatically set to
          # ascending on the primary key ("id ASC") to make the batch ordering
          # work. This also mean that this method only works with integer-based
          # primary keys. You can't set the limit either, that's used to control
          # the the batch sizes.
    

    May 25th, 2009 @ 04:58 PM

  2. CancelProfileIsBroken
    CancelProfileIsBroken
    • State changed from new to invalid

    find_each is a special method for blasting through a table in PK order, that's all. It doesn't partake of the default scope at all.

    May 26th, 2009 @ 01:47 PM