Ruby on rails ActiveRecord对象在EventMachine回调中不工作
在测试Rails应用程序时,我遇到了以下问题 使用EventMachine发出异步HTTP请求时,由于某些原因,我无法在回调中使用ActiveRecord对象。 每当我尝试访问该对象时,都会出现以下异常:Ruby on rails ActiveRecord对象在EventMachine回调中不工作,ruby-on-rails,ruby,activerecord,eventmachine,thin,Ruby On Rails,Ruby,Activerecord,Eventmachine,Thin,在测试Rails应用程序时,我遇到了以下问题 使用EventMachine发出异步HTTP请求时,由于某些原因,我无法在回调中使用ActiveRecord对象。 每当我尝试访问该对象时,都会出现以下异常: /home/user/.rvm/gems/ruby-1.8.7-p371/gems/activerecord-2.3.18/lib/active_record/attribute_methods.rb:144:in `create_time_zone_conversion_attribute?
/home/user/.rvm/gems/ruby-1.8.7-p371/gems/activerecord-2.3.18/lib/active_record/attribute_methods.rb:144:in `create_time_zone_conversion_attribute?': You have a nil object when you didn't expect it! (NoMethodError)
You might have expected an instance of Array.
The error occurred while evaluating nil.include?
from /home/user/.rvm/gems/ruby-1.8.7-p371/gems/activerecord-2.3.18/lib/active_record/attribute_methods.rb:75:in `define_attribute_methods'
from /home/user/.rvm/gems/ruby-1.8.7-p371/gems/activerecord-2.3.18/lib/active_record/attribute_methods.rb:71:in `each'
from /home/user/.rvm/gems/ruby-1.8.7-p371/gems/activerecord-2.3.18/lib/active_record/attribute_methods.rb:71:in `define_attribute_methods'
from /home/user/.rvm/gems/ruby-1.8.7-p371/gems/activerecord-2.3.18/lib/active_record/attribute_methods.rb:257:in `method_missing'
如果我从回调中删除ActiveRecord对象,它可以正常工作。不幸的是,我真的需要他们是可访问的,所以删除不是一个选项
我是否访问对象进行阅读或写作并不重要
我编写的代码如下所示:
http = EM::HttpRequest.new(url).get(:query => query)
http.callback do
....
object.status = 'delivered'
object.save!
....
end
至于环境,我有以下设置:
- Ubuntu 12.04 LTS
- Ruby 1.8.7-p371
- 轨道2.3.18
- 精简服务器1.5.0
- ActiveRecord 2.3.18
- EventMachine 1.0.0
提前谢谢 看来我所要做的就是修改我的开发配置文件。
设置config.cache\u classes=true修复了这个问题。你能展示一下如何获得“object”吗?这太疯狂了,我不得不维护一些旧的Rails 2.3项目。你知道为什么我们会有这个bug吗?不幸的是没有。由于这个修复程序运行得很好,我没有必要再深入研究了。是的,但在开发过程中,每次进行更改时,我们都需要重新启动服务器。或者你知道更好的方法吗?是的,你必须在更改后将cache_类设置为true后重新启动它。但这对我来说从来都不是问题,因为重启只需要几秒钟,而且不经常需要。