This project is archived and is in readonly mode.
[PATCH] Eager loading don't work properly on nested includes
-
fxposter
It seems that removing ".uniq" ActiveRecord::AssociationPreload::ClassMethods#preload_associations and adding "records = Array.wrap(records).compact.uniq" to ActiveRecord::AssociationPreload::ClassMethods#preload_has_many_association fixes that bug and doesn't break anything (according to tests).
-
fxposter
No, I was wrong. It doesn't fix the problem.
def test_including_duplicate_objects_from_has_many post = Post.create!(:title => 'foo', :body => "I like cars!") comment = SpecialComment.create!(:body => 'Come on!', :post => post) first_category = Category.create! :name => 'First!', :posts => [post] second_category = Category.create! :name => 'Second!', :posts => [post] categories = Category.where(:id => [first_category.id, second_category.id]).includes(:posts => :special_comments) assert_equal categories.map { |category| category.posts.first.special_comments.loaded? }, [true, true] endThat's the test case, showing the bug.
-
fxposter
- Tag changed from eager_loading to bug, eager_loading, patch
- Assigned user set to Aaron Patterson
Added patch with fix and test case (I'm not sure if my fix is 100% right, but it works + doesn't break any tests).
-
fxposter
- Title changed from Eager loading don't work properly on nested includes to [PATCH] Eager loading don't work properly on nested includes
-
Peter Bui
+1
I applied this patch successfully to the 3-0-stable branch and ran the tests successfully.
I went back and looked at why the Array#uniq call was there in the first place (https://github.com/rails/rails/commit/5dee6ce9e0e31c86c4319c17a1ee5...) and it looks like it's residue from 3 years ago. The current code base (before patch) already ensures there are no duplicate records loaded with eager loading, so you're right that the Array#uniq is not needed.
-
Aaron Patterson
- State changed from new to committed
- Importance changed from to Low
I've pulled this commit in to 3-0-stable. I tried porting to master, but the test didn't fail on master. I'm closing this, but can you verify that master is OK?
-
fxposter
Thanks. I'll try to test master branch a bit later.
-
fxposter
It seems like rails edge doesn't have this problem - everything works fine.
