diff --git a/activerecord/lib/active_record/relation/predicate_builder.rb b/activerecord/lib/active_record/relation/predicate_builder.rb index b9eb5d9090da2ffa840387da2e42ea1b29add358..7146fad071def19f0c8bf30ef25f76fd0ab0f1b2 100644 --- a/activerecord/lib/active_record/relation/predicate_builder.rb +++ b/activerecord/lib/active_record/relation/predicate_builder.rb @@ -86,6 +86,10 @@ def expand_from_hash(attributes, &block) value = [value] unless value.is_a?(Array) klass = PolymorphicArrayValue end + elsif associated_table.through_association? + next associated_table.predicate_builder.expand_from_hash( + associated_table.association_join_foreign_key => value + ) end klass ||= AssociationQueryValue diff --git a/activerecord/lib/active_record/table_metadata.rb b/activerecord/lib/active_record/table_metadata.rb index 402a903374176addd783bf3e944573d3fb71766b..c81266f017320b5893e06ff4e7c8cb72d1545e3f 100644 --- a/activerecord/lib/active_record/table_metadata.rb +++ b/activerecord/lib/active_record/table_metadata.rb @@ -55,6 +55,10 @@ def polymorphic_association? reflection&.polymorphic? end + def through_association? + reflection&.through_reflection? + end + def reflect_on_aggregation(aggregation_name) klass&.reflect_on_aggregation(aggregation_name) end diff --git a/activerecord/test/cases/relation/where_test.rb b/activerecord/test/cases/relation/where_test.rb index 18e086690c458d987d7aa74d55f8349df14386bf..5f933b45a41ac8065498f2e44e1d27d7231e2365 100644 --- a/activerecord/test/cases/relation/where_test.rb +++ b/activerecord/test/cases/relation/where_test.rb @@ -25,6 +25,10 @@ def test_type_casting_nested_joins assert_equal [comment], Comment.joins(post: :author).where(authors: { id: "2-foo" }) end + def test_where_with_through_association + assert_equal [authors(:david)], Author.joins(:comments).where(comments: comments(:greetings)) + end + def test_type_cast_is_not_evaluated_at_relation_build_time posts = nil