Tag: named_scope
Understand Named_scope in 60 seconds
June 28th, 2009, 1 Comment
Named scope was introduced in Rails 2.x, it allows you to filter a selection of records easily and repeatedly, without having to write finder SQL all the time.
class Customer < ActiveRecord::Base
has_many :carts
end
class Cart < ActiveRecord::Base
belongs_to :customer
named_scope :completed, :conditions => ["NOT ISNULL(completed_at)"]
end
Example of self-model scoping and finding:
# All completed carts [...]