我在rails 4应用程序中使用aasm(以前称为acts_as_state_machine
)gem。我的Post
模型
...
aasm column: :state do
state :pending_approval, initial: true
state :active
state :pending_removal
event :accept_approval, :after => Proc.new { |user| binding.pry } do
transitions from: :pending_approval, to: :active
end
end
...
当我调用@post.accept_approval!(:active, current_user)
并触发后回调时,在我的控制台中,我可以检查user
是什么(传递到Proc中),它是nil
!
这里发生了什么?调用此转换的正确方法是什么?
答案 0 :(得分:5)
在章节回调中查看aasm文档。
...
aasm column: :state do
state :pending_approval, initial: true
state :active
state :pending_removal
after_all_transition :log_all_events
event :accept_approval, after: :log_approval do
transitions from: :pending_approval, to: :active
end
end
...
del log_all_events(user)
logger.debug "aasm #{aasm.current_event} from #{user}"
end
def log_approval(user)
logger.debug "aasm log_aproove from #{user}"
end
您可以使用所需的参数调用事件:
@post.accept_approval! current_user
答案 1 :(得分:1)
它适用于当前版本(4.3.0):
Document
答案 2 :(得分:1)
event :accept_approval do
transitions from: :pending_approval, to: :active
end
post.accept_approval!{post.set_approvaler(current_user)}
block to bang method will be call after transition success, if any activerecord operation, it will be wrap into transition transaction,
your can require a lock to prevent concurrency problem with option
requires_lock: true
.