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.

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

#3602

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!

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

State: duplicate
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. CancelProfileIsBroken
    CancelProfileIsBroken
    • State changed from new to duplicate

    Dupe of #1334

    December 21st, 2009 @ 01:10 AM