I often need this in specs, sometimes I'd like to include this in code as well. The idea is to ensure that the query returns really only single result
module FinderExtensions
def find_uniq!(cond)
res = where(cond)
res.first.tap do |r|
raise ActiveRecord::RecordNotFound unless r
raise ActiveRecord::ActiveRecordError, "multiple records found" if res[1]
end
end
end
ActiveRecord::Base.send(:extend, FinderExtensions)