aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/attribute_methods
diff options
context:
space:
mode:
authorMatthew Draper <matthew@trebex.net>2017-07-09 03:07:08 +0930
committerGitHub <noreply@github.com>2017-07-09 03:07:08 +0930
commit5c17e34946457b9af428bcf955eb65493257dc3e (patch)
treebc76a1000911cf0e9ddb89d02bf129ab06101425 /activerecord/lib/active_record/attribute_methods
parent1990c8f9b63aaa3b766c8ddf745f25cf8822b332 (diff)
parentdff37ff613d12201c2b770dbc61b125792c7bb6e (diff)
downloadrails-5c17e34946457b9af428bcf955eb65493257dc3e.tar.gz
rails-5c17e34946457b9af428bcf955eb65493257dc3e.tar.bz2
rails-5c17e34946457b9af428bcf955eb65493257dc3e.zip
Merge pull request #29495 from eugeneius/_write_attribute
Improve the performance of writing attributes
Diffstat (limited to 'activerecord/lib/active_record/attribute_methods')
-rw-r--r--activerecord/lib/active_record/attribute_methods/dirty.rb2
-rw-r--r--activerecord/lib/active_record/attribute_methods/primary_key.rb2
-rw-r--r--activerecord/lib/active_record/attribute_methods/write.rb20
3 files changed, 15 insertions, 9 deletions
diff --git a/activerecord/lib/active_record/attribute_methods/dirty.rb b/activerecord/lib/active_record/attribute_methods/dirty.rb
index 948249a6fd..8c58e63931 100644
--- a/activerecord/lib/active_record/attribute_methods/dirty.rb
+++ b/activerecord/lib/active_record/attribute_methods/dirty.rb
@@ -80,7 +80,7 @@ module ActiveRecord
clear_mutation_trackers
end
- def raw_write_attribute(attr_name, *)
+ def write_attribute_without_type_cast(attr_name, *)
result = super
clear_attribute_change(attr_name)
result
diff --git a/activerecord/lib/active_record/attribute_methods/primary_key.rb b/activerecord/lib/active_record/attribute_methods/primary_key.rb
index b9b2acff37..081aad434d 100644
--- a/activerecord/lib/active_record/attribute_methods/primary_key.rb
+++ b/activerecord/lib/active_record/attribute_methods/primary_key.rb
@@ -21,7 +21,7 @@ module ActiveRecord
# Sets the primary key value.
def id=(value)
sync_with_transaction_state
- write_attribute(self.class.primary_key, value) if self.class.primary_key
+ _write_attribute(self.class.primary_key, value) if self.class.primary_key
end
# Queries the primary key value.
diff --git a/activerecord/lib/active_record/attribute_methods/write.rb b/activerecord/lib/active_record/attribute_methods/write.rb
index 75c5a1a600..54b673c72e 100644
--- a/activerecord/lib/active_record/attribute_methods/write.rb
+++ b/activerecord/lib/active_record/attribute_methods/write.rb
@@ -17,7 +17,7 @@ module ActiveRecord
generated_attribute_methods.module_eval <<-STR, __FILE__, __LINE__ + 1
def __temp__#{safe_name}=(value)
name = ::ActiveRecord::AttributeMethods::AttrNames::ATTR_#{safe_name}
- write_attribute(name, value)
+ _write_attribute(name, value)
end
alias_method #{(name + '=').inspect}, :__temp__#{safe_name}=
undef_method :__temp__#{safe_name}=
@@ -36,20 +36,26 @@ module ActiveRecord
end
name = self.class.primary_key if name == "id".freeze && self.class.primary_key
- @attributes.write_from_user(name, value)
- value
+ _write_attribute(name, value)
end
- def raw_write_attribute(attr_name, value) # :nodoc:
- name = attr_name.to_s
- @attributes.write_cast_value(name, value)
+ # This method exists to avoid the expensive primary_key check internally, without
+ # breaking compatibility with the write_attribute API
+ def _write_attribute(attr_name, value) # :nodoc:
+ @attributes.write_from_user(attr_name.to_s, value)
value
end
private
+ def write_attribute_without_type_cast(attr_name, value)
+ name = attr_name.to_s
+ @attributes.write_cast_value(name, value)
+ value
+ end
+
# Handle *= for method_missing.
def attribute=(attribute_name, value)
- write_attribute(attribute_name, value)
+ _write_attribute(attribute_name, value)
end
end
end