diff options
| author | Ryuta Kamizono <kamipo@gmail.com> | 2018-01-07 12:42:37 +0900 | 
|---|---|---|
| committer | Ryuta Kamizono <kamipo@gmail.com> | 2018-01-07 12:52:59 +0900 | 
| commit | 68754d37706f11ca363b3108eda10749b9957817 (patch) | |
| tree | c874adc065384214f1876ce1c3ae7e22cbff52d9 | |
| parent | a194c527fa7b16840fd1b1250dc9f624c6536546 (diff) | |
| download | rails-68754d37706f11ca363b3108eda10749b9957817.tar.gz rails-68754d37706f11ca363b3108eda10749b9957817.tar.bz2 rails-68754d37706f11ca363b3108eda10749b9957817.zip  | |
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`.
| -rw-r--r-- | activerecord/lib/active_record/relation/finder_methods.rb | 2 | ||||
| -rw-r--r-- | activerecord/test/cases/finder_test.rb | 12 | 
2 files changed, 5 insertions, 9 deletions
diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb index b61e65cfb3..4bf346a512 100644 --- a/activerecord/lib/active_record/relation/finder_methods.rb +++ b/activerecord/lib/active_record/relation/finder_methods.rb @@ -148,7 +148,7 @@ module ActiveRecord      #      #   [#<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! diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb index 8369a10b5a..c0485a7be7 100644 --- a/activerecord/test/cases/finder_test.rb +++ b/activerecord/test/cases/finder_test.rb @@ -677,6 +677,10 @@ class FinderTest < ActiveRecord::TestCase      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 @@ 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") - -    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  | 
