This project is archived and is in readonly mode.

#3108 ✓stale
Niels Meersschaert

Eager loading uniq habtm association is broken

Reported by Niels Meersschaert | August 27th, 2009 @ 08:26 PM | in 3.x

Loading a model thru eager loading or simple loading should return same results for associations. That is not the case with habtm with uniq option.

I have a model Foo with a habtm relationship to Bar that is uniq.

class Foo < ActiveRecord::Base
has_and_belongs_to_many :bars, :uniq => true end

class Bar < ActiveRecord::Base
end

Let's build 2 models, one of each type:

foo = Foo.create(:name => "foo_test") #ID 13
bar = Bar.create(:name => "bar_test") #ID 87

I add the same instance of Bar to a Foo multiple times:

5.times do
foo.bars << bar
end

If I do a clean load of foo & then ask for bars, I get 1 as expected:

foo2 = Foo.find(13)

foo2.bars
[#]

However, if I eager load the bars:

foo3 = Foo.find(13, :include => :bars)
foo3.bars
[#,#,#,#,#]

The returned array isn't uniq, but shows all 5 instances of the duplicate bar.

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>

Pages