Skip to content

Commit

Permalink
Duplicate column_defaults properly
Browse files Browse the repository at this point in the history
Backport c517602 to fix #6115

Deleted:
    activerecord/lib/active_record/core.rb

Conflicts:
    activerecord/test/cases/base_test.rb
  • Loading branch information
drogus authored and olancheg committed Feb 3, 2013
1 parent bf794bb commit 6a0a5f3
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
6 changes: 6 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.

Expand Down
3 changes: 2 additions & 1 deletion activerecord/lib/active_record/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down
8 changes: 7 additions & 1 deletion activerecord/test/cases/base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions activerecord/test/schema/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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, :null => false, :default => ""
end

add_index :companies, [:firm_id, :type, :rating, :ruby_type], :name => "company_index"
Expand Down

0 comments on commit 6a0a5f3

Please sign in to comment.