Permalink
Browse files

Freeze columns just before using them as hash keys

  • Loading branch information...
1 parent 2004ef2 commit 2068d300917ed95a82e7377ee77b397fc4084a61 @spastorino spastorino committed Sep 20, 2012
Showing with 5 additions and 2 deletions.
  1. +5 −2 activerecord/lib/active_record/result.rb
View
7 activerecord/lib/active_record/result.rb
@@ -11,7 +11,7 @@ class Result
attr_reader :columns, :rows, :column_types
def initialize(columns, rows, column_types = {})
- @columns = columns.map{|c| c.freeze}
+ @columns = columns
@rows = rows
@hash_rows = nil
@column_types = column_types
@@ -54,7 +54,10 @@ def initialize_copy(other)
private
def hash_rows
@hash_rows ||= @rows.map { |row|
- Hash[@columns.zip(row)]
+ # We freeze the strings to prevent them getting duped when
+ # used as keys in ActiveRecord::Model's @attributes hash
+ columns = @columns.map { |c| c.freeze }
+ Hash[columns.zip(row)]
}
end
end

4 comments on commit 2068d30

@brainopia

Well, it should be reverted, comment in code even explains why :)

@jeremyevans

Agreed, this needs to be reverted or further patched. The freezing code needs to be moved outside of @rows.map (but within the ||=, probably using begin ... end), and it should either use columns = @columns.each{|c| c.freeze} (saves an array object creation, but modifies @columns) or columns = @columns.map{|c| c.dup.freeze} (allocates a single new array and one string per column, but doesn't modify @columns). #7631

@spastorino
Ruby on Rails member

Oooops you're right

@spastorino
Ruby on Rails member

Fixed da400fb

Please sign in to comment.