Help us understand the problem. What is going on with this article?

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

More than 5 years have passed since last update.

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

ms2sato
何でも屋だが、最近はRails中心。JavaScript好き。レビューやタスク分解が多くて近頃仕事ではコード書けていない。仕事でもプライベートでもコラボできる人が常に欲しい人。 会社では人を育てるの中心で http://circlearound.co.jp/training/ こんなのやってます。 独り言はこっち http://ms2sato.circlearound.co.jp
http://circlearound.co.jp
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした