aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSantiago Pastorino <santiago@wyeworks.com>2014-11-24 14:55:34 -0200
committerSantiago Pastorino <santiago@wyeworks.com>2014-11-24 14:55:34 -0200
commite3acd74fa8cbe5c09321ee75dd387e9d8cb044fc (patch)
tree03b48edff010244ca9f7f54588a1cd8ffdfe23f7
parent33c80cb69e183abb1212de0851565ef7f1c48cc4 (diff)
parent91e3dab804fffe0b1daebb438091418faa1fa256 (diff)
downloadrails-e3acd74fa8cbe5c09321ee75dd387e9d8cb044fc.tar.gz
rails-e3acd74fa8cbe5c09321ee75dd387e9d8cb044fc.tar.bz2
rails-e3acd74fa8cbe5c09321ee75dd387e9d8cb044fc.zip
Merge pull request #17678 from siddharth28/includes_with_scope_with_joins
Fix includes on association with a scope
-rw-r--r--activerecord/CHANGELOG.md6
-rw-r--r--activerecord/lib/active_record/associations/preloader/association.rb10
-rw-r--r--activerecord/lib/active_record/relation/query_methods.rb6
-rw-r--r--activerecord/test/cases/associations/eager_test.rb6
-rw-r--r--activerecord/test/models/post.rb1
5 files changed, 17 insertions, 12 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index a4de332d4c..9130cfbe9e 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -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*
diff --git a/activerecord/lib/active_record/associations/preloader/association.rb b/activerecord/lib/active_record/associations/preloader/association.rb
index 496c426986..7d6523dbc4 100644
--- a/activerecord/lib/active_record/associations/preloader/association.rb
+++ b/activerecord/lib/active_record/associations/preloader/association.rb
@@ -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!
diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb
index eb69943551..6e384facce 100644
--- a/activerecord/lib/active_record/relation/query_methods.rb
+++ b/activerecord/lib/active_record/relation/query_methods.rb
@@ -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
diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb
index 4539b99504..dd4f530791 100644
--- a/activerecord/test/cases/associations/eager_test.rb
+++ b/activerecord/test/cases/associations/eager_test.rb
@@ -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 }
diff --git a/activerecord/test/models/post.rb b/activerecord/test/models/post.rb
index 36cf221d45..a9996e5236 100644
--- a/activerecord/test/models/post.rb
+++ b/activerecord/test/models/post.rb
@@ -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'