Created
December 2, 2010 21:23
-
-
Save mjy/726098 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
named_scope :that_are_homonyms, { | |
:group => "sensus.label_id", | |
:joins => 'JOIN sensus on labels.id = sensus.label_id', | |
:select => "labels.*, count(distinct sensus.ontology_class_id) as total", | |
:having => 'total > 1' | |
} | |
When I do | |
bar.foo.that_are_homonyms.size # => 1000s (wrong) | |
When I do | |
blorf = bar.foo.that_are_homonyms | |
blorf.size # => 100s (right) | |
Why? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
found this: http://www.ruby-forum.com/topic/197436
Also, the reason why Thing.distinct.all.size works is because .all
converts the association proxy returned by the named scope into an
array. Rather than running an SQL query to determine the size of the
dataset, after calling .all it just returns the number of items in the
actual array of data. Since the GROUP is added properly to the actual
data fetch query, .all returns the proper number because that array
has the proper data in it.