aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorCarlos Antonio da Silva <carlosantoniodasilva@gmail.com>2013-02-03 04:30:14 -0800
committerCarlos Antonio da Silva <carlosantoniodasilva@gmail.com>2013-02-03 04:30:14 -0800
commitb4f189a162f4280b360dbc2a6635bbff6c6f09bc (patch)
tree638e165ad8490056f378d1715e4b01b88d1e2be8 /activerecord
parentbf794bb36f3bd06e2e863cd6c6dfb6ceae116af2 (diff)
parente8357935acde972fe42a1a3e0d8c969e518956ed (diff)
downloadrails-b4f189a162f4280b360dbc2a6635bbff6c6f09bc.tar.gz
rails-b4f189a162f4280b360dbc2a6635bbff6c6f09bc.tar.bz2
rails-b4f189a162f4280b360dbc2a6635bbff6c6f09bc.zip
Merge pull request #9159 from lexmag/col_defaults-dup-fix
Backport: Duplicate column_defaults properly
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG.md6
-rw-r--r--activerecord/lib/active_record/base.rb3
-rw-r--r--activerecord/test/cases/base_test.rb8
-rw-r--r--activerecord/test/schema/schema.rb1
4 files changed, 16 insertions, 2 deletions
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..ab62fb82b6 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, :default => ""
end
add_index :companies, [:firm_id, :type, :rating, :ruby_type], :name => "company_index"