This project is archived and is in readonly mode.

#3974 ✓stale
Stephen Touset

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

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

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.

Comments and changes to this ticket

  • Ryan Bigg

    Ryan Bigg May 25th, 2010 @ 10:57 AM

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

  • Stephen Touset

    Stephen Touset May 25th, 2010 @ 02:56 PM

    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.

  • Stephen Touset

    Stephen Touset May 25th, 2010 @ 02:57 PM

    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.

  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 04:52 PM

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

  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 04:52 PM

    • State changed from “open” to “stale”
  • Stephen Touset

    Stephen Touset February 3rd, 2011 @ 04:35 AM

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

  • rails

    rails May 4th, 2011 @ 01:00 AM

    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.

  • rails

    rails May 4th, 2011 @ 01:00 AM

    • State changed from “open” to “stale”

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="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages