This project is archived and is in readonly mode.
[VERIFIED] batches: :conditions for each are applied to each Model.find within the each loop
-
Pratik
- Assigned user set to DHH
-
Matthew Beale
This is really nasty. I've attached a patch (with tests) that doesn't use with_scope in find_in_batches.
Check this blog post:
http://davedupre.com/2009/05/20/gotcha-with-find_each-and-find_in_b...
A really simple example of why scopes here are very dangerous:
Balloons.count #=> 10 @clown = Clown.find(2) @clown.balloons.count #=> 3 @clown.balloons.collect {|b| b.color } #=> [ 'red', blue', 'green' ] Balloons.find_each(:conditions => { :color => 'red' }) do @clown = Clown.find(2) @clown.balloons.count #=> 1 @clown.balloons.collect {|b| b.color } #=> [ 'red' ] # Where did all my friggin balloons go? Balloons.all.collect { |b| b.color }.uniq #=> [ 'red' ] endThis means inside a find_each it's impossible to ask Balloons for a non-red object. Also hard to debug :-)
-
Matthew Beale
If this is closed, these can probably be marked dupes:
https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets...
This guy covers a bug with using "id" explicitly:
https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets...
-
Eugene Pimenov
- Tag changed from 2.3-rc1, 2.3-rc2, :conditions, active_record, find, named_scope to 2.3-rc1, 2.3-rc2, :conditions, active_record, find, named_scope, patch
More elegant fix
-
Jacob Kjeldahl
- Tag changed from 2.3-rc1, 2.3-rc2, :conditions, active_record, find, named_scope, patch to 2-3-stable, 2.3-rc1, 2.3-rc2, :conditions, active_record, find, named_scope, patch
I have verified that the 0001-find_in_batches-shouldn-t-clog-conditions-for-find-c.patch applies cleany to 2.3-stable and the tests are running.
-
Thong Kuah
+1 I have tried this patch(0001-find_in_batches-shouldn-t-clog-conditions-for-find-c.patch) against 2-3-stable, and the tests work well. Nice test - shows how innocuous find(:id) fails in a find_each block.
-
Valentin Mihov
https://rails.lighthouseapp.com/projects/8994/tickets/2791-activere...
is the same is this one. I tested the patch on 2-3 stable and it applies and works cleanly. I like the fix. It is better than the two I offered.
I am attaching an additional unit test from #2791. It should be applied over the patch of this issue.
-
Thong Kuah
- Title changed from batches: :conditions for each are applied to each Model.find within the each loop to [VERIFIED] batches: :conditions for each are applied to each Model.find within the each loop
state:verified
-
DHH
- Assigned user changed from DHH to Pratik
-
Scott Windsor
So... this is a bug that's been "verified". Does that mean this fix will get integrated anytime soon?
This is kind of a horrible bug. I just ran into a case where a model was getting incremented with callbacks and that update was getting scoped. (because update_counters uses update_all). Any chance this will get integrated into 2.3 stable?
-
chris finne
+1
-
Ryan Bigg
- State changed from new to verified
-
Repository
- State changed from verified to resolved
(from [18ba648e0de032c6cb68cb48c2aaf3a347294e3d]) Implement find_in_batches without with_scope [#2227 state:resolved]
Signed-off-by: Pratik Naik pratiknaik@gmail.com
http://github.com/rails/rails/commit/18ba648e0de032c6cb68cb48c2aaf3... -
Christopher Sun
Is this patch supposed to fix find_in_batches with named_scopes? I'm seeing issues where conditions provided by named_scopes on a model are being passed through to subsequent finders on any instances obtained through find_in_batches. For example:
Subscription.active.commercial.expired_within(1.year.ago,1.year.from_now).find_in_batches(:batch_size => 500) { |subs| Subscription.first }My expected behavior was to not get the first subscription within active and commercial subscription scopes.
I tested the patched in 2.3.3 and 2.3.5 and had the same issue.
-
Pratik
Please open a new ticket w/ a patch and/or failing test and assign to me.
Thanks.
-
Greg Hazel
- Importance changed from to
Was there ever a new ticket created for this? The bug still exists in ActiveRecord 2.3.11
