Skip to content

Commit

Permalink
Document last, check for primary key on default order and use quote…
Browse files Browse the repository at this point in the history
…d table and column names
  • Loading branch information
mhfs committed May 3, 2012
1 parent 07e5301 commit 489166e
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions activerecord/lib/active_record/relation/finder_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 489166e

Please sign in to comment.