This project is archived and is in readonly mode.

#3602 ✓duplicate
Thomas Sinclair

Activerecord ignoring Distinct for Postgres in named scope for count / size

Reported by Thomas Sinclair | December 21st, 2009 @ 12:46 AM

Just spotted this one in rspec, I hope this is descriptive enough:

Competitor_spec.rb
it "should automatically take out duplicate email addresses under the newsletter_subscribers scope" do

5.times do Competitor.make(:newsletter => true) end
Competitor.create!(@valid_attributes)
Competitor.create!(@valid_attributes)
Competitor.create!(@valid_attributes)
Competitor.count.should eql(8)
p Competitor.newsletter_subscribers
Competitor.newsletter_subscribers.size.should eql(6)

end

Competitor.rb
named_scope :newsletter_subscribers, :select => "DISTINCT Competitors.email", :conditions => ['competitors.newsletter = ?', true]

And the sql logs:
Competitor Load (0.5ms) SELECT DISTINCT Competitors.email FROM "competitors" WHERE (competitors.newsletter = 't') SQL (0.4ms) SELECT count(*) AS count_all FROM "competitors" WHERE (competitors.newsletter = 't')

As you can see it's ignoring the distinct when doing a count or size on the named scope.

Cheers!

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>

People watching this ticket

Pages