diff options
author | Carlos Antonio da Silva <carlosantoniodasilva@gmail.com> | 2012-10-28 13:31:34 -0700 |
---|---|---|
committer | Carlos Antonio da Silva <carlosantoniodasilva@gmail.com> | 2012-10-28 13:31:34 -0700 |
commit | c6f47c198034ed2031723a16069005b321846452 (patch) | |
tree | 6e741da75ee9246fc85ca6bc1c222c7169681690 | |
parent | 5bbe245a51cec029101c844e159ba7f7a7afedab (diff) | |
parent | 1849665f738cf7b3650e508b05380ebb36748f9e (diff) | |
download | rails-c6f47c198034ed2031723a16069005b321846452.tar.gz rails-c6f47c198034ed2031723a16069005b321846452.tar.bz2 rails-c6f47c198034ed2031723a16069005b321846452.zip |
Merge pull request #8053 from henrik/update_columns_with_primary_key
Unbreak update_column/update_columns for the primary key attribute.
-rw-r--r-- | activerecord/CHANGELOG.md | 4 | ||||
-rw-r--r-- | activerecord/lib/active_record/persistence.rb | 4 | ||||
-rw-r--r-- | activerecord/test/cases/persistence_test.rb | 13 |
3 files changed, 20 insertions, 1 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 4916777ce7..684a8736c9 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,5 +1,9 @@ ## Rails 4.0.0 (unreleased) ## +* Fix bug where `update_columns` and `update_column` would not let you update the primary key column. + + *Henrik Nyh* + * The `create_table` method raises an `ArgumentError` when the primary key column is redefined. Fix #6378 diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb index 611d3d97c3..c336e1f5bb 100644 --- a/activerecord/lib/active_record/persistence.rb +++ b/activerecord/lib/active_record/persistence.rb @@ -224,11 +224,13 @@ module ActiveRecord verify_readonly_attribute(key.to_s) end + updated_count = self.class.where(self.class.primary_key => id).update_all(attributes) + attributes.each do |k,v| raw_write_attribute(k,v) end - self.class.where(self.class.primary_key => id).update_all(attributes) == 1 + updated_count == 1 end # Initializes +attribute+ to zero if +nil+ and adds the value passed as +by+ (default is 1). diff --git a/activerecord/test/cases/persistence_test.rb b/activerecord/test/cases/persistence_test.rb index b5f32a57b2..4b938da5c4 100644 --- a/activerecord/test/cases/persistence_test.rb +++ b/activerecord/test/cases/persistence_test.rb @@ -592,6 +592,19 @@ class PersistencesTest < ActiveRecord::TestCase assert_equal 'super_title', t.title end + def test_update_columns_changing_id + topic = Topic.find(1) + topic.update_columns(id: 123) + assert_equal 123, topic.id + topic.reload + assert_equal 123, topic.id + end + + def test_update_columns_returns_boolean + topic = Topic.find(1) + assert_equal true, topic.update_columns(title: "New title") + end + def test_update_attributes topic = Topic.find(1) assert !topic.approved? |