Skip to content

Commit

Permalink
Support attribute aliases for type_for_attribute
Browse files Browse the repository at this point in the history
Similar with #39496.
  • Loading branch information
kamipo committed Jun 2, 2020
1 parent a7c1873 commit 97b8aeb
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 2 deletions.
2 changes: 2 additions & 0 deletions activerecord/lib/active_record/model_schema.rb
Expand Up @@ -381,6 +381,8 @@ def yaml_encoder # :nodoc:
# a string or a symbol.
def type_for_attribute(attr_name, &block)
attr_name = attr_name.to_s
attr_name = attribute_aliases[attr_name] || attr_name

if block
attribute_types.fetch(attr_name, &block)
else
Expand Down
3 changes: 1 addition & 2 deletions activerecord/lib/active_record/relation.rb
Expand Up @@ -344,12 +344,11 @@ def cache_version(timestamp_column = :updated_at)

def compute_cache_version(timestamp_column) # :nodoc:
timestamp_column = timestamp_column.to_s
timestamp_column = klass.attribute_aliases[timestamp_column] || timestamp_column

if loaded? || distinct_value
size = records.size
if size > 0
timestamp = records.map { |record| record._read_attribute(timestamp_column) }.max
timestamp = records.map { |record| record.read_attribute(timestamp_column) }.max
end
else
collection = eager_loading? ? apply_join_dependency : self
Expand Down
2 changes: 2 additions & 0 deletions activerecord/test/cases/reflection_test.rb
Expand Up @@ -69,6 +69,8 @@ def test_column_string_type_and_limit
assert_equal :string, @first.column_for_attribute(:title).type
assert_equal :string, @first.type_for_attribute("title").type
assert_equal :string, @first.type_for_attribute(:title).type
assert_equal :string, @first.type_for_attribute("heading").type
assert_equal :string, @first.type_for_attribute(:heading).type
assert_equal 250, @first.column_for_attribute("title").limit
end

Expand Down

0 comments on commit 97b8aeb

Please sign in to comment.