From 48a7df9a9f039ecb6466d51c651e4bc624077cf4 Mon Sep 17 00:00:00 2001 From: Alexey Vakhov Date: Fri, 9 Mar 2012 15:18:16 +0400 Subject: In AR depths use &:to_i before :uniq to process mixed arrays likes ["1", 1] correct --- .../lib/active_record/associations/collection_association.rb | 2 +- activerecord/test/cases/associations/has_many_associations_test.rb | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/activerecord/lib/active_record/associations/collection_association.rb b/activerecord/lib/active_record/associations/collection_association.rb index b2136605e1..da4c311bce 100644 --- a/activerecord/lib/active_record/associations/collection_association.rb +++ b/activerecord/lib/active_record/associations/collection_association.rb @@ -544,7 +544,7 @@ module ActiveRecord # If using a custom finder_sql, #find scans the entire collection. def find_by_scan(*args) expects_array = args.first.kind_of?(Array) - ids = args.flatten.compact.uniq.map { |arg| arg.to_i } + ids = args.flatten.compact.map{ |arg| arg.to_i }.uniq if ids.size == 1 id = ids.first diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index 02a7f6af78..6a4f972356 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -378,6 +378,13 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert_equal 1, Firm.find(:first, :order => "id").clients_using_sql.size end + def test_finding_using_sql_take_into_account_only_uniq_ids + firm = Firm.find(:first, :order => "id") + client = firm.clients_using_sql.first + assert_equal client, firm.clients_using_sql.find(client.id, client.id) + assert_equal client, firm.clients_using_sql.find(client.id, client.id.to_s) + end + def test_counting_using_sql assert_equal 1, Firm.find(:first, :order => "id").clients_using_counter_sql.size assert Firm.find(:first, :order => "id").clients_using_counter_sql.any? -- cgit v1.2.3