aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorRyan Bigg <radarlistener@gmail.com>2011-09-14 18:53:08 +1000
committerRyan Bigg <radarlistener@gmail.com>2011-09-17 16:08:58 -0700
commit908f2616d5c2b9e26eb180859f7df529a7a59f08 (patch)
tree53a2c86b364945fe1835656a5a7dd7c7630e9b11 /activerecord
parent663031801cac577a88931cdfe6f062555112f370 (diff)
downloadrails-908f2616d5c2b9e26eb180859f7df529a7a59f08.tar.gz
rails-908f2616d5c2b9e26eb180859f7df529a7a59f08.tar.bz2
rails-908f2616d5c2b9e26eb180859f7df529a7a59f08.zip
Document ActiveRecord::QueryMethods#select
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/relation/query_methods.rb29
1 files changed, 29 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb
index a11b7a3864..4468a38ee6 100644
--- a/activerecord/lib/active_record/relation/query_methods.rb
+++ b/activerecord/lib/active_record/relation/query_methods.rb
@@ -37,6 +37,35 @@ module ActiveRecord
relation
end
+ # Works in two unique ways.
+ #
+ # First: takes a block so it can be used just like Array#select.
+ #
+ # Model.scoped.select { |m| m.field == value }
+ #
+ # This will build an array of objects from the database for the scope,
+ # converting them into an array and iterating through them using Array#select.
+ #
+ # Second: Modifies the SELECT statement for the query so that only certain
+ # fields are retreived:
+ #
+ # >> Model.select(:field)
+ # => [#<Model field:value>]
+ #
+ # Although in the above example it looks as though this method returns an
+ # array, in actual fact it returns a relation object and can have other query
+ # methods appended to it, such as the other methods in ActiveRecord::QueryMethods.
+ #
+ # This method will also take multiple parameters:
+ #
+ # >> Model.select(:field, :other_field, :and_one_more)
+ # => [#<Model field: "value", other_field: "value", :and_one_more: "value">]
+ #
+ # Any attributes that do not have fields retreived by a select
+ # will return `nil` when the getter method for that attribute is used:
+ #
+ # >> Model.select(:field).first.other_field
+ # => nil
def select(value = Proc.new)
if block_given?
to_a.select {|*block_args| value.call(*block_args) }