Skip to content

Commit 558b5bb

Browse files
committed
reduce object allocation during AR instantiation
1 parent 196f92f commit 558b5bb

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

activerecord/lib/active_record/base.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2092,6 +2092,8 @@ def convert_number_column_value(value)
20922092
end
20932093

20942094
def populate_with_current_scope_attributes
2095+
return unless self.class.scope_attributes?
2096+
20952097
self.class.scope_attributes.each do |att,value|
20962098
send("#{att}=", value) if respond_to?("#{att}=")
20972099
end

activerecord/lib/active_record/named_scope.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,18 @@ def scope_attributes # :nodoc:
4747
if current_scope
4848
current_scope.scope_for_create
4949
else
50-
# Return an empty hash in the simple case
51-
return {} unless default_scopes.any?
52-
5350
scope = relation.clone
5451
scope.default_scoped = true
5552
scope.scope_for_create
5653
end
5754
end
5855

56+
##
57+
# Are there default attributes associated with this scope?
58+
def scope_attributes? # :nodoc:
59+
current_scope || default_scopes.any?
60+
end
61+
5962
# Adds a class method for retrieving and querying objects. A \scope represents a narrowing of a database query,
6063
# such as <tt>where(:color => :red).select('shirts.*').includes(:washing_instructions)</tt>.
6164
#

0 commit comments

Comments
 (0)