diff --git a/lib/noosfero/core_ext/active_record/reflection.rb b/lib/noosfero/core_ext/active_record/reflection.rb index dd9a113..1eb2b3c 100644 --- a/lib/noosfero/core_ext/active_record/reflection.rb +++ b/lib/noosfero/core_ext/active_record/reflection.rb @@ -1,20 +1,28 @@ - # on STI classes tike Article and Profile, plugins' extensions # on associations should be reflected on descendants module ActiveRecord - module Reflection + module Associations + def association(name) #:nodoc: + association = association_instance_get(name) - class << self + if association.nil? + reflection = self.class._reflect_on_association(name) - def add_reflection_with_descendants(ar, name, reflection) - self.add_reflection_without_descendants ar, name, reflection - ar.descendants.each do |k| - k._reflections.merge!(name.to_s => reflection) - end if ar.base_class == ar - end + # Check inheritance chain for possible upstream association + klass = self.class.superclass + while reflection.nil? && klass.respond_to?(:_reflect_on_association) + reflection = klass._reflect_on_association(name) + klass = klass.superclass + end - alias_method_chain :add_reflection, :descendants + unless reflection + raise AssociationNotFoundError.new(self, name) + end + association = reflection.association_class.new(self, reflection) + association_instance_set(name, association) + end + association end end end -- libgit2 0.21.2