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

Fix ActiveRecord::Relation#include? in case where offset is provided

This commit is contained in:
Adrianna Chang 2020-10-06 15:30:16 -04:00 committed by GitHub
parent d71291cb3c
commit 5aff638e2d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 1 deletions

View file

@ -325,7 +325,7 @@ module ActiveRecord
# compared to the records in memory. If the relation is unloaded, an
# efficient existence query is performed, as in #exists?.
def include?(record)
if loaded?
if loaded? || offset_value
records.include?(record)
else
record.is_a?(klass) && exists?(record.id)

View file

@ -405,6 +405,12 @@ class FinderTest < ActiveRecord::TestCase
end
end
def test_include_on_unloaded_relation_with_offset
assert_sql(/ORDER BY name ASC/) do
assert_equal true, Customer.offset(1).order("name ASC").include?(customers(:mary))
end
end
def test_include_on_loaded_relation_without_match
customers = Customer.where(name: "David").load
mary = customers(:mary)
@ -453,6 +459,12 @@ class FinderTest < ActiveRecord::TestCase
end
end
def test_member_on_unloaded_relation_with_offset
assert_sql(/ORDER BY name ASC/) do
assert_equal true, Customer.offset(1).order("name ASC").member?(customers(:mary))
end
end
def test_find_by_array_of_one_id
assert_kind_of(Array, Topic.find([ 1 ]))
assert_equal(1, Topic.find([ 1 ]).length)