Thursday, February 15, 2007

More on scope_out

Dan Manges, one of my coworkers, wrote an excellent post on some of the reasons why you would want to use the scope_out plugin. You should go read it because he does a much better job of explaining it than I would. I just wanted to take this opportunity to point out some of the newer features that weren't included in the first release.

Dynamic conditions are now accepted if you pass a block to scope out. Notice how the block evaluates to a Hash. This is important because the block is evaluated each time the scope is called and the result is fed directly to Rails' with_scope method as the options parameter.

class Article < ActiveRecord::Base
scope_out :published_today do
{:conditions => {:publish_date =>}}

You can combine scopes with a new method called (predictably) combined_scope. This method takes previously defined scopes which were created with scope_out and nests them in order to create a new scope.

class Person < ActiveRecord::Base
scope_out :software_developers, :conditions => {:job => "Software Developer"}
scope_out :ruby_programmers, :conditions => {:primary_language => "Ruby"}
combined_scope :happy_programmers, [:software_developers, :ruby_programmers]


Anonymous said...

Awesome plugin - I'm definitely going to be using this.

One point - I would suggest you make the with_blah method protected for the same reason that with_scope is becoming protected:

RoR Core thread

0xefc said...

John, this is excelent, the plugin works great.

In the plugin README you mention that dynamic conditions are evaluated at class load time and not when the condition is checked. Have you found any workaround for this?

Thank you and keep up the good work!

John Andrews said...

@3836: Thanks for the compliment. scope_out has had the ability to do dynamic conditions for some time now, but I hadn't updated the docs, apparently. I have now done so.

@anonymous: I just checked in revision 29 of the plugin, which makes with_x protected. Thanks for pointing that out to me.

Kip said...

Wish I'd found this code 3 weeks ago :-)

I have a situation where the scope to be set is based on joining several tables.

Any chance of a version of with_scope that allows :joins, or :includes?

That would just be fantastic.


Joe said...

John, any chance you could tell me if its possible to refactor the following with your plugin. Ive tried with no success:

This is a situation I seem to run into a lot that I havent found an elegant solution for yet. I have a feeling your plugin may provide the way to do this nicely, but I wasnt able to completely figure it out.

Walther said...

Excellent work - but I'm not sure how to mix with has_many_polymorphs?

Could I persuade you to consider taking just a quick peep at

Beyond that - all the best from Denmark,


Anonymous said...

Dear John....

I am hoping you are from the family I have been looking for a very long time. My parents back in the 50 and 60s were very good friend with Ben and Emma Andrews. You would of grown up in the suburbs of Cleveland. Would you either way please e-mail back to

thank you very much..and I will keep my fingers crossed!