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.

Having both ActiveRecord::Base#each and #find_each possibly unnecessary

#3974

I was reading the ActiveRecord 3 query interface documentation, and there are two methods to enumerate over ActiveRecord models: #each, and #find_each, with the difference being that find_each is batched.

Why not ditch the #find_each method, and make #each batch by default with an optional batch size override? That way Enumerable methods like Model.map "just work". Otherwise, you have to use an awkward combination of Model.find_each.map (assuming #find_each actually returns an Enumerator instead of just yielding).

If batching by default isn't desired, you could still do Model.each(10_000).map, which would at least be less awkward and more conventionally-named.

Is there any reason it's done the way it is currently? If not, I wouldn't mind submitting a patch to add that functionality directly to #each, while deprecating #find_each.

Reported by Stephen Touset · February 16th, 2010 @ 11:01 PM

State: stale
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. Ryan Bigg
    Ryan Bigg
    • State changed from new to open

    I think the POLS could be applied here. If I do Model.each, I should be able to expect this to iterate over every single record in my model. I do agree with you however on the point of find_each.map, which is ugly, but I'm going to have to:

    -1

    this.

    May 25th, 2010 @ 10:57 AM

  2. Stephen Touset
    Stephen Touset

    ActiveRecord::Base#find_each does iterate over every single record in the model, with the same calling conventions as #each. The difference being that, behind the scenes, multiple records are fetched from the database at a time. I would argue that POLS points toward #each being doing this automatically, since it's very surprising when calling #each nukes database performance and takes forever.

    As it stands, #each is practically useless and should never be called due to the load it places in the database. Just making it batch behind the scenes would make the method actually useful. Plus, this could be combined with standard Enumerator practice to composite enumeration methods.

    May 25th, 2010 @ 02:56 PM

  3. Stephen Touset
    Stephen Touset

    For what it's worth, you may be confusing #find_each and #find_in_batches. The former obeys #each's calling convention (no required params, yields each object in succession), and the latter yields an Array of each batched set of objects.

    May 25th, 2010 @ 02:57 PM

  4. Santiago Pastorino
    Santiago Pastorino
    • Importance changed from to Low

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    February 2nd, 2011 @ 04:52 PM

  5. Santiago Pastorino
    Santiago Pastorino
    • State changed from open to stale

    February 2nd, 2011 @ 04:52 PM

  6. Stephen Touset
    Stephen Touset
    • State changed from stale to open

    This issue is still alive and relevant. ActiveRecord::Base#each is effectively useless in its current state, and would be somewhat useful if it fetched records in batches behind the scenes.

    [state:open]

    February 3rd, 2011 @ 04:35 AM

  7. rails
    rails

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    May 4th, 2011 @ 01:00 AM

  8. rails
    rails
    • State changed from open to stale

    May 4th, 2011 @ 01:00 AM