Permalink
Browse files

AR object instantiation is ~30% faster in the simple case

  • Loading branch information...
1 parent 5ddb60f commit 06d4247f0272109d6d3f1ff6beeaf6927f9b3c29 @tenderlove tenderlove committed Jun 27, 2011
Showing with 17 additions and 1 deletion.
  1. +1 −1 activerecord/lib/active_record/base.rb
  2. +16 −0 activerecord/lib/active_record/named_scope.rb
@@ -2074,7 +2074,7 @@ def convert_number_column_value(value)
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
@@ -40,6 +40,22 @@ def scoped(options = nil)
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 <tt>where(:color => :red).select('shirts.*').includes(:washing_instructions)</tt>.
#

0 comments on commit 06d4247

Please sign in to comment.