Permalink
Browse files

Ensure that the :uniq option for has_many :through associations retai…

…ns the order. #10463 [remvee]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8376 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent f67add4 commit 66e97c34c116bbf0e7b87802e81a9f7df0684b47 @technoweenie technoweenie committed Dec 11, 2007
View
@@ -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]
@@ -148,7 +148,8 @@ def find_target
: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.
@@ -539,6 +539,12 @@ def test_has_many_through_sum_uses_calculations
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
@@ -18,6 +18,8 @@ def testing_proxy_target
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

0 comments on commit 66e97c3

Please sign in to comment.