named_scope goodness on edge rails
named_scope :active, :conditions => { :is_active => true }
named_scope :inactive, :conditions => { :is_active => false } do
def activate
# Use with great care.
# User.inactive.activate
each { |user| user.update_attribute(:active, true) }
end
end
# Oh oh oh. Should work in production mode this way.
named_scope :called, lambda { |username| { :conditions => { :username => username } } }
def self.perform_login(username, password)
# New Rails goodness.
usr = active.called(username).first
...
And no, it doesn’t do select * from without limit and pick the first result, it writes in sql the LIMIT clause. Win.
Trackbacks
Use the following link to trackback from your own site:
http://www.icoretech.org/trackbacks?article_id=named_scope-goodness-on-edge-rails&day=04&month=07&year=2008
-
Lyrics prozac made me stay.