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

**URL:** <https://discuss.rubyonrails.org/t/database-fields-are-polluted-with-both-nil-and-empty-values-when-submitting-forms/74877>\
**Category:** A May Of WTFs\
**Created:** [May 11, 2020, 10:36pm UTC](https://discuss.rubyonrails.org/t/database-fields-are-polluted-with-both-nil-and-empty-values-when-submitting-forms/74877 "2020-05-11T22:36:45Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![jeromedalbert](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/jeromedalbert/32/11998_2.png) [@jeromedalbert](https://discuss.rubyonrails.org/u/jeromedalbert)\
**Post date:** [May 12, 2020, 2:30am UTC](https://discuss.rubyonrails.org/t/database-fields-are-polluted-with-both-nil-and-empty-values-when-submitting-forms/74877/3 "2020-05-12T02:30:18Z")

</div>

Just to make sure this wasn’t just our app, I made a dummy new Rails 6.0.3 app [here](https://github.com/jeromedalbert/coconut-app-test-empty-fields) with:

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

```

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

 ![Screen Shot 2020-05-11 at 7.12.29 PM](https://global.discourse-cdn.com/rubyonrails/original/2X/a/ac3e948d66b38fe10aeec4d3bcbbe5b19180e0af.png)

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

 ![Screen Shot 2020-05-11 at 7.20.41 PM](https://global.discourse-cdn.com/rubyonrails/original/2X/e/e4be72dd89b0579d13b11036d00f9c17db279415.png)

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)`.

---

_[View the full topic](https://discuss.rubyonrails.org/t/database-fields-are-polluted-with-both-nil-and-empty-values-when-submitting-forms/74877)._
