Restrict values that can *not* be assigned
elsapet opened this issue · 3 comments
elsapet commented
Is there a way to restrict the values that can not be assigned to attributes or associations?
For example, if I wanted user.name
to be any value except admin
or administrator
, I could do something like this:
class User < ActiveRecord::Base
non_assignable_values_for :name do
['admin', 'administrator']
end
end
And if there isn't a way, would this be worth a pull request?
Thanks for a great gem, by the way :)
codener commented
Sounds interesting, definitely. I think @henning-koch should decide on this.
triskweline commented
Hi @elsapet . I think getting this into the gem would involve a lot of pain because of questions like:
- What would happen if we call
user.assignable_values
to receive the list of all possible values? - What would happen if the same model has both
assignable_values
andnon_assignable_values
? What if they contradict each other?
I would implement your use case above with Rails' built-in validates_exclusion_of
macro.