Skip to content

Commit

Permalink
Merge pull request #19479 from huoxito/4-1-stable-polymorphic-associa…
Browse files Browse the repository at this point in the history
…tion

Don't error when passing an empty array to a polymorphic association (rails 4.1.10 regression)
  • Loading branch information
sgrif committed Mar 23, 2015
2 parents 3a4d7d8 + 78958a7 commit a33c082
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
7 changes: 5 additions & 2 deletions activerecord/lib/active_record/relation/predicate_builder.rb
Expand Up @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions activerecord/test/cases/relation/where_test.rb
Expand Up @@ -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]))
Expand Down

0 comments on commit a33c082

Please sign in to comment.