Empty select box gives empty string not nil

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the rails category.

Last Updated: 2024-12-03

My frontend was as follows:

<%= f.select :law_level, @tutor_search_form.possible_law_levels,
  {include_blank: "Any"} %>

In my backend, I had the following

institution = params[:institution]
tutors = tutors.where(institution: institution) if institution

This gave me bugs. Why? Because institution was "", i.e. empty string, which is truthy.

The fix:

tutors = tutors.where(institution: institution) if institution.present?

Lessons