Normalise array in Rails 7.1

Jorge Alvarez - Oct 10 '23 - - Dev Community

One of the (many) good features of Rails 7.1 is the normalizes method. This is how you can use it with fields of type array.

In this example there is a field called folders that we want to normalise removing blank entries and ensuring all entries are down cased.

normalizes :folders, with: ->(values) { (values || []).map(&:downcase).reject(&:blank?) }
Enter fullscreen mode Exit fullscreen mode
. . . . . . . . . . . . . . . .