Fix includes on association with a scope containing joins along with conditions

on the joined assoiciation
This commit is contained in:
siddharth@vinsol.com 2014-11-20 01:35:04 +05:30
parent 7e375afba3
commit 91e3dab804
5 changed files with 17 additions and 12 deletions

@ -2,6 +2,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*

@ -142,14 +142,8 @@ def build_scope
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!

@ -427,14 +427,12 @@ def unscope!(*args) # :nodoc:
# => 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

@ -904,6 +904,12 @@ def test_preconfigured_includes_with_has_one
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 }

@ -78,6 +78,7 @@ def greeting
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'