diff --git a/activerecord/lib/active_record/associations/association_scope.rb b/activerecord/lib/active_record/associations/association_scope.rb index f1a3b23d5ab3ae6187ed5ae22c53029dd9f5a8b6..572f556999dd1d5984108400afed30da9a38453f 100644 --- a/activerecord/lib/active_record/associations/association_scope.rb +++ b/activerecord/lib/active_record/associations/association_scope.rb @@ -47,15 +47,8 @@ def join_type def self.get_bind_values(owner, chain) bvs = [] chain.each_with_index do |reflection, i| - if reflection.source_macro == :belongs_to - foreign_key = reflection.foreign_key - else - foreign_key = reflection.active_record_primary_key - end - if reflection == chain.last - bvs << owner[foreign_key] - + bvs << reflection.join_id_for(owner) if reflection.type bvs << owner.class.base_class.name end diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb index 95485ddadac8207f074a91e9876d4ac71723e90b..7f4d77849af5c75b0b874710c06f4072f2797129 100644 --- a/activerecord/lib/active_record/reflection.rb +++ b/activerecord/lib/active_record/reflection.rb @@ -278,6 +278,11 @@ def check_validity_of_inverse! end end + def join_id_for(owner) #:nodoc: + key = (source_macro == :belongs_to) ? foreign_key : active_record_primary_key + owner[key] + end + def through_reflection nil end