From 524238139025ccddfa886bcfd7a1a6434954e305 Mon Sep 17 00:00:00 2001 From: Yasyf Mohamedali Date: Mon, 2 Mar 2015 17:18:35 -0500 Subject: Honour the order of the joining model in a `has_many :through` association when eager loading. Previously, eager loading a `has_many :through` association with no defined order would return the records in the natural order of the database. Now, these records will be returned in the order that the joining record is returned, in case there is a defined order there. --- activerecord/test/models/tag.rb | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'activerecord/test/models') diff --git a/activerecord/test/models/tag.rb b/activerecord/test/models/tag.rb index 80d4725f7e..b48b9a2155 100644 --- a/activerecord/test/models/tag.rb +++ b/activerecord/test/models/tag.rb @@ -5,3 +5,9 @@ class Tag < ActiveRecord::Base has_many :tagged_posts, :through => :taggings, :source => 'taggable', :source_type => 'Post' end + +class OrderedTag < Tag + self.table_name = "tags" + + has_many :taggings, -> { order('taggings.id DESC') }, foreign_key: 'tag_id' +end -- cgit v1.2.3