aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorLauro Caetano <laurocaetano1@gmail.com>2013-12-03 16:33:34 -0200
committerLauro Caetano <laurocaetano1@gmail.com>2013-12-03 17:09:58 -0200
commit2a517e7291d6f93b6ca3d8c56fc8559c48089c28 (patch)
tree7559121e8ecc9bc4fb51e477093a4f3bd0e060da /activerecord
parentbe5527b8e8fcc25946b128fe78db10d5bee2a483 (diff)
downloadrails-2a517e7291d6f93b6ca3d8c56fc8559c48089c28.tar.gz
rails-2a517e7291d6f93b6ca3d8c56fc8559c48089c28.tar.bz2
rails-2a517e7291d6f93b6ca3d8c56fc8559c48089c28.zip
Fix offset with last.
Closes #7441
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG.md6
-rw-r--r--activerecord/lib/active_record/relation/finder_methods.rb2
-rw-r--r--activerecord/test/cases/finder_test.rb9
3 files changed, 16 insertions, 1 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index 936dcbb2fb..16d5e15489 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -1,3 +1,9 @@
+* `Model.offset(4).last` now returns the 4th record from the end, not just last record.
+
+ Fixes #7441.
+
+ *kostya*
+
* `type_to_sql` returns a `String` for unmapped columns. This fixes an error
when using unmapped array types in PG
diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb
index d91d6367a3..3963f2b3e0 100644
--- a/activerecord/lib/active_record/relation/finder_methods.rb
+++ b/activerecord/lib/active_record/relation/finder_methods.rb
@@ -384,7 +384,7 @@ module ActiveRecord
@records.last
else
@last ||=
- if offset_value || limit_value
+ if limit_value
to_a.last
else
reverse_order.limit(1).to_a.first
diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb
index e76c1a113a..b06418d9fe 100644
--- a/activerecord/test/cases/finder_test.rb
+++ b/activerecord/test/cases/finder_test.rb
@@ -720,6 +720,15 @@ class FinderTest < ActiveRecord::TestCase
assert_raise(ArgumentError) { Topic.find_by_title_and_author_name("The First Topic") }
end
+ def test_find_last_with_offset
+ devs = Developer.order('id').all
+
+ assert_equal devs[2], Developer.offset(2).first
+ assert_equal devs[-3], Developer.offset(2).last
+ assert_equal devs[-3], Developer.offset(2).last
+ assert_equal devs[-3], Developer.offset(2).order('id DESC').first
+ end
+
def test_find_by_nil_attribute
topic = Topic.find_by_last_read nil
assert_not_nil topic