2016-12-04 19 views
0

我有一個會話變量,存儲組ID爲。我已在我的應用程序控制器之前,過濾器設置會話[:GROUP_ID]如何讓我的會話變量可用於具有線程變量的模型?

# In my application controller 
before_filter :save_request_path 

def save_request_path 
    if request.params[:group_id] 
    session[:group_id]= request.params[:group_id] 
    end 
end 

在這一點上,我試圖訪問一個模型裏面這個會話變量。我已經看到了一個使用Thread變量爲current_user執行此操作的示例。我似乎並不瞭解這樣的例子,但是https://www.zorched.net/2007/05/29/making-session-data-available-to-models-in-ruby-on-rails/。 如何在我的具體情況下使用線程變量來做到這一點?

回答

0

如果你真的想要使用線程變量,我建議使用request_store,它通過使用線程局部變量來避免併發問題。

這是你如何存儲的值:

def save_request_path 
    if request.params[:group_id] 
    RequestStore.store[:group_id] = request.params[:group_id] 
    end 
end 

這是你如何訪問:

RequestStore.store[:group_id]