def traverse_association(*associations)
scope = scoped({})
associations.each do |association|
reflection = scope.reflect_on_association(association) or raise UnknownAssociation, "Could not find association: #{self.name}##{association}"
foreign_key = reflection.respond_to?(:foreign_key) ? reflection.foreign_key : reflection.primary_key_name
raise NotImplementedError if reflection.options[:conditions] or (reflection.respond_to?(:scope) && reflection.scope)
if reflection.macro == :belongs_to
ids = scope.collect_column(foreign_key, distinct: true)
scope = EdgeRider::Util.exclusive_query(reflection.klass, id: ids)
elsif reflection.macro == :has_many || reflection.macro == :has_one
if reflection.through_reflection
scope = scope.traverse_association(reflection.through_reflection.name, reflection.source_reflection.name)
else
conditions = {}
conditions[reflection.type] = self.name if reflection.type.present?
conditions[foreign_key] = scope.collect_ids
scope = EdgeRider::Util.exclusive_query(reflection.klass, conditions)
end
else
raise UnsupportedAssociation, "Unsupport association type: #{reflection.macro}"
end
end
scope
end