aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorRick Olson <technoweenie@gmail.com>2007-12-11 20:08:30 +0000
committerRick Olson <technoweenie@gmail.com>2007-12-11 20:08:30 +0000
commit66e97c34c116bbf0e7b87802e81a9f7df0684b47 (patch)
treec9cd3bcb8e58a6259194bf412a8434c0852a3411 /activerecord
parentf67add4283069653f5707bd45cff214913dfc579 (diff)
downloadrails-66e97c34c116bbf0e7b87802e81a9f7df0684b47.tar.gz
rails-66e97c34c116bbf0e7b87802e81a9f7df0684b47.tar.bz2
rails-66e97c34c116bbf0e7b87802e81a9f7df0684b47.zip
Ensure that the :uniq option for has_many :through associations retains the order. #10463 [remvee]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8376 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG2
-rw-r--r--activerecord/lib/active_record/associations/has_many_through_association.rb3
-rw-r--r--activerecord/test/associations/join_model_test.rb6
-rw-r--r--activerecord/test/fixtures/author.rb2
4 files changed, 12 insertions, 1 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index 7d924284a4..7dc1722fac 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Ensure that the :uniq option for has_many :through associations retains the order. #10463 [remvee]
+
* Base.exists? doesn't rescue exceptions to avoid hiding SQL errors. #10458 [Michael Klishin]
* Documentation: Active Record exceptions, destroy_all and delete_all. #10444, #10447 [Michael Klishin]
diff --git a/activerecord/lib/active_record/associations/has_many_through_association.rb b/activerecord/lib/active_record/associations/has_many_through_association.rb
index 06b1276860..14f752abe4 100644
--- a/activerecord/lib/active_record/associations/has_many_through_association.rb
+++ b/activerecord/lib/active_record/associations/has_many_through_association.rb
@@ -148,7 +148,8 @@ module ActiveRecord
:include => @reflection.options[:include] || @reflection.source_reflection.options[:include]
)
- @reflection.options[:uniq] ? records.to_set.to_a : records
+ records.uniq! if @reflection.options[:uniq]
+ records
end
# Construct attributes for associate pointing to owner.
diff --git a/activerecord/test/associations/join_model_test.rb b/activerecord/test/associations/join_model_test.rb
index 631f7034f4..aaef40ebf1 100644
--- a/activerecord/test/associations/join_model_test.rb
+++ b/activerecord/test/associations/join_model_test.rb
@@ -539,6 +539,12 @@ class AssociationsJoinModelTest < Test::Unit::TestCase
def test_has_many_through_has_many_with_sti
assert_equal [comments(:does_it_hurt)], authors(:david).special_post_comments
end
+
+ def test_uniq_has_many_through_should_retain_order
+ comment_ids = authors(:david).comments.map(&:id)
+ assert_equal comment_ids.sort, authors(:david).ordered_uniq_comments.map(&:id)
+ assert_equal comment_ids.sort.reverse, authors(:david).ordered_uniq_comments_desc.map(&:id)
+ end
private
# create dynamic Post models to allow different dependency options
diff --git a/activerecord/test/fixtures/author.rb b/activerecord/test/fixtures/author.rb
index d41d8ac439..1503e17f87 100644
--- a/activerecord/test/fixtures/author.rb
+++ b/activerecord/test/fixtures/author.rb
@@ -18,6 +18,8 @@ class Author < ActiveRecord::Base
has_many :comments_desc, :through => :posts, :source => :comments, :order => 'comments.id DESC'
has_many :limited_comments, :through => :posts, :source => :comments, :limit => 1
has_many :funky_comments, :through => :posts, :source => :comments
+ has_many :ordered_uniq_comments, :through => :posts, :source => :comments, :uniq => true, :order => 'comments.id'
+ has_many :ordered_uniq_comments_desc, :through => :posts, :source => :comments, :uniq => true, :order => 'comments.id DESC'
has_many :special_posts
has_many :special_post_comments, :through => :special_posts, :source => :comments