mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
Merge pull request #451 from baroquebobcat/fixing_last_for_issue_371
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -683,6 +683,27 @@ class FinderTest < ActiveRecord::TestCase
|
||||
assert_nil Topic.find_last_by_title_and_author_name(topic.title, "Anonymous")
|
||||
end
|
||||
|
||||
def test_find_last_with_limit_gives_same_result_when_loaded_and_unloaded
|
||||
scope = Topic.limit(2)
|
||||
unloaded_last = scope.last
|
||||
loaded_last = scope.all.last
|
||||
assert_equal loaded_last, unloaded_last
|
||||
end
|
||||
|
||||
def test_find_last_with_limit_and_offset_gives_same_result_when_loaded_and_unloaded
|
||||
scope = Topic.offset(2).limit(2)
|
||||
unloaded_last = scope.last
|
||||
loaded_last = scope.all.last
|
||||
assert_equal loaded_last, unloaded_last
|
||||
end
|
||||
|
||||
def test_find_last_with_offset_gives_same_result_when_loaded_and_unloaded
|
||||
scope = Topic.offset(3)
|
||||
unloaded_last = scope.last
|
||||
loaded_last = scope.all.last
|
||||
assert_equal loaded_last, unloaded_last
|
||||
end
|
||||
|
||||
def test_find_all_by_one_attribute
|
||||
topics = Topic.find_all_by_content("Have a nice day")
|
||||
assert_equal 2, topics.size
|
||||
|
||||
Reference in New Issue
Block a user