LoginSignup
31
24

More than 5 years have passed since last update.

自分が気に入った enum 利用のスタイル(validationと一緒)

Last updated at Posted at 2015-04-10

enum 使ってみたかったので試していたら、指定が配列だったりするとどうもバリデーションがうまく通らず(数値でも文字列でも拒否られてしまった。なにか方法があるのだろうか)、いろいろこねくって下記の形かなと思った。

なんだか時間をやけに使ってしまったのでとりあえず記録しておく。多分暫くは自分はこのスタイルで書くと思う。

i18nとかいろいろあるけどそれは追々。

class ProjectUser < ActiveRecord::Base
  enum role: {worker: 0, manager: 1}
  enum status: {normal: 0}

  validates :role,
            inclusion: {in: ProjectUser.roles.keys}
  validates :status,
            inclusion: {in: ProjectUser.statuses.keys}

end

31
24
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
31
24