I’d like to know if a Rails database query returns a row or not (I don’t need to know whats in the row, just if a row is returned).
I can do this:
academic_year = AcademicYear.find_by_raw_input(year)
if academic_year
...
end
but if I mistype and do a find_all_by:
academic_year = AcademicYear.find_all_by_raw_input(year)
then an empty array is returned, which causes the if statement to be true.
I know, I should be careful and just avoid the all call, but is there a rails-esque call to see if the return result from any query (all or not) is nil?
As you said,
find_by_...will returnnil, andfind_all_by_...will return[]. I think what you’re looking for is.blank?.In console