Skip to content

Commit

Permalink
Fix includes on association with a scope containing joins along with …
Browse files Browse the repository at this point in the history
…conditions

on the joined assoiciation
  • Loading branch information
siddharth@vinsol.com committed Nov 21, 2014
1 parent 7e375af commit 91e3dab
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 12 deletions.
6 changes: 6 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down
6 changes: 2 additions & 4 deletions activerecord/lib/active_record/relation/query_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions activerecord/test/cases/associations/eager_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
1 change: 1 addition & 0 deletions activerecord/test/models/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down

0 comments on commit 91e3dab

Please sign in to comment.