The inclusion of verification fails when providing a character instead of a string

My model is similar:

class Client < ActiveRecord::Base
  VALID_STATES = %w(active suspended closed)
  validates :status, :inclusion => { :in => VALID_STATES }
end

Validation works fine if the status is obtained from the form (as a string), but I like to do something like:

@client.status = :active

which throws an error that the status is not in the list, obviously, because it %walso does not generate an array of characters. Is there any work around this, not ending with using strings?

+5
source share
1 answer

you can define a setter for status, for example:

    def status=(new_status)
      super new_status.to_s
    end
+7
source

All Articles