diff options
author | Chris Holmes <tochrisholmes@gmail.com> | 2016-12-28 19:15:49 +0000 |
---|---|---|
committer | Chris Holmes <tochrisholmes@gmail.com> | 2017-01-04 11:56:08 +0000 |
commit | f8ab3ae18fbb5c4a4c9563296d0e7c528e754c42 (patch) | |
tree | 6cf3de11fe91e202811009251f1666b643c7100d /activerecord/lib | |
parent | d304aefc91b485176b3d2fdc2f24147c1f78c132 (diff) | |
download | rails-f8ab3ae18fbb5c4a4c9563296d0e7c528e754c42.tar.gz rails-f8ab3ae18fbb5c4a4c9563296d0e7c528e754c42.tar.bz2 rails-f8ab3ae18fbb5c4a4c9563296d0e7c528e754c42.zip |
Raise error when has_many through is defined before through association
https://github.com/rails/rails/issues/26834
This change raises an error if a has_many through association
is defined before the through association.
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/associations.rb | 10 | ||||
-rw-r--r-- | activerecord/lib/active_record/reflection.rb | 8 |
2 files changed, 18 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 1db5fc0dd1..c05a6c87df 100644 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -97,6 +97,16 @@ module ActiveRecord end end + class HasManyThroughOrderError < ActiveRecordError #:nodoc: + def initialize(owner_class_name = nil, reflection = nil, through_reflection = nil) + if owner_class_name && reflection && through_reflection + super("Cannot have a has_many :through association '#{owner_class_name}##{reflection.name}' which goes through '#{owner_class_name}##{through_reflection.name}' before the through association is defined.") + else + super("Cannot have a has_many :through association before the through association is defined.") + end + end + end + class ThroughCantAssociateThroughHasOneOrManyReflection < ActiveRecordError #:nodoc: def initialize(owner = nil, reflection = nil) if owner && reflection diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb index f3e81ee1e2..a0016f0735 100644 --- a/activerecord/lib/active_record/reflection.rb +++ b/activerecord/lib/active_record/reflection.rb @@ -931,6 +931,14 @@ module ActiveRecord raise HasOneThroughCantAssociateThroughCollection.new(active_record.name, self, through_reflection) end + if parent_reflection.nil? + reflections = active_record.reflections.keys.map(&:to_sym) + + if reflections.index(through_reflection.name) > reflections.index(name) + raise HasManyThroughOrderError.new(active_record.name, self, through_reflection) + end + end + check_validity_of_inverse! end |