DEV Community

Augusts Bautra
Augusts Bautra

Posted on

TIL use `normalizes :field, apply_to_nil: true`

tl;dr

Use normalizes :boolean_field, apply_to_nil: true, with: -> { _1.present? }.

Today I got a nasty little surprise from the otherwise excellent normalizes macro. Turns out it does not normalize nil values out of the box. This bit me in the ass because I'm working on eliminating nil values from boolean fields and hadn't bothered checking whether it works. Now I need to review all the normalizes statements I've added recently, sigh.

You can read more about the macro on apidoc.

Top comments (0)