Ruby on rails 验证因未知原因失败的\u包含\u
我有以下型号:Ruby on rails 验证因未知原因失败的\u包含\u,ruby-on-rails,ruby-on-rails-3,activerecord,Ruby On Rails,Ruby On Rails 3,Activerecord,我有以下型号: class Article < ActiveRecord::Base attr_accessible :body, :date_time, :excerpt, :permalink, :title, :premium, :status before_create :set_defaults validates_inclusion_of :status , :in => %w(draft pending published) , :allow_b
class Article < ActiveRecord::Base
attr_accessible :body, :date_time, :excerpt, :permalink, :title, :premium, :status
before_create :set_defaults
validates_inclusion_of :status , :in => %w(draft pending published) , :allow_blank => false
def set_defaults
self.status ||= 'draft'
end
end
它失败并提供错误信息:
[:status, "is not included in the list"]
我试图弄明白为什么没有设置默认值。有什么想法吗?我想你需要在验证之前执行
:设置默认值,:打开=>:创建
,验证运行,然后再创建回调。这是可行的,尽管在rails的更高版本中是这样的:验证之前:设置默认值,:打开=>:创建
。另外,我只是在初始化后尝试了,这同样有效。我想知道哪一种方法更好。如果您可以在验证之前将其更改为:设置默认值,:on=>:create
上次我在初始化之后检查时有一些性能问题,我会将其标记为正确的,所以如果可以的话,我通常会避免它,然而,在某个时刻,可以创建并保存一个对象,而无需验证,这将跳过默认设置。在我看来,最好的方法似乎相当依赖于你的应用程序和公差。
[:status, "is not included in the list"]