From 6a0a5f30324571416174430cf41f3355635c3328 Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Sun, 3 Feb 2013 15:27:42 +0400 Subject: Duplicate column_defaults properly Backport c517602 to fix #6115 Deleted: activerecord/lib/active_record/core.rb Conflicts: activerecord/test/cases/base_test.rb --- activerecord/CHANGELOG.md | 6 ++++++ activerecord/lib/active_record/base.rb | 3 ++- activerecord/test/cases/base_test.rb | 8 +++++++- activerecord/test/schema/schema.rb | 1 + 4 files changed, 16 insertions(+), 2 deletions(-) (limited to 'activerecord') diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 6e8e45db20..52e0e05a5a 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,5 +1,11 @@ ## Rails 3.2.12 (unreleased) ## +* Don't update `column_defaults` when calling destructive methods on column with default value. + Backport c517602. + Fix #6115. + + *Piotr Sarnacki + Aleksey Magusev + Alan Daud* + * When `#count` is used in conjunction with `#uniq` we perform `count(:distinct => true)`. Fix #6865. diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 612114bbce..5a7743b808 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -479,7 +479,8 @@ module ActiveRecord #:nodoc: # # Instantiates a single new object bypassing mass-assignment security # User.new({ :first_name => 'Jamie', :is_admin => true }, :without_protection => true) def initialize(attributes = nil, options = {}) - @attributes = self.class.initialize_attributes(self.class.column_defaults.dup) + defaults = Hash[self.class.column_defaults.map { |k, v| [k, v.duplicable? ? v.dup : v] }] + @attributes = self.class.initialize_attributes(defaults) @association_cache = {} @aggregation_cache = {} @attributes_cache = {} diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index b849eead26..97d6c0cf88 100644 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -325,6 +325,12 @@ class BasicsTest < ActiveRecord::TestCase assert parrot.valid? end + def test_default_values_are_deeply_dupped + company = Company.new + company.description << "foo" + assert_equal "", Company.new.description + end + def test_load topics = Topic.find(:all, :order => 'id') assert_equal(4, topics.size) @@ -2148,7 +2154,7 @@ class BasicsTest < ActiveRecord::TestCase end def test_attribute_names - assert_equal ["id", "type", "ruby_type", "firm_id", "firm_name", "name", "client_of", "rating", "account_id"], + assert_equal ["id", "type", "ruby_type", "firm_id", "firm_name", "name", "client_of", "rating", "account_id", "description"], Company.attribute_names end diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index 32982e4f82..1ad4d971f4 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -176,6 +176,7 @@ ActiveRecord::Schema.define do t.integer :client_of t.integer :rating, :default => 1 t.integer :account_id + t.string :description, :null => false, :default => "" end add_index :companies, [:firm_id, :type, :rating, :ruby_type], :name => "company_index" -- cgit v1.2.3