diff --git a/activerecord/lib/active_record/attributes/typecasting.rb b/activerecord/lib/active_record/attributes/typecasting.rb index de36a297eb..56c32f9895 100644 --- a/activerecord/lib/active_record/attributes/typecasting.rb +++ b/activerecord/lib/active_record/attributes/typecasting.rb @@ -37,6 +37,12 @@ module ActiveRecord hash.merge!(self) hash end + + def dup # :nodoc: + copy = super + copy.types = types.dup + copy + end # Provides a duplicate with typecasting disabled. # diff --git a/activerecord/test/cases/attributes/typecasting_test.rb b/activerecord/test/cases/attributes/typecasting_test.rb index c712f224b2..8a3b551375 100644 --- a/activerecord/test/cases/attributes/typecasting_test.rb +++ b/activerecord/test/cases/attributes/typecasting_test.rb @@ -86,12 +86,14 @@ class TypecastingTest < ActiveRecord::TestCase end test "without typecasting" do + @attributes.merge!('comments_count' => '5') attributes = @attributes.without_typecast - attributes['comments_count'] = '5' - + assert_equal '5', attributes['comments_count'] + assert_equal 5, @attributes['comments_count'], "Original attributes should typecast" end + test "typecast all attributes" do attributes = @attributes.merge('title' => 'I love sandwiches', 'comments_count' => '5') attributes.typecast!