I’m trying to set up a couple of validations – unclear what the best approach is:
attribute :allowed_types, {:array, :atom} do
allow_nil? false
default [:value_add, :non_value_add]
end
attribute :default_type, :atom do
allow_nil? false
end
validations do
# this works but is clunky (what if allowed set is > 2?)
validate one_of(:allowed_types, [[:value_add, :non_value_add], [:value_add], [:non_value_add]])
# this doesn't work ... :allowed_types is supposed to be an attribute, not an atom... :/
validate one_of(:default_type, [:allowed_types])
end
Potentially pretty obvious, but the intention of the two validations:
- The first is to make sure the
:allowed_types
conforms to some combination of one or more of a set (currently, just [:value_add, :non_value_add]) - The second is to make sure that the value of
:default_type
is one of the types specified in the attribute:allowed_types
I can live with #1 for now (until the list grows, but then it becomes a problem). #2 seems like it may require a custom validation, but I’m unsure if that’s correct… or how to write one quite yet…