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/cases/associations
Ryuta Kamizono 10b36e81a3 Fix incorrect result when eager loading with duplicated through association with join scope
I had found the issue while working on fixing #33525.

That is if duplicated association has a scope which has `where` with
explicit table name condition (e.g. `where("categories.name": "General")`),
that condition in all duplicated associations will filter the first one
only, other all duplicated associations are not filtered, since
duplicated joins will be aliased except the first one (e.g.
`INNER JOIN "categories" "categories_categorizations"`).

```ruby
class Author < ActiveRecord::Base
  has_many :general_categorizations, -> { joins(:category).where("categories.name": "General") }, class_name: "Categorization"
  has_many :general_posts, through: :general_categorizations, source: :post
end

authors = Author.eager_load(:general_categorizations, :general_posts).to_a
```

Generated eager loading query:

```sql
SELECT "authors"."id" AS t0_r0, ... FROM "authors"

-- `has_many :general_categorizations, -> { joins(:category).where("categories.name": "General") }`
LEFT OUTER JOIN "categorizations" ON "categorizations"."author_id" = "authors"."id"
INNER JOIN "categories" ON "categories"."id" = "categorizations"."category_id" AND "categories"."name" = ?

-- `has_many :general_posts, through: :general_categorizations, source: :post`
---- duplicated `through: :general_categorizations` part
LEFT OUTER JOIN "categorizations" "general_categorizations_authors_join" ON "general_categorizations_authors_join"."author_id" = "authors"."id"
INNER JOIN "categories" "categories_categorizations" ON "categories_categorizations"."id" = "general_categorizations_authors_join"."category_id" AND "categories"."name" = ? -- <-- filtering `"categories"."name" = ?` won't work
---- `source: :post` part
LEFT OUTER JOIN "posts" ON "posts"."id" = "general_categorizations_authors_join"."post_id"
```

Originally eager loading with join scope didn't work before Rails 5.2
(#29413), and duplicated through association with join scope raised a
duplicated alias error before alias tracking is improved in 590b045.

But now it will potentially be got incorrect result instead of an error,
it is worse than an error.

To fix the issue, it makes eager loading to deduplicate / re-use
duplicated through association if possible, like as `preload`.

```sql
SELECT "authors"."id" AS t0_r0, ... FROM "authors"

-- `has_many :general_categorizations, -> { joins(:category).where("categories.name": "General") }`
LEFT OUTER JOIN "categorizations" ON "categorizations"."author_id" = "authors"."id"
INNER JOIN "categories" ON "categories"."id" = "categorizations"."category_id" AND "categories"."name" = ?

-- `has_many :general_posts, through: :general_categorizations, source: :post`
---- `through: :general_categorizations` part is deduplicated / re-used
LEFT OUTER JOIN "posts" ON "posts"."id" = "categorizations"."post_id"
```

Fixes #32819.
2020-08-07 13:39:30 +09:00
..
belongs_to_associations_test.rb Fix preloading for polymorphic association with custom scope 2020-05-26 04:07:28 +09:00
bidirectional_destroy_dependencies_test.rb Use frozen-string-literal in ActiveRecord 2017-07-19 22:27:07 +03:00
callbacks_test.rb Fix typo in test name 2020-03-21 20:58:56 +09:00
cascaded_eager_loading_test.rb Avoid extraneous preloading when loading across has_one associations 2020-01-08 08:14:04 +13:00
eager_load_includes_full_sti_class_test.rb Fix CI failure due to remaining tagging records 2019-03-26 12:59:16 +09:00
eager_load_nested_include_test.rb Fix EagerLoadPolyAssocsTest setup (#38883) 2020-04-07 10:14:17 +09:00
eager_singularization_test.rb Use frozen-string-literal in ActiveRecord 2017-07-19 22:27:07 +03:00
eager_test.rb Fix flakey test due to non-deterministic order 2020-07-27 10:53:12 -05:00
extension_test.rb Enable Layout/EmptyLinesAroundAccessModifier cop 2019-06-13 12:00:45 +09:00
has_and_belongs_to_many_associations_test.rb Fix join middle table alias when using HABTM 2019-07-26 23:12:44 +09:00
has_many_associations_test.rb Resolve attribute alias for counter cache column 2020-07-06 12:06:01 +09:00
has_many_through_associations_test.rb Fix incorrect result when eager loading with duplicated through association with join scope 2020-08-07 13:39:30 +09:00
has_one_associations_test.rb Fix flakey destroyed_by_association tests 2020-05-04 19:26:54 -05:00
has_one_through_associations_test.rb More exercise singular association query 2018-11-28 03:34:20 +09:00
inner_join_association_test.rb Move Arel attribute normalization into arel_table 2020-07-19 23:41:24 +09:00
inverse_associations_test.rb Do not use object_id on Active Record object directly 2020-05-24 11:08:24 +09:00
join_model_test.rb Add DidYouMean for HasManyThroughAssociationNotFoundError 2020-05-19 08:27:08 +02:00
left_outer_join_association_test.rb Fix eager load with Arel joins to maintain the original joins order 2020-05-16 10:17:11 +09:00
nested_through_associations_test.rb Fix through association to respect source scope for includes/preload 2020-05-21 07:40:01 +09:00
required_test.rb Enable Layout/EmptyLinesAroundAccessModifier cop 2019-06-13 12:00:45 +09:00