提交 68754d37 编写于 作者: R Ryuta Kamizono

Fix `last` with `offset` to behave consistently with loaded relation

Currently `last` with `offset` behaves incorrectly because `offset` can
not be reversed like `limit`. Therefore, `offset` should also be handled
like `limit`.
上级 a194c527
......@@ -148,7 +148,7 @@ def first!
#
# [#<Person id:4>, #<Person id:3>, #<Person id:2>]
def last(limit = nil)
return find_last(limit) if loaded? || limit_value
return find_last(limit) if loaded? || has_limit_or_offset?
result = ordered_relation.limit(limit)
result = result.reverse_order!
......
......@@ -677,6 +677,10 @@ def test_last_on_relation_with_limit_and_offset
assert_equal comments.limit(2).to_a.last(2), comments.limit(2).last(2)
assert_equal comments.limit(2).to_a.last(3), comments.limit(2).last(3)
assert_equal comments.offset(2).to_a.last, comments.offset(2).last
assert_equal comments.offset(2).to_a.last(2), comments.offset(2).last(2)
assert_equal comments.offset(2).to_a.last(3), comments.offset(2).last(3)
comments = comments.offset(1)
assert_equal comments.limit(2).to_a.last, comments.limit(2).last
assert_equal comments.limit(2).to_a.last(2), comments.limit(2).last(2)
......@@ -1049,14 +1053,6 @@ def test_find_by_two_attributes_but_passing_only_one
assert_raise(ArgumentError) { Topic.find_by_title_and_author_name("The First Topic") }
end
def test_find_last_with_offset
devs = Developer.order("id")
assert_equal devs[2], Developer.offset(2).first
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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册