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

Comments (8)

Tim ConnorJanuary 27th, 2009 at 19:10

FYI, limit can just as easily without this via .first(5) and .last(5) and a little shaving with Occam might be useful to avoid too many ways to do the same thing.

DanJanuary 27th, 2009 at 20:17

Is there a way to specify the offset with the limit?

Have you looked at the API for the Ruby ORM called Sequel? This is starting to look similar.

Andrew TimberlakeJanuary 27th, 2009 at 20:36

@tim Interesting that you can use first and last - I didn’t know that. Not sure that I like them as the obvious way to limit the number of results though.
I am trying to create quick scopes that mimic the options in find as closely as possible (unfortunately include doesn’t work and conditions doesn’t fit the ‘language’ flow quite as nicely)

@dan You can now, just pushed another commit to github

Eric AndersonJanuary 27th, 2009 at 21:48

I like the plugin. Has a more DSL feel compared to the data-structure feel that ActiveRecord::Base.find has. Would love to see this in Rails itself but the plugin is good enough for now.

@Tim - I don’t think post.comments.limit(5) would be the same as post.comments.first(5). If I am reading the Rails code correctly (even edge) then first is just a simple method that calls find(:first, *args). So post.comments.find(5) will not return the first 5 records but instead return the comment with id 5 if it is a comment of post. So I think his limit is necessary. Plus even if first and last worked like you said I agree with Andrew that limit seems more natural.

lucapetteJanuary 28th, 2009 at 16:09

Instant bookmark… very nice and useful. I’ll give it a try next days. I feel i’ll add to my Rails 2.3 templates :)

pimpmasterJanuary 28th, 2009 at 21:33

This is sexy!

Thanks for sharing :)

Akhil BansalJanuary 29th, 2009 at 12:44

I think it should named as sexy_scopes instead of quick_scopes ;-)

Anyways, Thanks a lot for the plugin

Andrew TimberlakeJanuary 29th, 2009 at 13:09

@eric, @lucapette, @pimpmaster & @akhil - Thanks for the kind words and glad you’re finding it useful