Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inclusion validation fails when provided a symbol instead of a string

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?

like image 613
CodeOverload Avatar asked Jul 19 '12 21:07

CodeOverload


1 Answers

you can define a setter for status eg:

    def status=(new_status)
      super new_status.to_s
    end
like image 196
lisowski.r Avatar answered Oct 02 '22 22:10

lisowski.r