Sunday, September 09, 2007

Possibility with scoped_proxy

class User < ActiveRecord::Base
scoped_proxy :admins, :find => { :conditions => ['role = ?', 'super_user'] }
scoped_proxy :has_login do |login|
{ :find => { :conditions => ['login = ?', login] } }
end
scoped_proxy :no_op do
nil
end
end

# This gives you the first administrator of the system
User.admins.find(:first)
# This counts the administrators
User.admins.count

# All users with a given login
User.has_login('foo').count
User.has_login('foo').find(:all, :order => 'created_at desc')

# And finally, I give you the no op
User.no_op.find(:all) # => User.find(:all)

No comments: