9
9

More than 5 years have passed since last update.

Rails4.1 Enum

Last updated at Posted at 2014-05-01

Rails4.1の新機能 ActiveRecored Enum についての理解と誤解

class Bug < ActiveRecord::Base
  enum status: {
    unverified: 0,
    confirmed: 1,
    in_progress: 3,
    resolved: 4,
    rejected: 5,
    reopened: 6
  }
end

こんなモデルがあったとして statusを responsedにする場合、intでもいいし、シンボルでもOK
rb
bug = Bug.first
bug.status = 6
bug.status = :reopened

んで、値を評価したいときは。。。

bug.status = :unverified
bug.status == :unverified
=> false

false になる。

なるほど、ちょっと誤解していた。
モデルの属性の状態を表すのではなくモデルそのものの状態を表すということなのですね。

bug.unverified?
=> true
9
9
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
9
9