diff options
author | Yves Senn <yves.senn@gmail.com> | 2015-12-21 12:27:49 +0100 |
---|---|---|
committer | Yves Senn <yves.senn@gmail.com> | 2015-12-21 12:31:52 +0100 |
commit | b06f6a1d8c7c09841c6829b788c6560b70eb36a7 (patch) | |
tree | 496ee4e6970147888d9502972b3fd2aae2cdaddb /activerecord | |
parent | 078d7c9de79dfa7f5758d52ae67c1157d8ecfe36 (diff) | |
download | rails-b06f6a1d8c7c09841c6829b788c6560b70eb36a7.tar.gz rails-b06f6a1d8c7c09841c6829b788c6560b70eb36a7.tar.bz2 rails-b06f6a1d8c7c09841c6829b788c6560b70eb36a7.zip |
Revert "Merge pull request #22486 from methyl/fix-includes-for-groupped-association"
This reverts commit 537ac7d6ade61e95f2b70685ff2236b7de965bab, reversing
changes made to 9c9c54abe08d86967efd3dcac1d65158a0ff74ea.
Reason:
The way we preload associations will change the meaning of GROUP BY
operations. This is illustrated in the SQL generated by the added
test (failing on PG):
Association Load:
D, [2015-12-21T12:26:07.169920 #26969] DEBUG -- : Post Load (0.7ms) SELECT "posts".* FROM "posts" LEFT JOIN comments ON comments.post_id = posts.id WHERE "posts"."author_id" = $1 GROUP BY posts.id ORDER BY SUM(comments.tags_count) [["author_id", 1]]
Preload:
D, [2015-12-21T12:26:07.128305 #26969] DEBUG -- : Post Load (1.3ms) SELECT "posts".* FROM "posts" LEFT JOIN comments ON comments.post_id = posts.id WHERE "posts"."author_id" IN (1, 2, 3) GROUP BY posts.id ORDER BY SUM(comments.tags_count)
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/CHANGELOG.md | 4 | ||||
-rw-r--r-- | activerecord/lib/active_record/associations/preloader/association.rb | 6 | ||||
-rw-r--r-- | activerecord/test/cases/associations/eager_test.rb | 17 |
3 files changed, 1 insertions, 26 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index c82d706d6e..fb6c5e7e81 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,7 +1,3 @@ -* Use group by values when preloading scoped associations. - - *Lucjan Suski* - ## Rails 5.0.0.beta1 (December 18, 2015) ## * Order the result of `find(ids)` to match the passed array, if the relation diff --git a/activerecord/lib/active_record/associations/preloader/association.rb b/activerecord/lib/active_record/associations/preloader/association.rb index 91e7f90739..e11a5cfb8a 100644 --- a/activerecord/lib/active_record/associations/preloader/association.rb +++ b/activerecord/lib/active_record/associations/preloader/association.rb @@ -107,7 +107,7 @@ module ActiveRecord @preloaded_records = slices.flat_map do |slice| records_for(slice) end - @preloaded_records.group_by do |record| + @preloaded_records.group_by do |record| convert_key(record[association_key_name]) end end @@ -139,10 +139,6 @@ module ActiveRecord scope.order!(order_values) end - if group_values = preload_values[:group] || values[:group] - scope.group!(group_values) - end - if preload_values[:reordering] || values[:reordering] scope.reordering_value = true end diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb index 966789884b..874d53c51f 100644 --- a/activerecord/test/cases/associations/eager_test.rb +++ b/activerecord/test/cases/associations/eager_test.rb @@ -1242,23 +1242,6 @@ class EagerAssociationTest < ActiveRecord::TestCase assert_equal projects.last.mentor.developers.first.contracts, projects.last.developers.last.contracts end - def test_eager_load_with_group_clause - assert_nothing_raised(ActiveRecord::StatementInvalid) do - subclass = Class.new(ActiveRecord::Base) do - def self.name; "Author"; end - self.table_name = "authors" - has_many :posts_ordered_by_comments_tags_count, -> { - joins('LEFT JOIN comments ON comments.post_id = posts.id'). - order("SUM(comments.tags_count)"). - group('posts.id') }, class_name: "Post" - end - - preloaded = subclass.includes(:posts_ordered_by_comments_tags_count).map(&:posts_ordered_by_comments_tags_count) - loaded = subclass.all.map(&:posts_ordered_by_comments_tags_count) - assert preloaded == loaded, 'Preloaded records differ from expected result.' - end - end - test "scoping with a circular preload" do assert_equal Comment.find(1), Comment.preload(:post => :comments).scoping { Comment.find(1) } end |