提交 5834d2f5 编写于 作者: R Rafael França 提交者: GitHub

Merge pull request #30126 from chopraanmol1/support_for_has_many_and_has_one_for_where_relation

Fixed query building when relation is passed for has one or has many association in where 
......@@ -305,13 +305,17 @@ def chain
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 @@ def predicate_builder(table)
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 @@ def join_id_for(owner) # :nodoc:
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
......
......@@ -9,7 +9,7 @@ def initialize(associated_table, value)
end
def queries
[associated_table.association_foreign_key.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.
......@@ -30,7 +30,7 @@ def ids
end
def primary_key
associated_table.association_primary_key
associated_table.association_join_keys.key
end
def convert_to_id(value)
......
......@@ -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, :join_foreign_key, to: :association, prefix: true
delegate :association_primary_key, to: :association
def initialize(klass, arel_table, association = nil)
......
......@@ -295,6 +295,20 @@ def test_where_on_association_with_custom_primary_key_with_array_of_ids
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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册