From cfad74cfef51888321c042623c9e2fb9663640f3 Mon Sep 17 00:00:00 2001 From: Neeraj Singh Date: Thu, 12 Aug 2010 10:23:20 -0400 Subject: updating documentation for named_scope and default_scope --- activerecord/lib/active_record/base.rb | 10 ++++++++++ activerecord/lib/active_record/named_scope.rb | 9 +++++++++ 2 files changed, 19 insertions(+) diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 8da4fbcba7..a464b65c19 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1147,6 +1147,16 @@ MSG # class Person < ActiveRecord::Base # default_scope order('last_name, first_name') # end + # + # default_scope is also applied while creating/building a record. It is not + # applied while updating a record. + # + # class Article < ActiveRecord::Base + # default_scope where(:published => true) + # end + # + # Article.new.published #=> true + # Article.create.published #=> true def default_scope(options = {}) self.default_scoping << construct_finder_arel(options, default_scoping.pop) end diff --git a/activerecord/lib/active_record/named_scope.rb b/activerecord/lib/active_record/named_scope.rb index 0e560418dc..c95060126e 100644 --- a/activerecord/lib/active_record/named_scope.rb +++ b/activerecord/lib/active_record/named_scope.rb @@ -88,6 +88,15 @@ module ActiveRecord # end # end # end + # + # Scopes can also be used while creating/building a record. + # + # class Article < ActiveRecord::Base + # scope :published, where(:published => true) + # end + # + # Article.published.new.published #=> true + # Article.published.create.published #=> true def scope(name, scope_options = {}, &block) name = name.to_sym valid_scope_name?(name) -- cgit v1.2.3