1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Fix where with aliased associations

As the API doc shows, `alias_attribute` is designed to allow us to make
aliases for *attributes*.

https://api.rubyonrails.org/classes/ActiveModel/AttributeMethods/ClassMethods.html#method-i-alias_attribute

However, coincidentally (and historically), `alias_attribute` also
worked for associations.

I've restored the behavior (it was lost in 56f3096) for the
compatibility.

Fixes #40832.
This commit is contained in:
Ryuta Kamizono 2020-12-14 22:08:28 +09:00
parent 0ecf163cc8
commit 97d67908e2
3 changed files with 10 additions and 1 deletions

View file

@ -1106,7 +1106,10 @@ module ActiveRecord
when String, Array
parts = [klass.sanitize_sql(rest.empty? ? opts : [opts, *rest])]
when Hash
opts = opts.stringify_keys
opts = opts.transform_keys do |key|
key = key.to_s
klass.attribute_aliases[key] || key
end
references = PredicateBuilder.references(opts)
self.references_values |= references unless references.empty?

View file

@ -33,6 +33,10 @@ module ActiveRecord
assert_equal [authors(:bob)], Author.joins(:categories).where(categories: categories(:technology))
end
def test_where_with_aliased_association
assert_equal [comments(:does_it_hurt)], Comment.where(entry: posts(:thinking))
end
def test_type_cast_is_not_evaluated_at_relation_build_time
posts = nil

View file

@ -23,6 +23,8 @@ class Comment < ActiveRecord::Base
has_many :children, class_name: "Comment", foreign_key: :parent_id, inverse_of: :parent
belongs_to :parent, class_name: "Comment", counter_cache: :children_count, inverse_of: :children
alias_attribute :entry, :post
enum label: [:default, :child]
class ::OopsError < RuntimeError; end