DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

William has posted 8 posts at DZone. View Full User Profile

How To Use Limit With Rails Named_scope

09.30.2009
| 6962 views |
  • submit to reddit
        
class Competition < ActiveRecord::Base
  named_scope :limit, lambda { |num| { :limit => num } }
  named_scope :active, :conditions => {:status => Status::ACTIVE}
end

For example, if you want to get 5 active competitions

Competition.active.limit(5)