Skip to content

How to alias and create a new named scope out of existing ones

Named scopes in Rails are just awesome. They so much DRY and beautify your code! But did you ever had the trouble to create a new named_scope out of others? E.g. aliasing multiple ones to a new one. Luckily I now figured out how this works in a nice way.
The magic key is the method scope( :find )

This method called on a Scope class returns the Hash used for the ActiveRecord .find() call. First it’s a nice way to debug your scopes, second it’s perfect for our need to chain multiple scoped into one. Check this out:

[ruby]
class Fu < ActiveRecord::Base
named_scope :has_moo, :conditions => { :moo => true }
named_scope :has_foo, :conditions => { :foo => true }

named_scope :has_moo_and_foo, lambda { has_moo.has_foo.scope(:find) }
end
[/ruby]

We chain the two scoped has_moo and has_foo together into a new one called has_moo_and_foo. As the return value can only be a Hash we use the above mentioned scope(:find) to transform it into one.

Now this works:

[ruby]Fu.has_moo_and_foo.all[/ruby]

Sweet! Imagine how great this would be if we can return a scope object instead of a hash as well??

See my pastie here too.