Skip to content

Instantly share code, notes, and snippets.

@mjy
Created December 2, 2010 21:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjy/726098 to your computer and use it in GitHub Desktop.
Save mjy/726098 to your computer and use it in GitHub Desktop.
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?
@mjy
Copy link
Author

mjy commented Dec 2, 2010

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment