From 16b70fddd4dc7e7fb7be108add88bae6e3c2509b Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Thu, 13 Jun 2013 10:09:50 -0700 Subject: [PATCH] push ambiguous reflection warning down to reflection name calculation --- activerecord/lib/active_record/reflection.rb | 51 +++++++++++--------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb index 8b7f15882a..98c469f2f3 100644 --- a/activerecord/lib/active_record/reflection.rb +++ b/activerecord/lib/active_record/reflection.rb @@ -501,7 +501,7 @@ class ThroughReflection < AssociationReflection #:nodoc: def initialize(macro, name, scope, options, active_record) super - @source_reflection = nil + @source_reflection_name = options[:source] end # Returns the source of the through reflection. It checks both a singularized @@ -522,27 +522,7 @@ def initialize(macro, name, scope, options, active_record) # # => # def source_reflection - return @source_reflection if @source_reflection - - reflections = source_reflection_names.collect { |name| - through_reflection.klass.reflect_on_association(name) - }.compact - - if reflections.length > 1 - example_options = options.dup - example_options[:source] = source_reflection_names.first - ActiveSupport::Deprecation.warn <<-eowarn -Ambiguous source reflection for through association. Please specify a :source -directive on your declaration like: - - class #{active_record.name} < ActiveRecord::Base - #{macro} :#{name}, #{example_options} - end - - eowarn - end - - @source_reflection = reflections.first + through_reflection.klass.reflect_on_association(source_reflection_name) end # Returns the AssociationReflection object specified in the :through option @@ -660,7 +640,32 @@ def association_primary_key(klass = nil) # # => [:tag, :tags] # def source_reflection_names - @source_reflection_names ||= (options[:source] ? [options[:source]] : [name.to_s.singularize, name]).collect { |n| n.to_sym }.uniq + (options[:source] ? [options[:source]] : [name.to_s.singularize, name]).collect { |n| n.to_sym }.uniq + end + + def source_reflection_name # :nodoc: + return @source_reflection_name if @source_reflection_name + + names = [name.to_s.singularize, name].collect { |n| n.to_sym }.uniq + names = names.find_all { |n| + through_reflection.klass.reflect_on_association(n) + } + + if names.length > 1 + example_options = options.dup + example_options[:source] = source_reflection_names.first + ActiveSupport::Deprecation.warn <<-eowarn +Ambiguous source reflection for through association. Please specify a :source +directive on your declaration like: + + class #{active_record.name} < ActiveRecord::Base + #{macro} :#{name}, #{example_options} + end + + eowarn + end + + @source_reflection_name = names.first end def source_options