Permalink
Browse files

Document `last`, check for primary key on default order and use quote…

…d table and column names
  • Loading branch information...
1 parent 07e5301 commit 489166e114ba3af54b1f2081027cd44078d32bbc @mhfs mhfs committed Apr 26, 2012
Showing with 5 additions and 2 deletions.
  1. +5 −2 activerecord/lib/active_record/relation/finder_methods.rb
@@ -87,15 +87,18 @@ def first!
first or raise RecordNotFound
end
+ # Find the last record (or last N records if a parameter is supplied).
+ # If no order is defined it will order by primary key.
+ #
# Examples:
#
# Person.last # returns the last object fetched by SELECT * FROM people
# Person.where(["user_name = ?", user_name]).last
# Person.order("created_on DESC").offset(5).last
def last(limit = nil)
if limit
- if order_values.empty?
- order("#{primary_key} DESC").limit(limit).reverse
+ if order_values.empty? && primary_key
+ order("#{quoted_table_name}.#{quoted_primary_key} DESC").limit(limit).reverse
else
to_a.last(limit)
end

0 comments on commit 489166e

Please sign in to comment.