为什么我的线程变量在 Rails 中是间歇性的? [英] Why are my thread variables intermittent in Rails?

查看:29
本文介绍了为什么我的线程变量在 Rails 中是间歇性的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序控制器中有以下内容:

I have the following in my application controller:

before_filter :set_current_subdomain

  protected
    def set_current_subdomain
      Thread.current[:current_subdomain] = current_subdomain
      @account = Account.find_by_subdomain(current_subdomain)
    end

    def current_subdomain
      request.subdomain
    end

然后是我的一些模型中的以下内容:

and then the following in some of my models:

default_scope :conditions => { :account_id => (Thread.current[:account].id unless Thread.current[:account].nil?) }  

现在,这有效 - 在某些时候.例如,我加载一个索引方法并返回一个应用范围的记录列表,但有时也会得到一个空列表,因为 Thread.current[:account_id] 显示为 nil,即使请求中较早的查询正在工作使用相同的值.

Now, this works - some of the time. I for instance load up an index method and get back a list of records with the scope applied, but also sometimes get an empty list as Thread.current[:account_id] is coming out as nil, even though queries earlier in the request are working using the same value.

问题是,为什么这不起作用,是否有更好的方法来设置当前请求的全局变量?

Question is, why is this not working, and is there a better way to set a variable that's global to the current request?

推荐答案

操作 Thread 局部变量是一个非常糟糕的主意,只会导致悲伤、心痛和痛苦.无法保证请求处理的不同部分将由同一线程处理,因此,您的变量可能最终会丢失.

Manipulating the Thread local variables is a really bad idea and is going to lead to nothing but sadness, heartache, and pain. There's no guarantee that different parts of the request processing will be handled by the same thread, and because of this, your variables might end up getting lost.

Rails 约定是在 ApplicationController 的上下文中创建实例变量.简单来说,您真正要做的就是:

The Rails convention is to create instance variables in the context of ApplicationController. In simple terms, all you really do is this:

class ApplicationController < ActionController::Base
  before_filter :set_current_subdomain

  attr_reader :current_subdomain
  helper_method :current_subdomain

protected
  def set_current_subdomain
    @current_subdomain = request.subdomain

    @account = Account.find_by_subdomain(@current_subdomain)
  end
end

您创建的任何 @... 类型变量都将附加到与当前请求关联的 ApplicationController 实例.需要注意的是,每个请求都会发出一个相应控制器类的全新实例.

Any @... type variables you create will be attached to the instance of the ApplicationController associated with the current request. It's important to note that each request will be issued a brand-new instance of the appropriate controller class.

您可以随意创建任何您想要的实例变量,前提是它们不会与 Rails 本身使用的实例变量发生冲突,但一般而言,这种情况不会经常发生,而冲突通常发生在方法名称上.

You're free to create whatever instance variables you want provided they don't somehow conflict with those used by Rails itself but in general terms this doesn't happen very often and conflicts typically occur on method names instead.

类级别的实例变量将在启用缓存类"标志的环境中的请求之间保持不变.在开发环境中,每次发出请求时都会重新加载控制器类,以确保它反映源文件的当前状态.

Class-level instance variables will persist between requests in environments where the "cache classes" flag is enabled. In the development environment your controller class is re-loaded each time a request is made to ensure it reflects the current state of your source files.

这篇关于为什么我的线程变量在 Rails 中是间歇性的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆