Skip to content

Commit

Permalink
reduce object allocation during AR instantiation
Browse files Browse the repository at this point in the history
  • Loading branch information
tenderlove committed Jun 28, 2011
1 parent 196f92f commit 558b5bb
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
2 changes: 2 additions & 0 deletions activerecord/lib/active_record/base.rb
Expand Up @@ -2092,6 +2092,8 @@ def convert_number_column_value(value)
end

def populate_with_current_scope_attributes
return unless self.class.scope_attributes?

self.class.scope_attributes.each do |att,value|
send("#{att}=", value) if respond_to?("#{att}=")
end
Expand Down
9 changes: 6 additions & 3 deletions activerecord/lib/active_record/named_scope.rb
Expand Up @@ -47,15 +47,18 @@ def scope_attributes # :nodoc:
if current_scope
current_scope.scope_for_create
else
# Return an empty hash in the simple case
return {} unless default_scopes.any?

scope = relation.clone
scope.default_scoped = true
scope.scope_for_create
end
end

##
# Are there default attributes associated with this scope?
def scope_attributes? # :nodoc:
current_scope || default_scopes.any?
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>.
#
Expand Down

0 comments on commit 558b5bb

Please sign in to comment.