Skip to content

Commit 0944182

Browse files
committed
Raise when a through association has an ambiguous reflection name
1 parent 249f71a commit 0944182

File tree

4 files changed

+27
-10
lines changed

4 files changed

+27
-10
lines changed

activerecord/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Raises when a through association has an ambiguous reflection name.
2+
3+
*Rafael Mendonça França*
4+
15
* Raises when `ActiveRecord::Migration` is inherited directly.
26

37
*Rafael Mendonça França*

activerecord/lib/active_record/associations.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,21 @@ def initialize(owner = nil, reflection = nil)
107107
end
108108
end
109109

110+
class AmbiguousSourceReflectionForThroughAssociation < ActiveRecordError # :nodoc:
111+
def initialize(klass, macro, association_name, options, possible_sources)
112+
example_options = options.dup
113+
example_options[:source] = possible_sources.first
114+
115+
super("Ambiguous source reflection for through association. Please " \
116+
"specify a :source directive on your declaration like:\n" \
117+
"\n" \
118+
" class #{klass} < ActiveRecord::Base\n" \
119+
" #{macro} :#{association_name}, #{example_options}\n" \
120+
" end"
121+
)
122+
end
123+
end
124+
110125
class HasManyThroughCantAssociateThroughHasOneOrManyReflection < ThroughCantAssociateThroughHasOneOrManyReflection #:nodoc:
111126
end
112127

activerecord/lib/active_record/reflection.rb

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -878,15 +878,13 @@ def source_reflection_name # :nodoc:
878878
}
879879

880880
if names.length > 1
881-
example_options = options.dup
882-
example_options[:source] = source_reflection_names.first
883-
ActiveSupport::Deprecation.warn \
884-
"Ambiguous source reflection for through association. Please " \
885-
"specify a :source directive on your declaration like:\n" \
886-
"\n" \
887-
" class #{active_record.name} < ActiveRecord::Base\n" \
888-
" #{macro} :#{name}, #{example_options}\n" \
889-
" end"
881+
raise AmbiguousSourceReflectionForThroughAssociation.new(
882+
active_record.name,
883+
macro,
884+
name,
885+
options,
886+
source_reflection_names
887+
)
890888
end
891889

892890
@source_reflection_name = names.first

activerecord/test/cases/errors_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ def test_can_be_instantiated_with_no_args
55
base = ActiveRecord::ActiveRecordError
66
error_klasses = ObjectSpace.each_object(Class).select { |klass| klass < base }
77

8-
error_klasses.each do |error_klass|
8+
(error_klasses - [ActiveRecord::AmbiguousSourceReflectionForThroughAssociation]).each do |error_klass|
99
begin
1010
error_klass.new.inspect
1111
rescue ArgumentError

0 commit comments

Comments
 (0)