1

使用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
4

2 回答 2

1

使用初始化方法怎么样?,它不是自我记录的,但应该可以工作。

于 2009-02-10T21:40:36.827 回答
1

我的猜测是,由于垃圾邮件检查是在设置初始状态之前进行的,因此您的 :spam 和 :ham 转换无法执行,因为 :from 条件表明状态应该是 :submitted、:rejected 或 :批准(但事实上,它是零)。初始状态是在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

这应该在设置spam_check后触发事件,initial_state并将 ste 状态设置为:approvedor : rejected

于 2009-02-10T21:49:57.397 回答