aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2014-08-06 23:44:00 -0300
committerRafael Mendonça França <rafaelmfranca@gmail.com>2014-08-06 23:46:31 -0300
commite6e81f856e969efda49166af82c27594f30ea592 (patch)
tree9c227904ca43765c03582d4c25e572b3e790ad55 /activerecord
parentee255794b348e066bdaf60ccb583a1de9c84db6d (diff)
downloadrails-e6e81f856e969efda49166af82c27594f30ea592.tar.gz
rails-e6e81f856e969efda49166af82c27594f30ea592.tar.bz2
rails-e6e81f856e969efda49166af82c27594f30ea592.zip
Define id_was to get the previous value of the primary key
Currently when we call id_was and we have a custom primary key name Active Record will return the current value of the primary key. This make impossible to correctly do an update operation if you change the id. Fixes #16413
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG.md11
-rw-r--r--activerecord/lib/active_record/attribute_methods/primary_key.rb8
-rw-r--r--activerecord/test/cases/primary_keys_test.rb10
3 files changed, 28 insertions, 1 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index c7e8b264fb..4b70d883fe 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -1,3 +1,14 @@
+* Define `id_was` to get the previous value of the primary key.
+
+ Currently when we call id_was and we have a custom primary key name
+ Active Record will return the current value of the primary key. This
+ make impossible to correctly do an update operation if you change the
+ id.
+
+ Fixes #16413.
+
+ *Rafael Mendonça França*
+
* Deprecate `DatabaseTasks.load_schema` to act on the current connection.
Use `.load_schema_current` instead. In the future `load_schema` will
require the `configuration` to act on as an argument.
diff --git a/activerecord/lib/active_record/attribute_methods/primary_key.rb b/activerecord/lib/active_record/attribute_methods/primary_key.rb
index cadad60ddd..9bd333bbac 100644
--- a/activerecord/lib/active_record/attribute_methods/primary_key.rb
+++ b/activerecord/lib/active_record/attribute_methods/primary_key.rb
@@ -39,6 +39,12 @@ module ActiveRecord
read_attribute_before_type_cast(self.class.primary_key)
end
+ # Returns the primary key previous value.
+ def id_was
+ sync_with_transaction_state
+ attribute_was(self.class.primary_key)
+ end
+
protected
def attribute_method?(attr_name)
@@ -54,7 +60,7 @@ module ActiveRecord
end
end
- ID_ATTRIBUTE_METHODS = %w(id id= id? id_before_type_cast).to_set
+ ID_ATTRIBUTE_METHODS = %w(id id= id? id_before_type_cast id_was).to_set
def dangerous_attribute_method?(method_name)
super && !ID_ATTRIBUTE_METHODS.include?(method_name)
diff --git a/activerecord/test/cases/primary_keys_test.rb b/activerecord/test/cases/primary_keys_test.rb
index b04df7ce43..f19a6ea5e3 100644
--- a/activerecord/test/cases/primary_keys_test.rb
+++ b/activerecord/test/cases/primary_keys_test.rb
@@ -5,6 +5,7 @@ require 'models/subscriber'
require 'models/movie'
require 'models/keyboard'
require 'models/mixed_case_monkey'
+require 'models/dashboard'
class PrimaryKeysTest < ActiveRecord::TestCase
fixtures :topics, :subscribers, :movies, :mixed_case_monkeys
@@ -164,6 +165,15 @@ class PrimaryKeysTest < ActiveRecord::TestCase
MixedCaseMonkey.reset_primary_key
assert_equal "monkeyID", MixedCaseMonkey.primary_key
end
+
+ def test_primary_key_update_with_custom_key_name
+ dashboard = Dashboard.create!(dashboard_id: '1')
+ dashboard.id = '2'
+ dashboard.save!
+
+ dashboard = Dashboard.first
+ assert_equal '2', dashboard.id
+ end
end
class PrimaryKeyWithNoConnectionTest < ActiveRecord::TestCase