instance method
sole
Ruby on Rails 7.0.10
Since v7.0.10Signature
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