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 ignores limit

#4123

The following code iterates over the entire table, ignoring the limit scope:

klass.limit(3).find_each {|obj| puts obj.to_s}

In ActiveRecord 2.3, one could iterate over the first 3000 of 10000 records without loading all 3000 at once. In ActiveRecord 3.0, this is not possible.

find_each works with all other scope methods, so silently discarding limit scopes is inconsistent behavior. I suggest one of two options:

  1. find_each should ignore limits but issue a warning;
  2. find_each should behave as under ActiveRecord 2.3 and use the limit.

Reported by wtn · March 7th, 2010 @ 10:45 AM

State: resolved
Milestone: 3.0.2
Assigned to: Emilio Tagua Emilio Tagua
Importance: High

Activity

  1. José Valim
    José Valim

    If you want to iterate just over three objects, you can simply do klass.limit(3).all.each {}, or am I missing something?

    If you want to iterate in batches of 3 though, you need to given :batch_size as option.

    March 27th, 2010 @ 02:16 PM

  2. wtn
    wtn
    • Tag changed from activerecord 3.0 to activerecord 3.0, 3.0.0.beta, activerecord, find_each, limit, scope

    In ActiveRecord 2.3, one could iterate over the first 3000 of 10000 records without loading all 3000 at once. In ActiveRecord 3.0, this is not possible.

    find_each works with all other scope methods, so silently discarding limit scopes is inconsistent behavior. I suggest one of two options:

    1. find_each should ignore limits but issue a warning;
    2. find_each should behave as under ActiveRecord 2.3 and use the limit.

    March 27th, 2010 @ 11:48 PM

  3. José Valim
    José Valim
    • Assigned user set to Emilio Tagua
    • Milestone cleared.

    March 28th, 2010 @ 04:27 AM

  4. Repository
  5. Jeremy Kemper
    Jeremy Kemper
    • Milestone set to 3.0.2
    • Importance changed from to High

    October 15th, 2010 @ 11:01 PM