diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2006-05-07 02:03:25 +0000 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2006-05-07 02:03:25 +0000 |
commit | d59f3a78a423064f53dadd9f55d05b5f7b2b240e (patch) | |
tree | 2d69b9e85362f4b6b73422e5bb17e453a7af9208 | |
parent | 50f538b72bbe6657627c9efe55b65d167956d1b7 (diff) | |
download | rails-d59f3a78a423064f53dadd9f55d05b5f7b2b240e.tar.gz rails-d59f3a78a423064f53dadd9f55d05b5f7b2b240e.tar.bz2 rails-d59f3a78a423064f53dadd9f55d05b5f7b2b240e.zip |
uniq preserves order. References [4325].
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4326 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rw-r--r-- | activerecord/lib/active_record/associations/association_collection.rb | 9 | ||||
-rwxr-xr-x | activerecord/test/associations_test.rb | 15 | ||||
-rw-r--r-- | activerecord/test/fixtures/project.rb | 3 |
3 files changed, 19 insertions, 8 deletions
diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb index e705576120..428152eea0 100644 --- a/activerecord/lib/active_record/associations/association_collection.rb +++ b/activerecord/lib/active_record/associations/association_collection.rb @@ -109,7 +109,14 @@ module ActiveRecord end def uniq(collection = self) - collection.to_set.to_a + seen = Set.new + collection.inject([]) do |kept, record| + unless seen.include?(record.id) + kept << record + seen << record.id + end + kept + end end # Replace this collection with +other_array+ diff --git a/activerecord/test/associations_test.rb b/activerecord/test/associations_test.rb index f94908ced1..6076b10446 100755 --- a/activerecord/test/associations_test.rb +++ b/activerecord/test/associations_test.rb @@ -1278,17 +1278,20 @@ class HasAndBelongsToManyAssociationsTest < Test::Unit::TestCase def test_habtm_saving_multiple_relationships new_project = Project.new("name" => "Grimetime") amount_of_developers = 4 - developers = (0..amount_of_developers).collect {|i| Developer.create(:name => "JME #{i}") } - + developers = (0...amount_of_developers).collect {|i| Developer.create(:name => "JME #{i}") }.reverse + new_project.developer_ids = [developers[0].id, developers[1].id] new_project.developers_with_callback_ids = [developers[2].id, developers[3].id] assert new_project.save - + new_project.reload assert_equal amount_of_developers, new_project.developers.size - amount_of_developers.times do |i| - assert_equal developers[i].name, new_project.developers[i].name - end + assert_equal developers, new_project.developers + end + + def test_habtm_unique_order_preserved + assert_equal [developers(:poor_jamis), developers(:jamis), developers(:david)], projects(:active_record).non_unique_developers + assert_equal [developers(:poor_jamis), developers(:jamis), developers(:david)], projects(:active_record).developers end def test_build diff --git a/activerecord/test/fixtures/project.rb b/activerecord/test/fixtures/project.rb index c1aa4145f9..4d4e0e127a 100644 --- a/activerecord/test/fixtures/project.rb +++ b/activerecord/test/fixtures/project.rb @@ -1,5 +1,6 @@ class Project < ActiveRecord::Base - has_and_belongs_to_many :developers, :uniq => true + has_and_belongs_to_many :developers, :uniq => true, :order => 'developers.name desc, developers.id desc' + has_and_belongs_to_many :non_unique_developers, :order => 'developers.name desc, developers.id desc', :class_name => 'Developer' has_and_belongs_to_many :limited_developers, :class_name => "Developer", :limit => 1 has_and_belongs_to_many :developers_named_david, :class_name => "Developer", :conditions => "name = 'David'", :uniq => true has_and_belongs_to_many :salaried_developers, :class_name => "Developer", :conditions => "salary > 0" |