Skip to content
Browse files

Add has_and_belongs_to_many back to reflections hash.

This adds back `has_and_belongs_to_many` reflection to the reflections
hash. Even thought we are not using that internally, as
`Model.reflections` is a public method we need to keep the hash of
reflections coherent.

ad7b5ef is the commit that made
has_and_belongs_to_many became a has_many :through and a has_many.

[fixes #14682]
  • Loading branch information...
1 parent 78169e1 commit bb6861d4b74a34ac07be851ded2c53ec5a0d2ab9 @arthurnn arthurnn committed
View
3 activerecord/lib/active_record/associations.rb
@@ -1606,6 +1606,9 @@ def destroy_associations
end
has_many name, scope, hm_options, &extension
+
+ reflection = ActiveRecord::Reflection::AssociationReflection.new(:has_and_belongs_to_many, name, scope, options, self)
+ self.reflections = self.reflections.except(middle_reflection.name).merge!(name => reflection)
end
end
end
View
7 activerecord/lib/active_record/reflection.rb
@@ -4,8 +4,10 @@ module Reflection # :nodoc:
extend ActiveSupport::Concern
included do
+ class_attribute :_reflections
class_attribute :reflections
class_attribute :aggregate_reflections
+ self._reflections = {}
self.reflections = {}
self.aggregate_reflections = {}
end
@@ -22,6 +24,7 @@ def self.create(macro, name, scope, options, ar)
end
def self.add_reflection(ar, name, reflection)
+ ar._reflections = ar._reflections.merge(name => reflection)
ar.reflections = ar.reflections.merge(name => reflection)
end
@@ -62,7 +65,7 @@ def reflect_on_aggregation(aggregation)
# Account.reflect_on_all_associations(:has_many) # returns an array of all has_many associations
#
def reflect_on_all_associations(macro = nil)
- association_reflections = reflections.values
+ association_reflections = :has_and_belongs_to_many == macro ? reflections.values : _reflections.values
macro ? association_reflections.select { |reflection| reflection.macro == macro } : association_reflections
end
@@ -77,7 +80,7 @@ def reflect_on_association(association)
# Returns an array of AssociationReflection objects for all associations which have <tt>:autosave</tt> enabled.
def reflect_on_all_autosave_associations
- reflections.values.select { |reflection| reflection.options[:autosave] }
+ _reflections.values.select { |reflection| reflection.options[:autosave] }
end
end
View
7 activerecord/test/cases/reflection_test.rb
@@ -200,7 +200,12 @@ def test_association_reflection_in_modules
end
def test_reflection_should_not_raise_error_when_compared_to_other_object
- assert_nothing_raised { Firm.reflections[:clients] == Object.new }
+ assert_nothing_raised { Firm._reflections[:clients] == Object.new }
+ end
+
+ def test_has_and_belongs_to_many_reflection
+ assert_equal :has_and_belongs_to_many, Category.reflections[:posts].macro
+ assert_equal :posts, Category.reflect_on_all_associations(:has_and_belongs_to_many).first.name
end
def test_has_many_through_reflection

0 comments on commit bb6861d

Please sign in to comment.
Something went wrong with that request. Please try again.