I'm trying to implement to validations on a given model array-like field, using the Enumerize Gem. I want to:
validates :field, presence: true
)It seems that when I provide a list containing an empty string, the presence validator fails. See this example.
class MyModel
include ActiveModel::Model
extend Enumerize
enumerize :cities, in: %w(Boston London Berlin Paris), multiple: true
validates :cities, presence: true
end
# Does not behave as expected
a = MyModel.new(cities: [""])
a.cities.present? # => false
a.valid? # => true, while it should be false.
It seems to work in some other cases (for instance when you provide a non empty string that is not in the Enum). For instance
# Behaves as expected
a = MyModel.new(cities: ["Dublin"])
a.cities.present? # => false
a.valid? # => false
Is there a workaround available to be able to use both Enumerize validation and ActiveModel presence validation?
Thanks!
The enumerize
gem is saving your multiple values as an string array. Something like this: "[\"Boston\"]"
. So, with an empty array you have: "[]"
. The presence
validator uses blank?
method to check if the value is present or not. "[]".blank?
returns false
obviously.
So, you can try some alternatives:
validates :cities, inclusion: { in: %w(Boston London Berlin Paris) }
Add a custom validator
validate :ensure_valid_cities
def ensure_valid_cities
errors.add(:cities, "invalid") unless cities.values.empty?
end