1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activerecord/test
eileencodes 51660f0191 Fix leaky chain on polymorphic association
If there was a polymorphic hm:t association with a scope AND second
non-scoped hm:t association on a model the polymorphic scope would leak
through into the call for the non-polymorhic hm:t association.

This would only break if `hotel.drink_designers` was called before
`hotel.recipes`. If `hotel.recipes` was called first there would be
no problem with the SQL.

Before (employable_type should not be here):
```
SELECT COUNT(*) FROM "drink_designers" INNER JOIN "chefs" ON
"drink_designers"."id" = "chefs"."employable_id" INNER JOIN
"departments" ON "chefs"."department_id" = "departments"."id" WHERE
"departments"."hotel_id" = ? AND "chefs"."employable_type" = ?
[["hotel_id", 1], ["employable_type", "DrinkDesigner"]]
```

After:
```
SELECT COUNT(*) FROM "recipes" INNER JOIN "chefs" ON "recipes"."chef_id"
= "chefs"."id" INNER JOIN "departments" ON "chefs"."department_id" =
"departments"."id" WHERE "departments"."hotel_id" = ?  [["hotel_id", 1]]
```

From the SQL you can see that `employable_type` was leaking through when
calling recipes. The solution is to dup the chain of the polymorphic
association so it doesn't get cached. Additionally, this follows
`scope_chain` which dup's the `source_reflection`'s `scope_chain`.

This required another model/table/relationship because the leak only
happens on a hm:t polymorphic that's called before another hm:t on the
same model.

I am specifically testing the SQL here instead of the number of records
becasue the test could pass if there was 1 drink designer recipe for the
drink designer chef even though the `employable_type` was leaking through.
This needs to specifically check that `employable_type` is not in the SQL
statement.
2015-03-15 10:39:42 -04:00
..
active_record/connection_adapters rm Column#cast_type 2015-02-03 15:02:18 -07:00
assets
cases Fix leaky chain on polymorphic association 2015-03-15 10:39:42 -04:00
fixtures Fixes #18492 2015-01-14 21:36:58 +05:30
migrations Add line endings to files which are missing them 2015-02-17 13:28:06 -07:00
models Fix leaky chain on polymorphic association 2015-03-15 10:39:42 -04:00
schema Fix leaky chain on polymorphic association 2015-03-15 10:39:42 -04:00
support Add YAML compatibility for objects from Rails 4.2 2015-03-10 11:56:45 -06:00
.gitignore
config.example.yml
config.rb