Whats the significance of named scope in rails?
Posted
by piemesons
on Stack Overflow
See other posts from Stack Overflow
or by piemesons
Published on 2010-05-27T09:00:37Z
Indexed on
2010/05/27
9:01 UTC
Read the original article
Hit count: 392
ruby-on-rails
|named-scope
Before going for details.
Question 1:-- Whats the meaning of scope here (ie named scope)? whats the benefits of using named scope?
Now:-
from agile development with rails book:--
class Order < ActiveRecord::Base
named_scope :last_n_days, lambda { |days| {:conditions =>
['updated < ?' , days] } }
named_scope :checks, :conditions => {:pay_type => :check}
end
Such a named scope would make finding the last weeks worth of orders a snap.
orders = Orders.last_n_days(7)
Scopes can also be combined
orders = Orders.checks.last_n_days(7)
why we are using named_scope here. We can do the same using methods. Whats special thing we got using named_scope.
© Stack Overflow or respective owner