0
1

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 3 years have passed since last update.

enum属性をもつカラムの値を取得する [初心者][rails]

Last updated at Posted at 2020-02-03

##enum属性をもつカラムの値を取得したい

モデルに記述するこういうのです:point_down_tone2:

enum status: {
    error: 0,
    shipping: 1,
    completed: 2,
  }

errorはなんらかの不具合、shippingは配送中、completedは配送完了

ちなみにstatusカラムの error: 0
errorがkey
0がvalue

##key取得方法
上記のようなstautsカラムをもつOrderモデルで

Order.shipping?

で取得可能
enumの便利なメソッドです:thumbsup_tone2:

###間違えた例

Order.status.shipping?

と記述してしまいエラーになっていました

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?