我们在很多模型中使用AASM,但我们正在考虑简化模型。我们想要做的事情之一就是将所有通知内容从模型中移到观察者中。与事件挂钩的观察者
因此,考虑:
class ClarificationRequest < ActiveRecord::Base
include AASM
aasm_initial_state :open
# States
aasm_state :open
aasm_state :closed
# Events
aasm_event :close, :after => :notify_closed do transitions :to => :closed, :from => [:open,:replied], :guard => :can_close? end
end
我想这一点,但没有运气:
class ClarificationRequestObserver < ActiveRecord::Observer
observe :clarification_request
def after_close
puts '############### message from observer!!!!!'
end
end
我如何可以移动:notify_closed以观察员?
Thx!
。卡林
我们用这种方法遇到的一个主要问题是,我们实际上想要创建也需要具有current_user_id的事件(一种审计模型),并且据我所知,并不容易或不适合方式包括在模型...因此观察员? – khelal 2010-02-10 10:55:11
虽然这可能并不理想,但您可以像这样获取用户标识:在用户模型中添加cattr_accessor:current_user。在应用程序控制器中添加一个调用def的before_filter set_current_user User.current_user = self.current_user – tsdbrown 2010-02-10 11:55:08