//DEVGURU

Bartosz Pietrzak @ March 26th, 2009

Useful gems: Quick Scopes

Some time ago I’ve found an useful gem – internuity’s quick scopes.

As readme for the plugin says,

A Rails plugin to automatically add some quick named_scopes to your models.

These are especially useful for quick modifications to a query on an association.

It creates default named_scopes for any of your models:

  • order
  • limit
  • offset
  • with – alias for :include
  • where – alias for :conditions

Personally I don’t like mixing named_scopes with :conditions and :order options – with this gem we can chain our scopes with limit, order etc:

  user.posts.order('created_at')

  user.posts.where(:published => true)

Another small thing that makes our code cleaner.

3 comments do “Useful gems: Quick Scopes”

  1. Marcin Stecki wrote:

    Well,”order”, “limit”, “offset” – i used to define such scopes by myself, i’m not sure i need a gem to do that for me? I like the ‘where’ and ‘with’ idea doe.

  2. Wiktor Schmidt wrote:

    @marcin
    I guess the point is you don’t have to do it anymore = cleaner code :)

  3. Krzysztof Jabłoński wrote:

    This gem is not compatible with Searchlogic :)

Comment this post!