Database fields are polluted with both nil and empty '' values when submitting forms

Just to make sure this wasn’t just our app, I made a dummy new Rails 6.0.3 app here with:

rails generate scaffold Coconut name:string diameter:integer weight:integer

Then I created a new rew record by leaving the name field emtpy:

And what I get in the DB is an empty string:

I was able to repro both for the default SQLite DB, as well as Postgres.

For a brand new app like this with scaffolded forms that cover all fields, all new Coconut records will have an empty string, so that’s nice because all your records are consistently created with an empty '' field.

But for any app that goes beyond rails scaffolds where forms don’t cover all fields, for example if a record is updated across several forms that a user may not necessarily fill out completely, or with an app with different ways of creating a record (form 1, form 2, background jobs, etc), you will often end up with a mix of empty '' and nil fields.

Thus leading to a “WTF” situation where you have to do where(name: [nil, '']) whenever you want to get records whose name is empty, instead of just where(name: nil).