diff options
author | Matthew Draper <matthew@trebex.net> | 2017-08-02 17:21:07 +0930 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-08-02 17:21:07 +0930 |
commit | ac6cd6800a1eb7dc86ff3897cca7434af636ea48 (patch) | |
tree | 15a2751d59260c785c55e9514ee5bee4a5e92b1d /activerecord/lib/active_record | |
parent | f9a43f28c087f8ffd35ff7c33a60c938b60f2be2 (diff) | |
parent | b19c4eff47d912ee3a038a6e0653eea7df5b67b8 (diff) | |
download | rails-ac6cd6800a1eb7dc86ff3897cca7434af636ea48.tar.gz rails-ac6cd6800a1eb7dc86ff3897cca7434af636ea48.tar.bz2 rails-ac6cd6800a1eb7dc86ff3897cca7434af636ea48.zip |
Merge pull request #29724 from eugeneius/sync_primary_key
Sync transaction state when accessing primary key
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r-- | activerecord/lib/active_record/attribute_methods/read.rb | 3 | ||||
-rw-r--r-- | activerecord/lib/active_record/attribute_methods/write.rb | 3 | ||||
-rw-r--r-- | activerecord/lib/active_record/transactions.rb | 4 |
3 files changed, 8 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/attribute_methods/read.rb b/activerecord/lib/active_record/attribute_methods/read.rb index 6adaeb0121..615b2fa701 100644 --- a/activerecord/lib/active_record/attribute_methods/read.rb +++ b/activerecord/lib/active_record/attribute_methods/read.rb @@ -31,9 +31,11 @@ module ActiveRecord temp_method = "__temp__#{safe_name}" ActiveRecord::AttributeMethods::AttrNames.set_name_cache safe_name, name + sync_with_transaction_state = "sync_with_transaction_state" if name == primary_key generated_attribute_methods.module_eval <<-STR, __FILE__, __LINE__ + 1 def #{temp_method} + #{sync_with_transaction_state} name = ::ActiveRecord::AttributeMethods::AttrNames::ATTR_#{safe_name} _read_attribute(name) { |n| missing_attribute(n, caller) } end @@ -57,6 +59,7 @@ module ActiveRecord end name = self.class.primary_key if name == "id".freeze && self.class.primary_key + sync_with_transaction_state if name == self.class.primary_key _read_attribute(name, &block) end diff --git a/activerecord/lib/active_record/attribute_methods/write.rb b/activerecord/lib/active_record/attribute_methods/write.rb index fa01f832ac..62c5ce059b 100644 --- a/activerecord/lib/active_record/attribute_methods/write.rb +++ b/activerecord/lib/active_record/attribute_methods/write.rb @@ -15,10 +15,12 @@ module ActiveRecord def define_method_attribute=(name) safe_name = name.unpack("h*".freeze).first ActiveRecord::AttributeMethods::AttrNames.set_name_cache safe_name, name + sync_with_transaction_state = "sync_with_transaction_state" if name == primary_key generated_attribute_methods.module_eval <<-STR, __FILE__, __LINE__ + 1 def __temp__#{safe_name}=(value) name = ::ActiveRecord::AttributeMethods::AttrNames::ATTR_#{safe_name} + #{sync_with_transaction_state} _write_attribute(name, value) end alias_method #{(name + '=').inspect}, :__temp__#{safe_name}= @@ -38,6 +40,7 @@ module ActiveRecord end name = self.class.primary_key if name == "id".freeze && self.class.primary_key + sync_with_transaction_state if name == self.class.primary_key _write_attribute(name, value) end diff --git a/activerecord/lib/active_record/transactions.rb b/activerecord/lib/active_record/transactions.rb index 3e8a0789df..f91f0cdf12 100644 --- a/activerecord/lib/active_record/transactions.rb +++ b/activerecord/lib/active_record/transactions.rb @@ -432,8 +432,8 @@ module ActiveRecord @new_record = restore_state[:new_record] @destroyed = restore_state[:destroyed] pk = self.class.primary_key - if pk && read_attribute(pk) != restore_state[:id] - write_attribute(pk, restore_state[:id]) + if pk && _read_attribute(pk) != restore_state[:id] + _write_attribute(pk, restore_state[:id]) end freeze if restore_state[:frozen?] end |