0

我有以下模型:

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"]

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

4

1 回答 1

0

我想你想要before_validation :set_defaults , :on => :create,验证运行,然后在创建回调之前。

于 2013-06-13T17:19:37.093 回答