業界トップクラスの求人数を誇る転職エージェントPR

リクルートグループのコネクションを活かした非公開求人も充実、他にはない好条件の求人と出会える

31
24

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

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

Qiita Conference 2025 will be held!: 4/23(wed) - 4/25(Fri)

Qiita Conference is the largest tech conference in Qiita!

Keynote Speaker

ymrl、Masanobu Naruse, Takeshi Kano, Junichi Ito, uhyo, Hiroshi Tokumaru, MinoDriven, Minorun, Hiroyuki Sakuraba, tenntenn, drken, konifar

View event details
31
24

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?