我正在创建一个简单的博客系统。 在我的博文模型中,我有布尔:发布字段:我发布帖子时将此字段设置为true。当字段更改为true时Rails验证
我想在以下情况下触发验证:发布为真:我的验证必须检查:title的存在(以避免空的发布后发布)。
我想用这样的:
class Post < ActiveRecord::Base
validates :title, presence: true, if: :can_publish?
def can_publish?
self.published == true
end
end
这句法确定或有更简单的东西使用?
self.published == true not symbol(:true) – Neelesh
您可以省略'== true'和'self.'。 – user2503775