diff --git a/activerecord/lib/active_record/relation/predicate_builder.rb b/activerecord/lib/active_record/relation/predicate_builder.rb index 4bb9ef85b7..2039418fb1 100644 --- a/activerecord/lib/active_record/relation/predicate_builder.rb +++ b/activerecord/lib/active_record/relation/predicate_builder.rb @@ -100,7 +100,7 @@ module ActiveRecord end elsif associated_table.through_association? next associated_table.predicate_builder.expand_from_hash( - associated_table.join_foreign_key => value + associated_table.primary_key => value ) end diff --git a/activerecord/lib/active_record/table_metadata.rb b/activerecord/lib/active_record/table_metadata.rb index 66df45e790..57c66fe57e 100644 --- a/activerecord/lib/active_record/table_metadata.rb +++ b/activerecord/lib/active_record/table_metadata.rb @@ -10,6 +10,10 @@ module ActiveRecord @reflection = reflection end + def primary_key + klass&.primary_key + end + def type(column_name) arel_table.type_for_attribute(column_name) end diff --git a/activerecord/test/cases/relation/where_test.rb b/activerecord/test/cases/relation/where_test.rb index bae8f597ec..006008a663 100644 --- a/activerecord/test/cases/relation/where_test.rb +++ b/activerecord/test/cases/relation/where_test.rb @@ -5,6 +5,8 @@ require "models/author" require "models/binary" require "models/cake_designer" require "models/car" +require "models/category" +require "models/categorization" require "models/chef" require "models/post" require "models/comment" @@ -18,7 +20,8 @@ require "support/stubs/strong_parameters" module ActiveRecord class WhereTest < ActiveRecord::TestCase - fixtures :posts, :comments, :edges, :authors, :author_addresses, :binaries, :essays, :cars, :treasures, :price_estimates, :topics + fixtures :authors, :author_addresses, :categories, :categorizations, :cars, :treasures, :price_estimates, + :binaries, :edges, :essays, :posts, :comments, :topics def test_type_casting_nested_joins comment = comments(:eager_other_comment1) @@ -27,6 +30,7 @@ module ActiveRecord def test_where_with_through_association assert_equal [authors(:david)], Author.joins(:comments).where(comments: comments(:greetings)) + assert_equal [authors(:bob)], Author.joins(:categories).where(categories: categories(:technology)) end def test_type_cast_is_not_evaluated_at_relation_build_time