Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed query building when relation is passed for has one or has many association in where #30126

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 9 additions & 9 deletions activerecord/lib/active_record/reflection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/table_metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions activerecord/test/cases/relation/where_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down