instance method include?

Ruby on Rails 7.1.6

Since v6.1.7.10

Available in: v6.1.7.10 v7.0.10 v7.1.6 v7.2.3 v8.0.4 v8.1.2

Signature

include?(record)

Returns true if the relation contains the given record or false otherwise.

No query is performed if the relation is loaded; the given record is compared to the records in memory. If the relation is unloaded, an efficient existence query is performed, as in #exists?.

Parameters

record req
Source
# File activerecord/lib/active_record/relation/finder_methods.rb, line 377
    def include?(record)
      # The existing implementation relies on receiving an Active Record instance as the input parameter named record.
      # Any non-Active Record object passed to this implementation is guaranteed to return `false`.
      return false unless record.is_a?(klass)

      if loaded? || offset_value || limit_value || having_clause.any?
        records.include?(record)
      else
        id = if record.class.composite_primary_key?
          record.class.primary_key.zip(record.id).to_h
        else
          record.id
        end

        exists?(id)
      end
    end

Defined in activerecord/lib/active_record/relation/finder_methods.rb line 377 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActiveRecord::FinderMethods

Type at least 2 characters to search.

↑↓ navigate · open · esc close