1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

values[:includes] in reflection_scope is not compatible with through_scope

Without this fix, preloading `:comments_with_include` will cause the
following error:

```
% bundle exec ruby -w -Itest test/cases/associations/eager_test.rb -n test_eager_with_has_many_through_join_model_with_include
Using sqlite3
Run options: -n test_eager_with_has_many_through_join_model_with_include --seed 1502

E

Error:
EagerAssociationTest#test_eager_with_has_many_through_join_model_with_include:
ActiveRecord::AssociationNotFoundError: Association named 'post' was not found on Post; perhaps you misspelled it?
```
This commit is contained in:
Ryuta Kamizono 2017-09-02 05:12:42 +09:00
parent 855e5f59f0
commit a26cff3c12
2 changed files with 7 additions and 2 deletions

View file

@ -86,9 +86,14 @@ module ActiveRecord
if options[:source_type]
scope.where! reflection.foreign_type => options[:source_type]
elsif !reflection_scope.where_clause.empty?
scope.includes_values = Array(values[:includes] || options[:source])
scope.where_clause = reflection_scope.where_clause
if includes = values[:includes]
scope.includes!(source_reflection.name => includes)
else
scope.includes!(source_reflection.name)
end
if joins = values[:joins]
scope.joins!(source_reflection.name => joins)
end

View file

@ -21,7 +21,7 @@ class Author < ActiveRecord::Base
end
has_many :comments_containing_the_letter_e, through: :posts, source: :comments
has_many :comments_with_order_and_conditions, -> { order("comments.body").where("comments.body like 'Thank%'") }, through: :posts, source: :comments
has_many :comments_with_include, -> { includes(:post) }, through: :posts, source: :comments
has_many :comments_with_include, -> { includes(:post).where(posts: { type: "Post" }) }, through: :posts, source: :comments
has_many :first_posts
has_many :comments_on_first_posts, -> { order("posts.id desc, comments.id asc") }, through: :first_posts, source: :comments