Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

has_many through append to collection should have duplicates before reload

#5964

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?

Reported by chris (at thredup) · November 12th, 2010 @ 08:03 PM

State: resolved
Milestone: none
Assigned to: Aaron Patterson Aaron Patterson
Importance: Low

Activity

  1. chris (at thredup)
    chris (at thredup)

    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

    November 12th, 2010 @ 10:43 PM

  2. chris (at thredup)
  3. chris (at thredup)
    chris (at thredup)

    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.

    November 13th, 2010 @ 12:25 AM

  4. chris (at thredup)
  5. chris (at thredup)
    chris (at thredup)
    • Title changed from has_many through collection bug to has_many through append to collection should have duplicates before reload

    November 13th, 2010 @ 01:00 AM

  6. Jon Leighton
    Jon Leighton
    • 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

    December 22nd, 2010 @ 12:22 AM

  7. Jon Leighton
    Jon Leighton
    • State changed from new to open

    December 22nd, 2010 @ 12:22 AM

  8. Repository
    Repository
    • 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 has_many through append to collection should have duplicates before reload state:resolved] https://github.com/rails/rails/commit/ff7bde62c857ec94f45a5be3bc764...

    December 23rd, 2010 @ 11:21 PM