instance method
sole
Ruby on Rails 8.1.2
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 143
def sole
found, undesired = take(2)
if found.nil?
raise_record_not_found_exception!
elsif undesired.nil?
found
else
raise ActiveRecord::SoleRecordExceeded.new(self)
end
end
Defined in activerecord/lib/active_record/relation/finder_methods.rb line 143
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActiveRecord::FinderMethods