1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Fix offset with last.

Closes #7441
This commit is contained in:
Lauro Caetano 2013-12-03 16:33:34 -02:00
parent be5527b8e8
commit 2a517e7291
3 changed files with 16 additions and 1 deletions

View file

@ -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 * `type_to_sql` returns a `String` for unmapped columns. This fixes an error
when using unmapped array types in PG when using unmapped array types in PG

View file

@ -384,7 +384,7 @@ module ActiveRecord
@records.last @records.last
else else
@last ||= @last ||=
if offset_value || limit_value if limit_value
to_a.last to_a.last
else else
reverse_order.limit(1).to_a.first reverse_order.limit(1).to_a.first

View file

@ -720,6 +720,15 @@ class FinderTest < ActiveRecord::TestCase
assert_raise(ArgumentError) { Topic.find_by_title_and_author_name("The First Topic") } assert_raise(ArgumentError) { Topic.find_by_title_and_author_name("The First Topic") }
end 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 def test_find_by_nil_attribute
topic = Topic.find_by_last_read nil topic = Topic.find_by_last_read nil
assert_not_nil topic assert_not_nil topic