named_scope goodness on edge rails

Posted by kain Thu, 03 Jul 2008 22:47:00 GMT


  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

  1. Prozac dangers. Prozac.
    Lyrics prozac made me stay.
Comments

Leave a comment

Comments