From 06d4247f0272109d6d3f1ff6beeaf6927f9b3c29 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 27 Jun 2011 16:15:34 -0700 Subject: AR object instantiation is ~30% faster in the simple case --- activerecord/lib/active_record/base.rb | 2 +- activerecord/lib/active_record/named_scope.rb | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 6106b03ba3..637e419735 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -2074,7 +2074,7 @@ MSG end def populate_with_current_scope_attributes - self.class.scoped.scope_for_create.each do |att,value| + self.class.scope_attributes.each do |att,value| respond_to?("#{att}=") && send("#{att}=", value) end end diff --git a/activerecord/lib/active_record/named_scope.rb b/activerecord/lib/active_record/named_scope.rb index 14db7a6cd6..f9299434ec 100644 --- a/activerecord/lib/active_record/named_scope.rb +++ b/activerecord/lib/active_record/named_scope.rb @@ -40,6 +40,22 @@ module ActiveRecord end end + ## + # Collects attributes from scopes that should be applied when creating + # an AR instance for the particular class this is called on. + def scope_attributes # :nodoc: + if current_scope + current_scope.scope_for_create + else + # Return an empty hash in the simple case + return {} unless finder_needs_type_condition? || default_scopes.any? + + scope = relation.clone + scope.default_scoped = true + scope.scope_for_create + end + end + # Adds a class method for retrieving and querying objects. A \scope represents a narrowing of a database query, # such as where(:color => :red).select('shirts.*').includes(:washing_instructions). # -- cgit v1.2.3