From cdfa4a5f869b42fc3894e777d94b976ece28f716 Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Sun, 28 Oct 2012 13:31:34 -0700 Subject: Merge pull request #8053 from henrik/update_columns_with_primary_key Unbreak update_column/update_columns for the primary key attribute. Conflicts: activerecord/CHANGELOG.md activerecord/lib/active_record/persistence.rb activerecord/test/cases/persistence_test.rb --- activerecord/CHANGELOG.md | 4 ++++ activerecord/lib/active_record/persistence.rb | 6 +++++- activerecord/test/cases/persistence_test.rb | 8 ++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) (limited to 'activerecord') diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 8c55c60769..aa68934099 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,5 +1,9 @@ ## Rails 3.2.9 (unreleased) +* Fix bug where `update_columns` and `update_column` would not let you update the primary key column. + + *Henrik Nyh* + * Decode URI encoded attributes on database connection URLs. *Shawn Veader* diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb index 8a3c3fed7d..d492ac7f16 100644 --- a/activerecord/lib/active_record/persistence.rb +++ b/activerecord/lib/active_record/persistence.rb @@ -193,8 +193,12 @@ module ActiveRecord name = name.to_s raise ActiveRecordError, "#{name} is marked as readonly" if self.class.readonly_attributes.include?(name) raise ActiveRecordError, "can not update on a new record object" unless persisted? + + updated_count = self.class.update_all({ name => value }, self.class.primary_key => id) == 1 + raw_write_attribute(name, value) - self.class.update_all({ name => value }, self.class.primary_key => id) == 1 + + updated_count == 1 end # Updates the attributes of the model from the passed-in hash and saves the diff --git a/activerecord/test/cases/persistence_test.rb b/activerecord/test/cases/persistence_test.rb index e4b8caae52..66f884287d 100644 --- a/activerecord/test/cases/persistence_test.rb +++ b/activerecord/test/cases/persistence_test.rb @@ -505,6 +505,14 @@ class PersistencesTest < ActiveRecord::TestCase assert_equal 'super_title', t.title end + def test_update_column_changing_id + topic = Topic.find(1) + topic.update_column("id", 123) + assert_equal 123, topic.id + topic.reload + assert_equal 123, topic.id + end + def test_update_attributes topic = Topic.find(1) assert !topic.approved? -- cgit v1.2.3