This project is archived and is in readonly mode.

#5964 ✓resolved
chris (at thredup)

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

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>

Referenced by

Pages