aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/core.rb
diff options
context:
space:
mode:
authorCaleb Thompson <cjaysson@gmail.com>2013-04-13 00:11:04 -0400
committerCaleb Thompson <cjaysson@gmail.com>2013-04-15 19:57:28 -0400
commitd5867a01a82d14216541c8bfc38e466b02580376 (patch)
tree1a23a58200e8566ec77df6b832a4350871b7e14b /activerecord/lib/active_record/core.rb
parentae4a02a7f02042514d50e2024f9751afbdb21cf0 (diff)
downloadrails-d5867a01a82d14216541c8bfc38e466b02580376.tar.gz
rails-d5867a01a82d14216541c8bfc38e466b02580376.tar.bz2
rails-d5867a01a82d14216541c8bfc38e466b02580376.zip
Fix freeze applying to cloned objects
Previously, freezing a cloned ActiveRecord object froze the original too. By cloning `@attributes` before freezing, we prevent cloned objects (which in Ruby share state of ivars) from being effected by `#freeze`. Resolves issue #4936, which has further information on this issue, as well as steps to reproduce. * Add a test case for `#freeze` not causing `cloned.frozen?` to be true. * Clone @attributes before freezing in `ActiveRecord::Core`, then reassign the cloned, frozen hash to the frozen model's `@attributes` ivar. /cc @steveklabnik
Diffstat (limited to 'activerecord/lib/active_record/core.rb')
-rw-r--r--activerecord/lib/active_record/core.rb6
1 files changed, 4 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/core.rb b/activerecord/lib/active_record/core.rb
index 733d4e1c67..9e45e6e474 100644
--- a/activerecord/lib/active_record/core.rb
+++ b/activerecord/lib/active_record/core.rb
@@ -307,9 +307,11 @@ module ActiveRecord
id.hash
end
- # Freeze the attributes hash such that associations are still accessible, even on destroyed records.
+ # Clone and freeze the attributes hash such that associations are still
+ # accessible, even on destroyed records, but cloned models will not be
+ # frozen.
def freeze
- @attributes.freeze
+ @attributes = @attributes.clone.freeze
self
end