From 489166e114ba3af54b1f2081027cd44078d32bbc Mon Sep 17 00:00:00 2001 From: Marcelo Silveira Date: Thu, 26 Apr 2012 20:10:10 -0300 Subject: Document `last`, check for primary key on default order and use quoted table and column names --- activerecord/lib/active_record/relation/finder_methods.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb index 9fd8b49ffb..4a41f1ea8b 100644 --- a/activerecord/lib/active_record/relation/finder_methods.rb +++ b/activerecord/lib/active_record/relation/finder_methods.rb @@ -87,6 +87,9 @@ module ActiveRecord 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 @@ -94,8 +97,8 @@ module ActiveRecord # 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 -- cgit v1.2.3