From 0ceb34295501a797c9e549c581ecee17f837f01c Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Tue, 19 Oct 2010 15:24:30 +0100 Subject: Bugfix/refactoring --- activerecord/lib/active_record/associations.rb | 12 ++++++------ .../active_record/associations/through_association_scope.rb | 10 +++++----- activerecord/lib/active_record/reflection.rb | 9 +++++++++ 3 files changed, 20 insertions(+), 11 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 29f1c7b81d..9e000f2aae 100644 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -2207,18 +2207,18 @@ module ActiveRecord else case reflection.source_reflection.macro when :belongs_to - key = reflection.source_reflection.association_primary_key - foreign_key = reflection.source_reflection.primary_key_name + key = reflection.association_primary_key + foreign_key = reflection.primary_key_name conditions << source_type_conditions(reflection, foreign_table) when :has_many, :has_one - key = reflection.source_reflection.primary_key_name + key = reflection.primary_key_name foreign_key = reflection.source_reflection.active_record_primary_key when :has_and_belongs_to_many table, join_table = table - join_key = reflection.source_reflection.primary_key_name - join_foreign_key = reflection.source_reflection.klass.primary_key + join_key = reflection.primary_key_name + join_foreign_key = reflection.klass.primary_key relation = relation.join(join_table, join_type).on( join_table[join_key]. @@ -2228,7 +2228,7 @@ module ActiveRecord foreign_table = join_table key = reflection.klass.primary_key - foreign_key = reflection.source_reflection.association_foreign_key + foreign_key = reflection.association_foreign_key end end diff --git a/activerecord/lib/active_record/associations/through_association_scope.rb b/activerecord/lib/active_record/associations/through_association_scope.rb index 2b2229f01f..1365851337 100644 --- a/activerecord/lib/active_record/associations/through_association_scope.rb +++ b/activerecord/lib/active_record/associations/through_association_scope.rb @@ -108,8 +108,8 @@ module ActiveRecord when :belongs_to joins << inner_join_sql( right_table_and_alias, - table_aliases[left], left.source_reflection.association_primary_key, - table_aliases[right], left.source_reflection.primary_key_name, + table_aliases[left], left.association_primary_key, + table_aliases[right], left.primary_key_name, source_type_conditions(left), reflection_conditions(right_index) ) @@ -123,7 +123,7 @@ module ActiveRecord joins << inner_join_sql( right_table_and_alias, - table_aliases[left], left.source_reflection.primary_key_name, + table_aliases[left], left.primary_key_name, right_table, left.source_reflection.active_record_primary_key, polymorphic_conditions(left, left.source_reflection), reflection_conditions(right_index) @@ -148,12 +148,12 @@ module ActiveRecord join_table ), left_table, left.klass.primary_key, - join_table, left.source_reflection.association_foreign_key + join_table, left.association_foreign_key ) joins << inner_join_sql( right_table_and_alias, - join_table, left.source_reflection.primary_key_name, + join_table, left.primary_key_name, table_aliases[right], right.klass.primary_key, reflection_conditions(right_index) ) diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb index 6078191773..7ce2bbb8ae 100644 --- a/activerecord/lib/active_record/reflection.rb +++ b/activerecord/lib/active_record/reflection.rb @@ -357,6 +357,8 @@ module ActiveRecord # Holds all the meta-data about a :through association as it was specified # in the Active Record class. class ThroughReflection < AssociationReflection #:nodoc: + delegate :primary_key_name, :association_foreign_key, :to => :source_reflection + # Gets the source of the through reflection. It checks both a singularized # and pluralized form for :belongs_to or :has_many. # @@ -451,6 +453,13 @@ module ActiveRecord def nested? through_reflection_chain.length > 2 end + + # We want to use the klass from this reflection, rather than just delegate straight to + # the source_reflection, because the source_reflection may be polymorphic. We still + # need to respect the source_reflection's :primary_key option, though. + def association_primary_key + @association_primary_key ||= source_reflection.options[:primary_key] || klass.primary_key + end # Gets an array of possible :through source reflection names: # -- cgit v1.2.3