ruby Rails 5.2枚举新用法
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby Rails 5.2枚举新用法相关的知识,希望对你有一定的参考价值。
create_table :conversations do |t|
t.column :status, :integer, default: 0
end
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
Conversation.where(status: [:active, :archived])
Conversation.where.not(status: :active)
class Conversation < ActiveRecord::Base
enum status: { active: 0, archived: 1 }
end
Conversation.statuses[:active] # => 0
Conversation.statuses["archived"] # => 1
Conversation.where("status <> ?", Conversation.statuses[:archived])
class Conversation < ActiveRecord::Base
enum status: [:active, :archived], _suffix: true
enum comments_status: [:active, :inactive], _prefix: :comments
end
conversation.active_status!
conversation.archived_status? # => false
conversation.comments_inactive!
conversation.comments_active? # => false
class Conversation < ActiveRecord::Base
enum status: [:active, :archived], _suffix: :comment
end
conversation.comments_inactive!
conversation.comments_active? # => false
以上是关于ruby Rails 5.2枚举新用法的主要内容,如果未能解决你的问题,请参考以下文章
ruby Rails 5.2读取配置文件并实例化全局对象
Ruby on Rails全栈课程5.2 项目上线--在云服务器上配置Ruby On Rails环境
Ruby on rails 5.2 - 带有活动存储的背景图像
Rails 5.2 API - 在JSON中返回枚举值
在 Ruby on Rails 项目中将“关注点”存储在哪里? (轨道 5.2+)
如何按此哈希数组进行分组(可枚举)[Ruby,Rails]