diff --git a/activemodel/lib/active_model/attributes.rb b/activemodel/lib/active_model/attributes.rb index 6048ff0113411e583c57fbbe33ceb09d32508f2f..d176ea88d0beb55f0d1d17d9b69a5239f2305bbd 100644 --- a/activemodel/lib/active_model/attributes.rb +++ b/activemodel/lib/active_model/attributes.rb @@ -116,22 +116,17 @@ def attribute_names private def write_attribute(attr_name, value) - name = if self.class.attribute_alias?(attr_name) - self.class.attribute_alias(attr_name).to_s - else - attr_name.to_s - end + name = attr_name.to_s + name = self.class.attribute_aliases[name] || name @attributes.write_from_user(name, value) value end def attribute(attr_name) - name = if self.class.attribute_alias?(attr_name) - self.class.attribute_alias(attr_name).to_s - else - attr_name.to_s - end + name = attr_name.to_s + name = self.class.attribute_aliases[name] || name + @attributes.fetch_value(name) end diff --git a/activerecord/lib/active_record/attribute_methods/dirty.rb b/activerecord/lib/active_record/attribute_methods/dirty.rb index 444568562b8c1669cc16e07613e1624ad86193d8..a43ebdf60db5ae3f2833baa415dac186f932b9b2 100644 --- a/activerecord/lib/active_record/attribute_methods/dirty.rb +++ b/activerecord/lib/active_record/attribute_methods/dirty.rb @@ -167,12 +167,8 @@ def mutations_before_last_save end def write_attribute_without_type_cast(attr_name, value) - name = attr_name.to_s - if self.class.attribute_alias?(name) - name = self.class.attribute_alias(name) - end - result = super(name, value) - clear_attribute_change(name) + result = super + clear_attribute_change(attr_name) result end diff --git a/activerecord/lib/active_record/attribute_methods/read.rb b/activerecord/lib/active_record/attribute_methods/read.rb index c787fb6a94973c001dfdea28b5e21c07da89f483..0562327a9ad2e396f51bff89bdc164eb0b6feaca 100644 --- a/activerecord/lib/active_record/attribute_methods/read.rb +++ b/activerecord/lib/active_record/attribute_methods/read.rb @@ -27,9 +27,7 @@ def #{temp_method_name} # to a date object, like Date.new(2004, 12, 12)). def read_attribute(attr_name, &block) name = attr_name.to_s - if self.class.attribute_alias?(name) - name = self.class.attribute_alias(name) - end + name = self.class.attribute_aliases[name] || name name = @primary_key if name == "id" && @primary_key _read_attribute(name, &block) diff --git a/activerecord/lib/active_record/attribute_methods/write.rb b/activerecord/lib/active_record/attribute_methods/write.rb index 6a54bd2fc28fd614fe3055dc9f2c5333201f1f5e..1c63b553d02ce4f899f42aab4a1d3ff6a36d2b6b 100644 --- a/activerecord/lib/active_record/attribute_methods/write.rb +++ b/activerecord/lib/active_record/attribute_methods/write.rb @@ -31,9 +31,7 @@ def #{temp_method_name}(value) # turned into +nil+. def write_attribute(attr_name, value) name = attr_name.to_s - if self.class.attribute_alias?(name) - name = self.class.attribute_alias(name) - end + name = self.class.attribute_aliases[name] || name name = @primary_key if name == "id" && @primary_key _write_attribute(name, value) diff --git a/activerecord/lib/active_record/core.rb b/activerecord/lib/active_record/core.rb index 28bd7f8cdc8b1f8d8186976f906cdcbcd94d785e..dfd33d3dd76eab66c334fed434540375640c8d23 100644 --- a/activerecord/lib/active_record/core.rb +++ b/activerecord/lib/active_record/core.rb @@ -268,7 +268,8 @@ def arel_table # :nodoc: end def arel_attribute(name, table = arel_table) # :nodoc: - name = attribute_alias(name) if attribute_alias?(name) + name = name.to_s + name = attribute_aliases[name] || name table[name] end diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb index 1ff7ee4d894e5b786faa3c8f7c59ce95eb2dd9ed..adfd56469573d5220ef5e6c9025579a8d251e622 100644 --- a/activerecord/lib/active_record/persistence.rb +++ b/activerecord/lib/active_record/persistence.rb @@ -664,8 +664,13 @@ def update_columns(attributes) raise ActiveRecordError, "cannot update a new record" if new_record? raise ActiveRecordError, "cannot update a destroyed record" if destroyed? + attributes = attributes.transform_keys do |key| + name = key.to_s + self.class.attribute_aliases[name] || name + end + attributes.each_key do |key| - verify_readonly_attribute(key.to_s) + verify_readonly_attribute(key) end id_in_database = self.id_in_database @@ -853,7 +858,7 @@ def touch(*names, time: nil) attribute_names = timestamp_attributes_for_update_in_model attribute_names |= names.map!(&:to_s).map! { |name| - self.class.attribute_alias?(name) ? self.class.attribute_alias(name) : name + self.class.attribute_aliases[name] || name } unless attribute_names.empty? diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index f30428d0a54273c4f8031c0968ccd891207c083b..c03ca7f1e7e95a1b3a32df636b151e0244ddfce6 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -1178,7 +1178,7 @@ def arel_columns(columns) end def arel_column(field) - field = klass.attribute_alias(field) if klass.attribute_alias?(field) + field = klass.attribute_aliases[field] || field from = from_clause.name || from_clause.value if klass.columns_hash.key?(field) && (!from || table_name_matches?(from)) diff --git a/activerecord/lib/active_record/table_metadata.rb b/activerecord/lib/active_record/table_metadata.rb index b67479fb6a335e6f65a4ec431ab2b8f0ba012eba..073866b8946989d581a0144c3106a3288a7f8803 100644 --- a/activerecord/lib/active_record/table_metadata.rb +++ b/activerecord/lib/active_record/table_metadata.rb @@ -12,9 +12,9 @@ def initialize(klass, arel_table, association = nil) def resolve_column_aliases(hash) new_hash = hash.dup - hash.each do |key, _| - if (key.is_a?(Symbol)) && klass.attribute_alias?(key) - new_hash[klass.attribute_alias(key)] = new_hash.delete(key) + hash.each_key do |key| + if key.is_a?(Symbol) && new_key = klass.attribute_aliases[key.to_s] + new_hash[new_key] = new_hash.delete(key) end end new_hash diff --git a/activerecord/test/models/post.rb b/activerecord/test/models/post.rb index c34968590faaff68dddfea5b696c0db4cbd024ee..090b576a5d0e0319459f2c20263d63c0258fd865 100644 --- a/activerecord/test/models/post.rb +++ b/activerecord/test/models/post.rb @@ -323,8 +323,8 @@ def table_name "posts" end - def attribute_alias?(name) - false + def attribute_aliases + {} end def sanitize_sql(sql)