diff options
author | chopraanmol1 <chopraanmol1@gmail.com> | 2017-08-08 03:23:45 +0530 |
---|---|---|
committer | chopraanmol1 <chopraanmol1@gmail.com> | 2017-08-08 03:23:45 +0530 |
commit | 7919d5ca7b11107671a72fa0cfd2adc139954e4e (patch) | |
tree | ddeabe7cbc98ed19384f9374e7b35f2b53f04cae | |
parent | df94b863c2ff8f1bcf12e36ed8fc1419292668e7 (diff) | |
download | rails-7919d5ca7b11107671a72fa0cfd2adc139954e4e.tar.gz rails-7919d5ca7b11107671a72fa0cfd2adc139954e4e.tar.bz2 rails-7919d5ca7b11107671a72fa0cfd2adc139954e4e.zip |
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.
3 files changed, 21 insertions, 3 deletions
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 diff --git a/activerecord/test/cases/relation/where_test.rb b/activerecord/test/cases/relation/where_test.rb index c45fd38bc9..2fe2a67b1c 100644 --- a/activerecord/test/cases/relation/where_test.rb +++ b/activerecord/test/cases/relation/where_test.rb @@ -295,6 +295,20 @@ module ActiveRecord assert_equal essays(:david_modest_proposal), essay end + def test_where_with_relation_on_has_many_association + essay = essays(:david_modest_proposal) + author = Author.where(essays: Essay.where(id: essay.id)).first + + assert_equal authors(:david), author + end + + def test_where_with_relation_on_has_one_association + author = authors(:david) + author_address = AuthorAddress.where(author: Author.where(id: author.id)).first + assert_equal author_addresses(:david_address), author_address + end + + def test_where_on_association_with_select_relation essay = Essay.where(author: Author.where(name: "David").select(:name)).take assert_equal essays(:david_modest_proposal), essay |