我有这个项目模型:
class Project < ActiveRecord::Base
validates :status, :inclusion => { :in => ['active', 'closed'] }
validates :title,
:presence => true,
:length => { :in => 4..30 }
before_save :set_default_status_if_not_specified
private
def set_default_status_if_not_specified
self.status = 'active' if self.status.blank?
end
end
如果我像这样创建一个新对象:
Project.create!(:title => 'Test 2', :pm_id => 1)
我收到以下错误:验证失败:状态未包含在列表中 但状态字段应在保存前填写。