2011-08-16 99 views
0

呃。今天不是我的一天。在觀察員的rails expire_action?

我有一個觀察者在我們的Rails應用程序3.0倍

class EventObserver < ActionController::Caching::Sweeper 
    observe :live_event, :event 

    def after_update(event) 
    Rails.logger.debug "EventObserver::after_update #{event.class.name}.#{event.id}" 
    channels = Channel.being_used 
    inc=channels.reject {|c| !c.current_source.events.include?(event) } 
    inc.each do |c| 
     expire_action("channel_tickers/#{c.id}.js") 
     expire_action("channel_events/#{c.id}.js") 
     Rails.logger.debug "expired stuff for channel #{c.id}" 
    end 
    end 
end 

我做的看着類之一的更新和after_update獲取調用,但它沒有到期任何東西。我在日誌中看不到對expire_action的調用。

我確實看到:

expired stuff for channel 63 

所以它打的是代碼,而不是轟炸出來。

我想使用觀察者,因爲這些模型不是從控制器更新的,而是從內部更新的。

+0

你從哪裏進行更新? –

回答

1

經過一番徘徊後,我找到了答案。我加入這行我「after_update」方法的開頭:

@controller ||= ActionController::Base.new # <= this line is the key! without this nothing works 

你會得到沒有異常,但沒有這一點,是行不通的。我猜想它正在尋找是否存在@controller,如果是的話。