CodeOverload
CodeOverload

Reputation: 48505

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?

Upvotes: 5

Views: 2375

Answers (1)

lisowski.r
lisowski.r

Reputation: 3761

you can define a setter for status eg:

    def status=(new_status)
      super new_status.to_s
    end

Upvotes: 7

Related Questions