Merge pull request #17678 from siddharth28/includes_with_scope_with_joins

Fix includes on association with a scope
This commit is contained in:
Santiago Pastorino 2014-11-24 14:55:34 -02:00
commit e3acd74fa8
5 changed files with 17 additions and 12 deletions

View File

@ -14,6 +14,12 @@
*Yves Senn*
* Fix includes on association with a scope containing joins along with conditions
on the joined assoiciation.
*Siddharth Sharma*
* Add `Table#name` to match `TableDefinition#name`.
*Cody Cutrer*

View File

@ -142,14 +142,8 @@ module ActiveRecord
scope._select! preload_values[:select] || values[:select] || table[Arel.star]
scope.includes! preload_values[:includes] || values[:includes]
if preload_values.key? :order
scope.order! preload_values[:order]
else
if values.key? :order
scope.order! values[:order]
end
end
scope.joins! preload_values[:joins] || values[:joins]
scope.order! preload_values[:order] || values[:order]
if preload_values[:readonly] || values[:readonly]
scope.readonly!

View File

@ -427,14 +427,12 @@ module ActiveRecord
# => SELECT "users".* FROM "users" LEFT JOIN bookmarks ON bookmarks.bookmarkable_type = 'Post' AND bookmarks.user_id = users.id
def joins(*args)
check_if_method_has_arguments!(:joins, args)
args.compact!
args.flatten!
spawn.joins!(*args)
end
def joins!(*args) # :nodoc:
args.compact!
args.flatten!
self.joins_values += args
self
end

View File

@ -904,6 +904,12 @@ class EagerAssociationTest < ActiveRecord::TestCase
assert_no_queries {assert_equal posts(:sti_comments), comment.post}
end
def test_eager_association_with_scope_with_joins
assert_nothing_raised do
Post.includes(:very_special_comment_with_post_with_joins).to_a
end
end
def test_preconfigured_includes_with_has_many
posts = authors(:david).posts_with_comments
one = posts.detect { |p| p.id == 1 }

View File

@ -78,6 +78,7 @@ class Post < ActiveRecord::Base
has_one :very_special_comment
has_one :very_special_comment_with_post, -> { includes(:post) }, :class_name => "VerySpecialComment"
has_one :very_special_comment_with_post_with_joins, -> { joins(:post).order('posts.id') }, class_name: "VerySpecialComment"
has_many :special_comments
has_many :nonexistant_comments, -> { where 'comments.id < 0' }, :class_name => 'Comment'