We just ran into a really thorny issue of null vs. “” when sending JSON to the client. Basically, putting a null in the JSON vs a
"" might have very different results! as in CRASH!
Here’s a detailed write-up I grabbed from the Rogues Parley forum:
- Here’s my take — the UI has no concept of displaying nil vs blank, so we should never send nil to the UI in the JSON. Rails will always send back a blank string.
- On the DB side, we could maintain consistency with one of several Rails gems that either force “” or NULL in the DB.
- We could also enforce consistency of no nulls at the DB level by specifying the options of
null: false, default: ""in the DB.
This is about as annoying as ‘\’ versus ‘/‘ and I think Oracle had it right in this case.
What’s our best recommendation?
It’s worth considering that this issue really doesn’t matter for standard form based rails apps. The UI simply returns empty strings when posting, and it gets blanks when display.