This project is archived and is in readonly mode.
Eager loading uniq habtm association is broken
-
CancelProfileIsBroken
- Tag changed from 2.3.3, activerecord to 2.3.3, activerecord, bugmash
-
Elad Meidar
+1 verified and reproduced on master and 2-3-stable. i've attached a failing test.
-
Elad Meidar
the failing test if for master.
-
hsume2 (Henry)
-1 changing the
developers.ymlsomehow introduced failures in other tests.I rewrote it using
dev_3instead. I've attached the edited failing test. -
hsume2 (Henry)
Also, this is my take on patching the issue. I thought of adding something like (-190,6 +190,7):
+ associated_records.uniq! if options[:uniq] set_association_collection_records(id_to_record_map, reflection.name, associated_records, 'the_parent_record_id')to
#preload_has_and_belongs_to_many_association, but I think this is a more general issue. Instead, it seems that when#load_targetis called by#count_records, the target is already loaded. I've attached a patch. (Applies to master and 2-3-stable).I'm also unsure if this is the optimal way (alternative,
options[:select] = 'DISTINCT *'). -
hsume2 (Henry)
- Tag changed from 2.3.3, activerecord, bugmash to 2.3.3, activerecord, bugmash, bugmash-review
-
Rizwan Reza
- Tag changed from 2.3.3, activerecord, bugmash, bugmash-review to 2.3.3, activerecord, bugmash-review
-
Rizwan Reza
- Tag changed from 2.3.3, activerecord, bugmash-review to 2.3.3, activerecord, bugmash
-
Subba
- Importance changed from to
Henry can you update your patch it seems your patch link is broken.
-
hsume2 (Henry)
I've attached reworked patches for master and 2-3-stable. The implementation is slightly different than previously specified. Instead of
associated_records.uniq! if options[:uniq]I've gone the
DISTINCTmethod, because that seems to be the behavior forhas_many :throughwith:uniq => trueand custom:select(seeconstruct_selectinthrough_association_scope.rb:48). -
Neeraj Singh
- Milestone set to 3.x
@Henry looks like you were not in the rails root directory while creating the patch. I tried applying rails edge patch and it failed.
Could you please create that patch again. thanks.
-
hsume2 (Henry)
Sure, here are the patches created from rails root.
-
Neeraj Singh
not sure why but I am still getting the error while applying your patch
(master)$ git am < patch.txt Applying: Fix a bug where eager loading of a :uniq => true has_and_belongs_to_many returns duplicate entries error: activerecord/lib/active_record/association_preload.rb: does not match index error: activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb: does not match index Patch failed at 0001 Fix a bug where eager loading of a :uniq => true has_and_belongs_to_many returns duplicate entries When you have resolved this problem run "git am --resolved". If you would prefer to skip this patch, instead run "git am --skip". To restore the original branch and stop patching run "git am --abort". -
hsume2 (Henry)
@Neeraj, I'm not sure why either. I've applied the patch multiple times today at different commits, most recently on [387036609268c4cf68401a1333715f2ee4aecffc], and it's worked.
It looks like you've had a few patches accepted recently, did you reset the branch and pull from master before you applied my patch? Or do you have any unstaged changes on
association_preload.rborhas_and_belongs_to_many_associations_test.rb? -
Neeraj Singh
I checked out brand new rails app before applying the patch. Well someone else will verify your patch.
-
hsume2 (Henry)
Okay, that's pretty weird. Well thanks anyway.
-
Santiago Pastorino
- State changed from new to open
- Tag changed from 2.3.3, activerecord, bugmash to 233, activerecord, bugmash
This issue has been automatically marked as stale because it has not been commented on for at least three months.
The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.
Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.
-
Santiago Pastorino
- State changed from open to stale
