From 78958a79e07c4c02b7ed87bf7e393de989e05c39 Mon Sep 17 00:00:00 2001 From: Sean Griffin Date: Thu, 26 Feb 2015 11:26:23 -0700 Subject: [PATCH] Don't error when passing an empty array to a polymorphic association Fixes #19087 --- .../lib/active_record/relation/predicate_builder.rb | 7 +++++-- activerecord/test/cases/relation/where_test.rb | 12 ++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/activerecord/lib/active_record/relation/predicate_builder.rb b/activerecord/lib/active_record/relation/predicate_builder.rb index 121878a9ac651..f98cd137a4c81 100644 --- a/activerecord/lib/active_record/relation/predicate_builder.rb +++ b/activerecord/lib/active_record/relation/predicate_builder.rb @@ -61,8 +61,11 @@ def self.expand(klass, table, column, value) end column = reflection.foreign_key - primary_key = reflection.association_primary_key(base_class) - value = convert_value_to_association_ids(value, primary_key) + + if base_class + primary_key = reflection.association_primary_key(base_class) + value = convert_value_to_association_ids(value, primary_key) + end end queries << build(table[column], value) diff --git a/activerecord/test/cases/relation/where_test.rb b/activerecord/test/cases/relation/where_test.rb index fd74f28c3557a..d0445901dcd69 100644 --- a/activerecord/test/cases/relation/where_test.rb +++ b/activerecord/test/cases/relation/where_test.rb @@ -96,6 +96,18 @@ def test_polymorphic_nested_array_where assert_equal expected.to_sql, actual.to_sql end + def test_polymorphic_empty_array_where + treasure = Treasure.new + treasure.id = 1 + hidden = HiddenTreasure.new + hidden.id = 2 + + expected = PriceEstimate.where("1=0") + actual = PriceEstimate.where(estimate_of: []) + + assert_equal expected.to_a, actual.to_a + end + def test_polymorphic_nested_relation_where expected = PriceEstimate.where(estimate_of_type: 'Treasure', estimate_of_id: Treasure.where(id: [1,2])) actual = PriceEstimate.where(estimate_of: Treasure.where(id: [1,2]))