diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb index 3b06efa442..05bfdbf6a0 100644 --- a/activerecord/lib/active_record/relation/finder_methods.rb +++ b/activerecord/lib/active_record/relation/finder_methods.rb @@ -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) diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb index d33d0fb341..9849ba79e7 100644 --- a/activerecord/test/cases/finder_test.rb +++ b/activerecord/test/cases/finder_test.rb @@ -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)