Skip to content
Browse files

Merge pull request #9159 from lexmag/col_defaults-dup-fix

Backport: Duplicate column_defaults properly
  • Loading branch information...
2 parents bf794bb + e835793 commit b4f189a162f4280b360dbc2a6635bbff6c6f09bc @carlosantoniodasilva carlosantoniodasilva committed Feb 3, 2013
View
6 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.
View
3 activerecord/lib/active_record/base.rb
@@ -479,7 +479,8 @@ def relation #: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 = {}
View
8 activerecord/test/cases/base_test.rb
@@ -325,6 +325,12 @@ def test_first_or_initialize
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 @@ def test_marshalling_with_associations
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
View
1 activerecord/test/schema/schema.rb
@@ -176,6 +176,7 @@ def create_table(*args, &block)
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"

0 comments on commit b4f189a

Please sign in to comment.
Something went wrong with that request. Please try again.