【发布时间】:2016-01-03 05:54:10
【问题描述】:
我正在开发一个 Rails 应用程序,并希望根据 enum 验证一些字段。这是我尝试过的。但是我遇到了一些错误
class Listing < ActiveRecord::Base
enum status: [:draft, :published]
scope :draft, -> { where status: :draft }
scope :published, -> { where status: :published }
validates_presence_of :attribute1, :attribute2, :attribute3, unless: "status.draft?", on: :update
def publish!
self.update status: :published
end
end
我所有的status 字段都有一个default value of 0,即draft。当我update 列表出现此错误时。
未定义的方法“草稿?”对于“草稿”:字符串
它发生在@listing.update(params) 期间。有人可以告诉我这里做错了什么吗?
【问题讨论】:
标签: ruby-on-rails enums