This project is archived and is in readonly mode.
ActiveRecord::Base.find_in_batches puts a with_scope into the block that is executed
-
Valentin Mihov
Here is the fix with adding with_exclusive_scope around the yield statement
-
Valentin Mihov
Here is another fix of the problem. This time the with_scope is removed from the yield and no with_exclusive_scope is used. This one should be a better solution. There is some code duplication but I didn't figure out a way to remove it. Probably a private method could be made to avoid that?
-
Elliot Winkler
I don't understand why the exclusive scope is needed. Shouldn't you be able to do Shirt.red.find_each and find_each then do the right thing?
-
Valentin Mihov
The problem here is that when you make additional queries into a find_each block, the queries are affected in an unexpected behavior.
Have a look at the unit test in the diff. In that example you have posts. Each post have a body and an author_id. You make a find_each for all posts with a given body (ex. "Hello world!"). In the find_each block imagine that you start processing a post from the author X. You want to count how many posts X has and you do Post.all(:conditions => { :author_id => id_of_author_X }). Unfortunately this query will return the number of posts from author X, which has a body equal to "Hello world!", which is obviously not the idea in that situation.
-
Elliot Winkler
Ah, my bad, you were already one step ahead. +1, then -- makes sense to me.
-
Thong Kuah
There is a similar one at #2227
-
Valentin Mihov
Yep... #2227 seems to be the same bug
