aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2017-10-27 23:36:09 +0900
committerRyuta Kamizono <kamipo@gmail.com>2017-10-28 23:16:07 +0900
commitcd4cbfccea281b670e527401b0e8572746e39a5d (patch)
tree712cbbdada4574f9a4509c122f96ee60c82d5e26 /activerecord
parente4eaf8faae03f31b110aee0baefcf7fb48536f5b (diff)
downloadrails-cd4cbfccea281b670e527401b0e8572746e39a5d.tar.gz
rails-cd4cbfccea281b670e527401b0e8572746e39a5d.tar.bz2
rails-cd4cbfccea281b670e527401b0e8572746e39a5d.zip
Ordinal methods should respect loaded records
We should reset partially loaded `@offsets` cache when latest records has loaded because the cache has been staled and it may not be consistent with latest records.
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/relation.rb1
-rw-r--r--activerecord/test/cases/finder_test.rb16
2 files changed, 17 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb
index 997cfe4b5e..c16d89aa00 100644
--- a/activerecord/lib/active_record/relation.rb
+++ b/activerecord/lib/active_record/relation.rb
@@ -594,6 +594,7 @@ module ActiveRecord
end
@records.each(&:readonly!) if readonly_value
+ @offsets = {} unless @offsets.empty?
@loaded = true
@records
diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb
index d8bc917e7f..d03af7f111 100644
--- a/activerecord/test/cases/finder_test.rb
+++ b/activerecord/test/cases/finder_test.rb
@@ -676,6 +676,22 @@ class FinderTest < ActiveRecord::TestCase
assert_kind_of Array, Topic.last(5)
end
+ def test_first_should_respect_loaded_records
+ authors = Author.order(:name)
+
+ assert_equal authors(:bob), authors.first
+
+ aaron = authors.create!(name: "Aaron")
+
+ authors.load
+
+ assert_no_queries do
+ assert_equal aaron, authors.first
+ assert_equal authors(:bob), authors.second
+ assert_not_equal authors.first, authors.second
+ end
+ end
+
def test_unexisting_record_exception_handling
assert_raise(ActiveRecord::RecordNotFound) {
Topic.find(1).parent