在使用AASM Gem时,是否可以在进入初始状态时调用方法?我希望在提交评论时调用spam_check方法,但它似乎不起作用。
class Comment < ActiveRecord::Base
include AASM
aasm_column :state
aasm_initial_state :submitted
aasm_state :submitted, :enter => :spam_check
aasm_state :approved
aasm_state :rejected
aasm_event :ham do
transitions :to => :approved, :from => [:submitted, :rejected]
end
aasm_event :spam do
transitions :to => :rejected, :from => [:submitted, :approved]
end
def spam_check
# Mark the comment as spam or ham...
end
end发布于 2009-02-10 21:49:57
我的猜测是,由于垃圾邮件检查是在设置初始状态之前进行的,您的:spam和:ham转换不能执行,因为:from条件说明状态应该是:submitted、:rejected或:approved (但实际上,它是空的)。初始状态是在before_validation_on_create回调上设置的,那么像这样尝试一下怎么样?
after_validation_on_create :spam_check
aasm_event :spam_check do
transitions :to => :approved, :from => [:submitted, :rejected], :guard => Proc.new {|c| !c.spam?}
transitions :to => :rejected, :from => [:submitted, :approved], :guard => 'spam?'
end
def spam?
# your spam checking routine
end这将在设置initial_state之后触发spam_check事件,并将ste状态设置为:approved或:rejected。
发布于 2009-02-10 21:40:37
使用initialize方法怎么样?,它不是自文档化的,但应该可以工作。
https://stackoverflow.com/questions/534191
复制相似问题