This project is archived and is in readonly mode.
batches: each with named_scope doesn't seem to work
Reported by Carlos Antonio da Silva | March 10th, 2009 @ 07:38 PM
I've been testing the new find_in_batches and each methods with named_scopes, and verified that the each method actually does not seem to work correctly. While find_in_batches uses the named_scope in the right way, the each method instead is executing the query and using the each method from Enumerable.
To demonstrate it:
class Person < ActiveRecord::Base
named_scope :all_active, :conditions => { :active => true }
def process_something
# Process
end
end
# This works as expected
Person.all_active.find_in_batches do |people_batch|
people_batch.each { |person| person.process_something }
end
# This doesn't work
Person.all_active.each do |person|
person.process_something
end
I guess the each method should work fine as find_in_batches, and for those who want to iterate over the array of records from named_scope call, could use something like:
Person.all_active.all.each do |person|
person.process_something
end
Comments and changes to this ticket
-
Carlos Antonio da Silva March 10th, 2009 @ 11:01 PM
- Assigned user set to DHH
I've made a little test adding the
each
method to theNON_DELEGATE_METHODS
inside Scope class, so the method won't be delegated to the Array and will correctly be handled by the neweach
method in batches.I think this way should work, and like I said we still have the
all.each
method use when needed. -
Pratik March 11th, 2009 @ 12:34 AM
- Assigned user changed from DHH to Pratik
- Milestone cleared.
-
Pratik March 11th, 2009 @ 03:04 AM
- no changes were found...
-
Repository March 11th, 2009 @ 03:10 PM
- State changed from new to resolved
(from [0b6f514947c38bf7af2b7e98905a49a94832e5f6]) Add NamedScope#find_each tests [#2201 state:resolved] http://github.com/rails/rails/co...
-
Carlos Antonio da Silva March 11th, 2009 @ 04:41 PM
I've created the patch using git diff and not git format-patch.. sorry..
I really liked changing method name from
each
tofind_each
. This makes things cleaner. -
Jeff Kreeftmeijer November 7th, 2010 @ 04:54 PM
- Tag cleared.
- Importance changed from to Low
Automatic cleanup of spam.
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>
People watching this ticket
Attachments
Referenced by
- 2201 batches: each with named_scope doesn't seem to work (from [0b6f514947c38bf7af2b7e98905a49a94832e5f6]) Add Nam...
- 2227 batches: :conditions for each are applied to each Model.find within the each loop I would call it by passing a song name, and it would prin...
- 2227 batches: :conditions for each are applied to each Model.find within the each loop Once again, the same occurs also after patch #2201, calli...