Skip to content

Commit

Permalink
Revert "Revert "Merge pull request #22026 from akihiro17/fix-preload-…
Browse files Browse the repository at this point in the history
…association""

This reverts commit 5243946.

This fixes an issue with the build where tests would fail on mysql and
postgresql due to different ordering.
  • Loading branch information
senny committed Oct 29, 2015
1 parent 3ad796e commit 857a34a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
6 changes: 6 additions & 0 deletions activerecord/CHANGELOG.md
@@ -1,3 +1,9 @@
* Set `scope.reordering_value` to `true` if :reordering values are specified.

Fixes #21886.

*Hiroaki Izu*

* Add support for bidirectional destroy dependencies.

Fixes #13609.
Expand Down
Expand Up @@ -136,6 +136,10 @@ def build_scope
end
scope.order! preload_values[:order] || values[:order]

if preload_values[:reordering] || values[:reordering]
scope.reordering_value = true
end

if preload_values[:readonly] || values[:readonly]
scope.readonly!
end
Expand Down
18 changes: 18 additions & 0 deletions activerecord/test/cases/associations/eager_test.rb
Expand Up @@ -1179,6 +1179,24 @@ def test_preloading_has_many_through_with_distinct
assert_equal 1, mary.unique_categorized_post_ids.length
end

def test_preloading_has_one_using_reorder
klass = Class.new(ActiveRecord::Base) do
def self.name; "TempAuthor"; end
self.table_name = "authors"
has_one :post, class_name: "PostWithDefaultScope", foreign_key: :author_id
has_one :reorderd_post, -> { reorder(title: :desc) }, class_name: "PostWithDefaultScope", foreign_key: :author_id
end

author = klass.first
# PRECONDITION: make sure ordering results in different results
assert_not_equal author.post, author.reorderd_post

preloaded_reorderd_post = klass.preload(:reorderd_post).first.reorderd_post

assert_equal author.reorderd_post, preloaded_reorderd_post
assert_equal Post.order(title: :desc).first.title, preloaded_reorderd_post.title
end

def test_preloading_polymorphic_with_custom_foreign_type
sponsor = sponsors(:moustache_club_sponsor_for_groucho)
groucho = members(:groucho)
Expand Down

1 comment on commit 857a34a

@senny
Copy link
Member Author

@senny senny commented on 857a34a Oct 29, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/cc @sgrif

Please sign in to comment.