Skip to content
This repository
Browse code

Document ActiveRecord::QueryMethods#select

  • Loading branch information...
commit c7ed6f303e47da6c91e5a035bbadb49de904018c 1 parent a8501c1
Ryan Bigg authored September 14, 2011 vijaydev committed September 21, 2011
29  activerecord/lib/active_record/relation/query_methods.rb
@@ -37,6 +37,35 @@ def preload(*args)
37 37
       relation
38 38
     end
39 39
 
  40
+    # Works in two unique ways.
  41
+    # 
  42
+    # First: takes a block so it can be used just like Array#select.
  43
+    #
  44
+    #   Model.scoped.select { |m| m.field == value }
  45
+    #
  46
+    # This will build an array of objects from the database for the scope,
  47
+    # converting them into an array and iterating through them using Array#select.
  48
+    #
  49
+    # Second: Modifies the SELECT statement for the query so that only certain
  50
+    # fields are retreived:
  51
+    #
  52
+    #   >> Model.select(:field)
  53
+    #   => [#<Model field:value>]
  54
+    #
  55
+    # Although in the above example it looks as though this method returns an
  56
+    # array, in actual fact it returns a relation object and can have other query
  57
+    # methods appended to it, such as the other methods in ActiveRecord::QueryMethods.
  58
+    #
  59
+    # This method will also take multiple parameters:
  60
+    #
  61
+    #   >> Model.select(:field, :other_field, :and_one_more)
  62
+    #   => [#<Model field: "value", other_field: "value", :and_one_more: "value">]
  63
+    #
  64
+    # Any attributes that do not have fields retreived by a select
  65
+    # will return `nil` when the getter method for that attribute is used:
  66
+    #
  67
+    #   >> Model.select(:field).first.other_field
  68
+    #   => nil
40 69
     def select(value = Proc.new)
41 70
       if block_given?
42 71
         to_a.select {|*block_args| value.call(*block_args) }

0 notes on commit c7ed6f3

Please sign in to comment.
Something went wrong with that request. Please try again.