2016年1月7日 星期四

[Rails] ActiveRecord::Enum

神奇的 enum 功能.
http://edgeapi.rubyonrails.org/classes/ActiveRecord/Enum.html
可以用這個gem 取代 https://github.com/brainspec/enumerize

class Conversation < ActiveRecord::Base
  enum status: [ :active, :archived ]
end

# conversation.update! status: 0
conversation.active!
conversation.active? # => true
conversation.status  # => "active"

# conversation.update! status: 1
conversation.archived!
conversation.archived? # => true
conversation.status    # => "archived"

# conversation.status = 1
conversation.status = "archived"

conversation.status = nil
conversation.status.nil? # => true
conversation.status      # => nil

【下列文章您可能也有興趣】

沒有留言: