Feature proposal: Use find_each/find_in_batches with pluck

1,136 views
Skip to first unread message

Paco Guzmán

unread,
Jun 14, 2015, 1:56:51 PM6/14/15
to rubyonra...@googlegroups.com

I would like to provide a new feature on Rails that consist on the use of pluck when using find_each/find_in_batches to speed up the loop when is not need to access to active record instances.

Do you think this could be incorporated in Rails? For the moment I'm going to implement a solution for our use case on a Rails 3.2 app

Thanks in advance

Vipul A M

unread,
Jun 14, 2015, 2:08:16 PM6/14/15
to rubyonra...@googlegroups.com
Can you share an example of this proposal?
Vipul A.M.
+91-8149-204995
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Core" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to rubyonrails-co...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> Visit this group at http://groups.google.com/group/rubyonrails-core.
> For more options, visit https://groups.google.com/d/optout.

George Ogata

unread,
Jun 14, 2015, 4:14:18 PM6/14/15
to rubyonra...@googlegroups.com
+1

I find one of the most frequent uses of find_each/find_in_batches is looping through a large collection in order to queue up a list of ids for a background job to process. e.g. queuing up a big list of user ids to send an email to. Would be nice to avoid the overhead of AR objects and just do something like:

    User.some_scopes.pluck_each(:id) { |id| ... }
    User.some_scopes.pluck_in_batches { |batch| ... }

Or maybe pluck could be an alternative to select?

    User.some_scopes.pluck(:id).find_each { |id| ... }

Paco Guzmán

unread,
Jun 19, 2015, 8:35:30 AM6/19/15
to rubyonra...@googlegroups.com
Yes, that our use case too, and as you said we wanted to avoid the AR objects overhead

Fernando Tapia Rico

unread,
Jul 22, 2015, 11:20:42 AM7/22/15
to Ruby on Rails: Core
+1

Kevin Deisz

unread,
Aug 16, 2015, 12:37:40 PM8/16/15
to rubyonra...@googlegroups.com
By the way, with the new in_batches API you can do this. a la

User.some_scopes.in_batches.each do |users|
  users.pluck(:id)
end
Kevin D. Deisz
TrialNetworks - part of DrugDev
Software Developer
383 Elliot Street, Suite G
Newton, MA 02464
+1 703.615.0396 (mobile)
Reply all
Reply to author
Forward
0 new messages