aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/relation/query_methods.rb
diff options
context:
space:
mode:
authorIsaac Sanders <isaac@isaacbfsanders.com>2012-08-23 13:48:02 -0500
committerFrancesco Rodriguez <lrodriguezsanc@gmail.com>2012-08-23 13:51:10 -0500
commit7bf9db529e69cd3cbcedddc0f9af752b0480d13c (patch)
treeaac99103e890ba60a53af8446c23a5c5f9d3c545 /activerecord/lib/active_record/relation/query_methods.rb
parent764deabd85964e4d64976ca0c3fc3a96faeb66a2 (diff)
downloadrails-7bf9db529e69cd3cbcedddc0f9af752b0480d13c.tar.gz
rails-7bf9db529e69cd3cbcedddc0f9af752b0480d13c.tar.bz2
rails-7bf9db529e69cd3cbcedddc0f9af752b0480d13c.zip
Model.select takes a variable list of arguments.
This is a cleaner version of #6916. Closes #3165.
Diffstat (limited to 'activerecord/lib/active_record/relation/query_methods.rb')
-rw-r--r--activerecord/lib/active_record/relation/query_methods.rb23
1 files changed, 12 insertions, 11 deletions
diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb
index 8e6254f918..f6bacf4822 100644
--- a/activerecord/lib/active_record/relation/query_methods.rb
+++ b/activerecord/lib/active_record/relation/query_methods.rb
@@ -136,8 +136,8 @@ module ActiveRecord
# Second: Modifies the SELECT statement for the query so that only certain
# fields are retrieved:
#
- # >> Model.select(:field)
- # => [#<Model field:value>]
+ # Model.select(:field)
+ # # => [#<Model field:value>]
#
# Although in the above example it looks as though this method returns an
# array, it actually returns a relation object and can have other query
@@ -145,25 +145,26 @@ module ActiveRecord
#
# The argument to the method can also be an array of fields.
#
- # >> Model.select([:field, :other_field, :and_one_more])
- # => [#<Model field: "value", other_field: "value", and_one_more: "value">]
+ # Model.select(:field, :other_field, :and_one_more)
+ # # => [#<Model field: "value", other_field: "value", and_one_more: "value">]
#
# Accessing attributes of an object that do not have fields retrieved by a select
# will throw <tt>ActiveModel::MissingAttributeError</tt>:
#
- # >> Model.select(:field).first.other_field
- # => ActiveModel::MissingAttributeError: missing attribute: other_field
- def select(value = Proc.new)
+ # Model.select(:field).first.other_field
+ # # => ActiveModel::MissingAttributeError: missing attribute: other_field
+ def select(*fields)
if block_given?
- to_a.select { |*block_args| value.call(*block_args) }
+ to_a.select { |*block_args| yield(*block_args) }
else
- spawn.select!(value)
+ raise ArgumentError, 'Call this with at least one field' if fields.empty?
+ spawn.select!(*fields)
end
end
# Like #select, but modifies relation in place.
- def select!(value)
- self.select_values += Array.wrap(value)
+ def select!(*fields)
+ self.select_values += fields.flatten
self
end