在我的模特中我有
class Test < ActiveRecord::Base
before_validation :set_pending, on: :create
validates :status, presence: true, inclusion: { in: %w(passed failed pending) }
.
.
.
private
def set_pending
status = 'pending'
end
end
在我的rails控制台中,我正在尝试创建一个新的测试
Test.create!(user_id: 9, runnable: true)
但我收到错误
ActiveRecord::RecordInvalid: Validation failed: Status can't be blank
我做错了什么?我在ruby 2.1.8和rails 4.0.13上。感谢
答案 0 :(得分:6)
我个人的偏好是在设置变量时非常明确,实际上你错过了self
def set_pending
self.status = 'pending'
end