Skip to content

Commit

Permalink
Changed ActiveRecord::Associations::CollectionProxy#select to take mu…
Browse files Browse the repository at this point in the history
…ltiple arguments.

This makes the arguments the same as ActiveRecord::QueryMethods::select.
  • Loading branch information
derikson committed Apr 8, 2013
1 parent a92814b commit a8ede36
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
Expand Up @@ -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

Expand Down
Expand Up @@ -76,7 +76,7 @@ def loaded?
# # #<Pet id: nil, name: "Choo-Choo">
# # ]
#
# person.pets.select([:id, :name])
# person.pets.select(:id, :name )
# # => [
# # #<Pet id: 1, name: "Fancy-Fancy">,
# # #<Pet id: 2, name: "Spook">,
Expand Down Expand Up @@ -107,8 +107,8 @@ def loaded?
# # #<Pet id: 2, name: "Spook">,
# # #<Pet id: 3, name: "Choo-Choo">
# # ]
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
Expand Down
Expand Up @@ -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
Expand Down

0 comments on commit a8ede36

Please sign in to comment.