Skip to content
This repository
Browse code

explaining ActiveRecord#first in rails 3 and 4

  • Loading branch information...
commit b8640d47907876d43335628fea40da03df0e84cd 1 parent 04417f3
Thiago James Pinto authored June 08, 2013
13  activerecord/lib/active_record/relation/finder_methods.rb
@@ -79,6 +79,19 @@ def take!
79 79
     #   Person.where(["user_name = :u", { u: user_name }]).first
80 80
     #   Person.order("created_on DESC").offset(5).first
81 81
     #   Person.first(3) # returns the first three objects fetched by SELECT * FROM people LIMIT 3
  82
+    #
  83
+    # ==== Rails 3
  84
+    #
  85
+    #   Person.first # SELECT "users".* FROM "users" LIMIT 1
  86
+    #
  87
+    # NOTE: Rails 3 may not +order+ this query by be the primary key.
  88
+    # The order will depend on the database implementation.
  89
+    # In order to ensure that behavior use <tt>User.order(:id).first</tt> instead.
  90
+    #
  91
+    # ==== Rails 4
  92
+    #
  93
+    #   Person.first # SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT 1
  94
+    #
82 95
     def first(limit = nil)
83 96
       if limit
84 97
         if order_values.empty? && primary_key

0 notes on commit b8640d4

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