Merge pull request #51948 from justinko/issue-51938

Restore inferred association class with the same modularized name
This commit is contained in:
Jean Boussier 2024-07-08 17:53:03 +02:00 committed by GitHub
commit 78ce2994b2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 26 additions and 6 deletions

@ -428,17 +428,21 @@ def autosave=(autosave)
# a new association object. Use +build_association+ or +create_association+
# instead. This allows plugins to hook into association object creation.
def klass
@klass ||= compute_class(compute_name(class_name))
@klass ||= _klass(class_name)
end
def _klass(class_name) # :nodoc:
if active_record.name.demodulize == class_name
return compute_class("::#{class_name}") rescue NameError
end
compute_class(class_name)
end
def compute_class(name)
name.constantize
end
def compute_name(name) # :nodoc:
active_record.name.demodulize == name ? "::#{name}" : name
end
# Returns +true+ if +self+ and +other_aggregation+ have the same +name+ attribute, +active_record+ attribute,
# and +other_aggregation+ has an options hash assigned to it.
def ==(other_aggregation)
@ -986,7 +990,7 @@ def through_reflection?
end
def klass
@klass ||= delegate_reflection.compute_class(compute_name(class_name))
@klass ||= delegate_reflection._klass(class_name)
end
# Returns the source of the through reflection. It checks both a singularized

@ -208,6 +208,18 @@ def test_reflection_klass_with_same_demodularized_different_modularized_name
assert_equal Nested::User, reflection.klass
end
def test_reflection_klass_with_same_modularized_name
reflection = ActiveRecord::Reflection.create(
:has_many,
:nested_users,
nil,
{},
Nested::NestedUser
)
assert_equal Nested::NestedUser, reflection.klass
end
def test_aggregation_reflection
reflection_for_address = AggregateReflection.new(
:address, nil, { mapping: [ %w(address_street street), %w(address_city city), %w(address_country country) ] }, Customer

@ -28,4 +28,8 @@ module Nested
class User < ActiveRecord::Base
self.table_name = "users"
end
class NestedUser < ActiveRecord::Base
has_many :nested_users
end
end