My model is like:
class Client < ActiveRecord::Base
VALID_STATES = %w(active suspended closed)
validates :status, :inclusion => { :in => VALID_STATES }
end
Ths validation works fine if the status came from a form (as a string), but i like to do something like:
@client.status = :active
which throws an error that the status isn't in the list, obviously that's because %w
doesn't generate an array of symbols too, Is there a work around this without ending up using strings?
you can define a setter for status eg:
def status=(new_status)
super new_status.to_s
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With