Distinct
og Group By
vil give dig forskellige resultater. For at få de resultater, du forventer, skal du bruge
Person.group(:name).count
(1.2ms) SELECT COUNT(*) AS count_all, name AS name FROM "people" GROUP BY "people"."name"
=> {"Dan"=>3, "Dave"=>2, "Vic"=>1}
Set ovenfor vil gruppen returnere ting som en hash. Mens distinkt bare returnerer antallet af personer i alt, ses nedenfor.
Person.distinct(:name).count
(0.4ms) SELECT DISTINCT COUNT(DISTINCT "people"."id") FROM "people"
=> 6