validates_inclusion_of因未知原因而失败

时间:2013-06-13 17:16:18

标签: 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_blank => false

    def set_defaults
        self.status ||= 'draft'
    end
end

然而,当我去创建一个这样的:

article = Article.new( {
            "title" => "test",
            "body" => "test" ,
            "excerpt" => "test"
            } )

article.save

失败并提供错误:

[:status, "is not included in the list"]

我正在试图弄清楚为什么没有设置默认值。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我认为你想要before_validation :set_defaults , :on => :create,运行验证然后再创建回调。