ernie/squeel — Issue #99: Squeel drops :uniq => true restriction on :has_many => :through
Author: aepstein
Created: 2012-01-21 21:17:04+00:00
State: open

Consider this scenario:
[@
class Parent < ActiveRecord::Base
has_many :children
has_many :schools, :through => :children, :uniq => true
end

class Child < ActiveRecord::Base
belongs_to :parent
belongs_to :school
end

class School < ActiveRecord::Base
has_many :children
end

p=Parent.create
s=School.create
2.times { Child.create( parent: p, school: s ) }

p.reload
p.schools.count
p.schools.length
@]

We expect the .count and .length to return the same result: 1. Without squeel we get the expected result. With squeel loaded, we get 1 from count and 2 from length. Squeel seems to cause the generated sql for length and find methods to omit the "SELECT DISTINCT" syntax that uniques the result. This seems to be new with an upgrade to Rails 3.2 -- did not have it with Rails 3.1.

I don't know enough about squeel internals to suggest where the problem is, but hoping this is enough to bring about a spec and patch soon.