0
0

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.

Rails | enums で 値を上書きする前の id を得る方法

Last updated at Posted at 2016-02-16

解決

メソッドではなく。ハッシュを使う。

person[:gender] # => 0

問題

たとえば、こういうモデルがある場合。

class Person < ActiveRecord::Base
  enum gender: { male: 0, female: 1 }
end

enum はオリジナルのメソッドを書き換えるので。
0 とか 1 とかの元の値は取れなくなってしまう。

person = Person.first

person.gender # => "male"

解決は冒頭のとおり。

環境

  • Rails 4.2.5

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

メンター受付

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?