From d5867a01a82d14216541c8bfc38e466b02580376 Mon Sep 17 00:00:00 2001 From: Caleb Thompson Date: Sat, 13 Apr 2013 00:11:04 -0400 Subject: 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 --- activerecord/lib/active_record/core.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'activerecord/lib') 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 -- cgit v1.2.3