This project is archived and is in readonly mode.

#2709 ✓invalid

find_each doesn't respect default_scope

Reported by trevor | May 25th, 2009 @ 04:51 AM | in 2.x

default_scope doesn't have any influence on find_each.

example in model -

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

this works

Person.all.each do |p|

this is out of order

Person.find_each do |p|

Comments and changes to this ticket

  • Daniel Guettler

    Daniel Guettler May 25th, 2009 @ 04:58 PM

    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.
  • CancelProfileIsBroken

    CancelProfileIsBroken May 26th, 2009 @ 01:47 PM

    • 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.

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=""></a>

People watching this ticket