This project is archived and is in readonly mode.
has_many through append to collection should have duplicates before reload
Reported by chris (at thredup) | November 12th, 2010 @ 08:03 PM
There seems to be a problem with the behavior of has_many through.
When you access the collection before appending multiple identical objects, the collection is not updated. For example the following RSpec should pass:
Models
Package has_many categories through package_categories
PackageCategory belongs to package and category
Category has_many packages through package_categories
THIS FAILS (on the last line):
@p = Package.create
@cat1 = Category.first
@p.categories.map(&:id).count.should be 0
@p.categories << @cat1
@p.categories << @cat1
@p.categories << @cat1
@p.categories.map(&:id).count.should be 3
THIS PASSES:
@p = Package.create
@cat1 = Category.first
@p.categories << @cat1
@p.categories << @cat1
@p.categories << @cat1
@p.categories.map(&:id).count.should be 3
THIS PASSES:
@p = Package.create
@cat1 = Category.find(1)
@cat2 = Category.find(2)
@cat3 = Category.find(3)
@p.categories.map(&:id).count.should be 0
@p.categories << @cat1
@p.categories << @cat2
@p.categories << @cat3
@p.categories.map(&:id).count.should be 3
It seems to be isolated to added multiple of the same object after previously accessing the collection.
Any ideas?
Comments and changes to this ticket
-
chris (at thredup) November 12th, 2010 @ 10:43 PM
Attaching has_many_through_associations_test.rb with a case I added to show this behavior.
My added case is line 67
activerecord/test/cases/associations/has_many_through_associations_test.rb -
chris (at thredup) November 13th, 2010 @ 12:25 AM
My first stab at fixing this is now pushed to my fork.
The problem is that my fix now causes problems for nested_attributes - two tests in there are now failing.
The commit is here: https://github.com/thredup/rails/commit/df912531295e3e57314b00ef811...
Any input from people that know the code base better than I would be awesome.
-
chris (at thredup) November 13th, 2010 @ 12:59 AM
Committed some files by mistake - fixed and re-pushed: https://github.com/thredup/rails/commit/ad0b5421e8e4893924ff70a97dd...
-
chris (at thredup) November 13th, 2010 @ 01:00 AM
- Title changed from has_many through collection bug to has_many through append to collection should have duplicates before reload
-
Jon Leighton December 22nd, 2010 @ 12:22 AM
- Assigned user set to Aaron Patterson
- Tag changed from rail3 has_many, has_many_through to rail3 has_many, has_many_through, patch
- Importance changed from to Low
Hi Chris,
Thanks for the bug report. I've created a patch which I'll hopefully get Aaron to merge at some point.
Cheers,
Jon -
Jon Leighton December 22nd, 2010 @ 12:22 AM
- State changed from new to open
-
Repository December 23rd, 2010 @ 11:21 PM
- State changed from open to resolved
(from [ff7bde62c857ec94f45a5be3bc76468deb8b0b3a]) When a has_many association is not :uniq, appending the same record multiple times should append it to the @target multiple times [#5964 state:resolved] https://github.com/rails/rails/commit/ff7bde62c857ec94f45a5be3bc764...
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
- 5964 has_many through append to collection should have duplicates before reload (from [ff7bde62c857ec94f45a5be3bc76468deb8b0b3a]) When a ...