From 7919d5ca7b11107671a72fa0cfd2adc139954e4e Mon Sep 17 00:00:00 2001 From: chopraanmol1 Date: Tue, 8 Aug 2017 03:23:45 +0530 Subject: Currently if relation object are passed to where condition for has one or has many association wrong set of primary key and foreign key are selected. Changed code to use 'join' primary key and foreign key over 'association' primary key and foreign key. --- .../relation/predicate_builder/association_query_value.rb | 4 ++-- activerecord/lib/active_record/table_metadata.rb | 6 +++++- 2 files changed, 7 insertions(+), 3 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/relation/predicate_builder/association_query_value.rb b/activerecord/lib/active_record/relation/predicate_builder/association_query_value.rb index e64d9fdf2a..27efd49622 100644 --- a/activerecord/lib/active_record/relation/predicate_builder/association_query_value.rb +++ b/activerecord/lib/active_record/relation/predicate_builder/association_query_value.rb @@ -9,7 +9,7 @@ module ActiveRecord end def queries - [associated_table.association_foreign_key.to_s => ids] + [associated_table.association_join_fk.to_s => ids] end # TODO Change this to private once we've dropped Ruby 2.2 support. @@ -30,7 +30,7 @@ module ActiveRecord end def primary_key - associated_table.association_primary_key + associated_table.association_join_keys.key end def convert_to_id(value) diff --git a/activerecord/lib/active_record/table_metadata.rb b/activerecord/lib/active_record/table_metadata.rb index 71351449e1..02e368e708 100644 --- a/activerecord/lib/active_record/table_metadata.rb +++ b/activerecord/lib/active_record/table_metadata.rb @@ -2,7 +2,7 @@ module ActiveRecord class TableMetadata # :nodoc: - delegate :foreign_type, :foreign_key, to: :association, prefix: true + delegate :foreign_type, :foreign_key, :join_keys, to: :association, prefix: true delegate :association_primary_key, to: :association def initialize(klass, arel_table, association = nil) @@ -66,6 +66,10 @@ module ActiveRecord association && association.polymorphic? end + def association_join_fk + association.send(:join_fk) + end + # TODO Change this to private once we've dropped Ruby 2.2 support. # Workaround for Ruby 2.2 "private attribute?" warning. protected -- cgit v1.2.3 From 5f64f361aea5a59c5782ed9d635668a738d9fabd Mon Sep 17 00:00:00 2001 From: chopraanmol1 Date: Wed, 9 Aug 2017 11:18:27 +0530 Subject: Changed join_fk private method to join_foreign_key public method --- activerecord/lib/active_record/reflection.rb | 18 +++++++++--------- .../predicate_builder/association_query_value.rb | 2 +- activerecord/lib/active_record/table_metadata.rb | 6 +----- 3 files changed, 11 insertions(+), 15 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb index 97f1a83033..d044f9dfe8 100644 --- a/activerecord/lib/active_record/reflection.rb +++ b/activerecord/lib/active_record/reflection.rb @@ -305,13 +305,17 @@ module ActiveRecord end def get_join_keys(association_klass) - JoinKeys.new(join_pk(association_klass), join_fk) + JoinKeys.new(join_pk(association_klass), join_foreign_key) end def build_scope(table, predicate_builder = predicate_builder(table)) Relation.create(klass, table, predicate_builder) end + def join_foreign_key + active_record_primary_key + end + protected def actual_source_reflection # FIXME: this is a horrible name self @@ -325,10 +329,6 @@ module ActiveRecord def join_pk(_) foreign_key end - - def join_fk - active_record_primary_key - end end # Base class for AggregateReflection and AssociationReflection. Objects of @@ -754,16 +754,16 @@ module ActiveRecord owner[foreign_key] end + def join_foreign_key + foreign_key + end + private def calculate_constructable(macro, options) !polymorphic? end - def join_fk - foreign_key - end - def join_pk(klass) polymorphic? ? association_primary_key(klass) : association_primary_key end diff --git a/activerecord/lib/active_record/relation/predicate_builder/association_query_value.rb b/activerecord/lib/active_record/relation/predicate_builder/association_query_value.rb index 27efd49622..0255a65bfe 100644 --- a/activerecord/lib/active_record/relation/predicate_builder/association_query_value.rb +++ b/activerecord/lib/active_record/relation/predicate_builder/association_query_value.rb @@ -9,7 +9,7 @@ module ActiveRecord end def queries - [associated_table.association_join_fk.to_s => ids] + [associated_table.association_join_foreign_key.to_s => ids] end # TODO Change this to private once we've dropped Ruby 2.2 support. diff --git a/activerecord/lib/active_record/table_metadata.rb b/activerecord/lib/active_record/table_metadata.rb index 02e368e708..a5187efc84 100644 --- a/activerecord/lib/active_record/table_metadata.rb +++ b/activerecord/lib/active_record/table_metadata.rb @@ -2,7 +2,7 @@ module ActiveRecord class TableMetadata # :nodoc: - delegate :foreign_type, :foreign_key, :join_keys, to: :association, prefix: true + delegate :foreign_type, :foreign_key, :join_keys, :join_foreign_key, to: :association, prefix: true delegate :association_primary_key, to: :association def initialize(klass, arel_table, association = nil) @@ -66,10 +66,6 @@ module ActiveRecord association && association.polymorphic? end - def association_join_fk - association.send(:join_fk) - end - # TODO Change this to private once we've dropped Ruby 2.2 support. # Workaround for Ruby 2.2 "private attribute?" warning. protected -- cgit v1.2.3