Skip to content

Instantly share code, notes, and snippets.

@bronson
Created April 9, 2010 01:55
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 bronson/360792 to your computer and use it in GitHub Desktop.
Save bronson/360792 to your computer and use it in GitHub Desktop.
Named scopes are great. You can compose huge queries by gluing
parts together.
u = User
u = u.employee if params[:employee]
u = u.unvested if params[:unvested]
... etc.
u.length -- returns the number of users matching the query.
Works great if you specify either parameter. You end up with one of:
User.employee.length
User.unvested.length
User.unvested.employee.length
However, if both scope1 and scope2 are false, you end up with:
User.length
Which fails.
Is this a design hole in named scopes? Is there a way to
make this work? It should of course return a count of every
user if none of the scopes are applied.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment