instance method sole

Ruby on Rails 7.0.10

Since v7.0.10

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

Signature

sole()

Finds the sole matching record. Raises ActiveRecord::RecordNotFound if no record is found. Raises ActiveRecord::SoleRecordExceeded if more than one record is found.

Product.where(["price = %?", price]).sole
Source
# File activerecord/lib/active_record/relation/finder_methods.rb, line 112
    def sole
      found, undesired = first(2)

      if found.nil?
        raise_record_not_found_exception!
      elsif undesired.present?
        raise ActiveRecord::SoleRecordExceeded.new(self)
      else
        found
      end
    end

Defined in activerecord/lib/active_record/relation/finder_methods.rb line 112 · 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