New Rails plugin - Quick Scopes

I’ve just release a new Rails plugin called Quick Scopes that adds some generic named_scopes to your models to quickly manipulate the results you receive from associations.

It’s great to have easy access to methods to get all associations but sometimes you need to manipulate how you receive those results.

Included named_scopes:

  • limit - to limit the number of results
  • order - to order the results
  • where - alias for conditions
  • with - alias for include

Examples

# Returns all the comments on the post
post.comments

# Using quick scopes, you will be able to do the following:

# Limit the number of results
post.comments.limit(5)

# and, order the results
post.comments.order('created_at desc').limit(5)

# and, add conditions to the query
post.comments.where(:approved => true).order('created_at desc').limit(5)

# and, include sub-associations
post.comments.with(:author).where(:approved => true).order('created_at desc').limit(5)

If you need all the manipulation of the last couple of examples, you probably should create a specific named_scope but this can be very useful to have available on the console.

Have a look and let me know how it works for you and what you would do differently.

Quick Scopes: http://github.com/internuity/quick_scopes

Install

./script/plugin install git://github.com/internuity/quick_scopes.git