From a8ede3664055f33c102b3f229cf280b0bf69c540 Mon Sep 17 00:00:00 2001 From: Dan Erikson Date: Mon, 8 Apr 2013 00:41:16 -0600 Subject: [PATCH] Changed ActiveRecord::Associations::CollectionProxy#select to take multiple arguments. This makes the arguments the same as ActiveRecord::QueryMethods::select. --- .../active_record/associations/collection_association.rb | 4 ++-- .../lib/active_record/associations/collection_proxy.rb | 6 +++--- .../test/cases/associations/has_many_associations_test.rb | 6 +++++- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/activerecord/lib/active_record/associations/collection_association.rb b/activerecord/lib/active_record/associations/collection_association.rb index 2a00ac138632b..5b08d07425124 100644 --- a/activerecord/lib/active_record/associations/collection_association.rb +++ b/activerecord/lib/active_record/associations/collection_association.rb @@ -67,11 +67,11 @@ def reset @target = [] end - def select(select = nil) + def select(*fields) if block_given? load_target.select.each { |e| yield e } else - scope.select(select) + scope.select(*fields) end end diff --git a/activerecord/lib/active_record/associations/collection_proxy.rb b/activerecord/lib/active_record/associations/collection_proxy.rb index 8a5b3128629cd..ef2acfce897fe 100644 --- a/activerecord/lib/active_record/associations/collection_proxy.rb +++ b/activerecord/lib/active_record/associations/collection_proxy.rb @@ -76,7 +76,7 @@ def loaded? # # # # # ] # - # person.pets.select([:id, :name]) + # person.pets.select(:id, :name ) # # => [ # # #, # # #, @@ -107,8 +107,8 @@ def loaded? # # #, # # # # # ] - def select(select = nil, &block) - @association.select(select, &block) + def select(*fields, &block) + @association.select(*fields, &block) end # Finds an object in the collection responding to the +id+. Uses the same diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index 781b87741d0ef..7c50c187639e5 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -523,7 +523,11 @@ def test_default_select end def test_select_query_method - assert_equal ['id'], posts(:welcome).comments.select(:id).first.attributes.keys + assert_equal ['id', 'body'], posts(:welcome).comments.select(:id, :body).first.attributes.keys + end + + def test_select_with_block + assert_equal [1], posts(:welcome).comments.select { |c| c.id == 1 }.map(&:id) end def test_adding