DEV Community

Augusts Bautra
Augusts Bautra

Posted on

2

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)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay