Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Thread.local的陷阱[:当前用户]_Ruby On Rails_Ruby On Rails 3_Thread Safety_Authlogic - Fatal编程技术网

Ruby on rails Thread.local的陷阱[:当前用户]

Ruby on rails Thread.local的陷阱[:当前用户],ruby-on-rails,ruby-on-rails-3,thread-safety,authlogic,Ruby On Rails,Ruby On Rails 3,Thread Safety,Authlogic,此问题与: 具体来说,我想在一个Model.rb中启用对当前用户的访问@moif留下了一条评论,说这个解决方案不是线程安全的,我已经读到使用这个过程还有额外的注意事项 我的问题是——如果我要补充: def self.current_user Thread.local[:current_user] end def self.current_user=(usr) Thread.local[:current_user] = usr end 对于一个Model.rb(仅少量和不经常使

此问题与:

具体来说,我想在一个
Model.rb
中启用对
当前用户的访问@moif留下了一条评论,说这个解决方案不是线程安全的,我已经读到使用这个过程还有额外的注意事项

我的问题是——如果我要补充:

def self.current_user
    Thread.local[:current_user]
end

def self.current_user=(usr)
    Thread.local[:current_user] = usr
end
对于一个
Model.rb
(仅少量和不经常使用),我的应用程序的真实含义是什么?我还需要做些什么来确保它的健康


设置:Rails 1.9、Rails 3.0、Heroku、Authlogic。

我不确定我是否同意您选择的路径。我同意另一篇文章的观点,将当前用户传递给模型是不合适的,但我不会为此使用Thread.local。原因如下:

  • 开发人员喜欢使用技术解决方案,没有比Thread.local更接近系统的了。在使用Thread.locals之前,它们是非常棘手的,如果您没有正确地使用它,那么您将花费无数的时间试图解决问题,更不用说解决方案了。也很难找到能够理解Thread.local的复杂性并能够彻底测试代码的测试人员。事实上,我想知道有多少开发人员为类似的东西进行了可靠的rspec测试(或等效测试)。这个解决方案的“成本”可能不值得

  • 我想我会看看你想做什么,看看是否有更简单的解决办法。例如,我可以立即想到两个(在您的情况下可能有效或无效):

  • a) 使用外键将历史记录表连接到用户表。“属于,有很多”;或

    b) 通过历史记录上的attr_访问器传递用户名,并在创建对象时设置该用户名

    好问题。我很想看看答案。我在当前项目中遇到过这个问题,我真的不喜欢使用thread对象来传递变量——我的做法与上面的类似,这让我感觉……肮脏。