我正在使用 Ruby on Rails 3.2.9,我只是想使用state_machine gem。我有以下陈述:
# Model class attributes are:
#
# [String] status
# [Boolean] checkin_1
# [Boolean] checkin_2
#
class Article < ActiveRecord::Base
state_machine :attribute => :status, :initial => :unconfirmed do
state :confirmed, :value => 'confirmed'
state :unconfirmed, :value => 'unconfirmed'
event :confirm do
transition :unconfirmed => :confirmed
end
event :unconfirm do
transition :confirmed => :unconfirmed
end
end
end
我想在confirm
触发事件checkin_1
和/或checkin_2
值为false
. 也就是说,鉴于我正在尝试confirm
以下每个对象:
<#Article id: 1, :status: 'unconfirmed', checkin_1: false, checkin_2: false>
<#Article id: 1, :status: 'unconfirmed', checkin_1: false, checkin_2: true>
<#Article id: 1, :status: 'unconfirmed', checkin_1: true, checkin_2: false>
然后我想避免保存对象并分别添加错误消息,如下所示:
"can not be confirmed if it is not checked for 1 and 2"
"can not be confirmed if it is not checked for 1"
"can not be confirmed if it is not checked for 2"
我该怎么做?