From 92c10760d7f5e1d308e492d5fd3d551df41bfabb Mon Sep 17 00:00:00 2001 From: Nick Howard Date: Sat, 7 May 2011 17:19:02 -0600 Subject: Fix for #371 if a query contains a limit or an offset, ActiveRecord::FinderMethods#find_last had inconsistent behavior. If the records were loaded, it returned the last record in the cached list. If they were not, it reversed the order of the query and changed the limit to one. If the earlier limit was less than the total matching the query in the db, it would return a different record than if the records had been cached. This commit changes find_last so that it loads the records when getting the last record on a query containing a limit or an offset, which makes the behavior consistent. --- activerecord/lib/active_record/relation/finder_methods.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'activerecord/lib/active_record/relation') diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb index 57c9921ea8..32d1cff6c3 100644 --- a/activerecord/lib/active_record/relation/finder_methods.rb +++ b/activerecord/lib/active_record/relation/finder_methods.rb @@ -375,7 +375,12 @@ module ActiveRecord if loaded? @records.last else - @last ||= reverse_order.limit(1).to_a[0] + @last ||= + if offset_value || limit_value + to_a.last + else + reverse_order.limit(1).to_a[0] + end end end -- cgit v1.2.3