aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorVijay Dev <vijaydev.cse@gmail.com>2011-09-21 19:35:09 +0530
committerVijay Dev <vijaydev.cse@gmail.com>2011-09-21 19:35:09 +0530
commitcaa95ab6d826f4bb112c2911849ce03c7312af11 (patch)
tree6d64a68ba038e0be20adecfa93dedc269dfdbec8 /activerecord/lib
parent95646f44fdb7cfecaac049a11b8ecef781b42d98 (diff)
parent3e80462b95808457eb1584195909e26887a1a40d (diff)
downloadrails-caa95ab6d826f4bb112c2911849ce03c7312af11.tar.gz
rails-caa95ab6d826f4bb112c2911849ce03c7312af11.tar.bz2
rails-caa95ab6d826f4bb112c2911849ce03c7312af11.zip
Merge branch 'master' of github.com:lifo/docrails
Diffstat (limited to 'activerecord/lib')
-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..670ba0987d 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 retrieved:
+ #
+ # >> 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
+ # 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 retrieved 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) }