如何让Rails 3在开发模式下重新加载STI类?
切换到 Rails 3 后,我注意到我必须重新启动服务器才能使 STI 模型类随着每个请求而重新加载。例如,假设我有这样的情况:
# app/models/vehicle.rb
class Vehicle < ActiveRecord::Base
end
# app/models/car.rb
class Car < Vehicle
end
如果我对 Vehicle
进行更改,则该更改将在下一个请求时加载。但如果我对 Car
进行更改,我必须重新启动服务器才能加载它。
有解决这个问题的想法吗?
我正在运行 WEBrick,但我并不致力于它。
After switching to Rails 3, I noticed that I have to reboot my server to make STI model classes reload with each request. For example, suppose I have this:
# app/models/vehicle.rb
class Vehicle < ActiveRecord::Base
end
# app/models/car.rb
class Car < Vehicle
end
If I make a change to Vehicle
, the change is loaded on the next request. But if I make a change to Car
, I have to reboot my server for it to load.
Any ideas on fixing this?
I'm running WEBrick, but I'm not committed to it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我们发现我们需要 zetetic 的解决方案和一些额外的代码来完成这项工作(至少在 Rails 3.0.9 中)。对于上述问题,解决方案如下所示:
在 config/environments/development.rb 中:
在 app/controllers/application_controller.rb 中:
development.rb 中的代码处理类的初始加载,ApplicationController 中的代码处理后续请求。
We found that we needed both zetetic's solution and some additional code to make this work (at least in Rails 3.0.9). For the above problem, the solution would look something like:
In config/environments/development.rb:
In app/controllers/application_controller.rb:
The code in development.rb handles the initial loading of the class, and the code in ApplicationController handles subsequent requests.
我相信这个问题可以通过在控制器中添加
require_dependency 'vehicle'
来解决。I believe this can be solved by adding
require_dependency 'vehicle'
in the controller.使用rails 3.0.3和passenger 3,我根本看不到这一点。如果将您的应用程序更新到 3.0.3 不能解决问题,我会放弃 WEBrick。
无论如何,我个人建议使用 WEBrick 以外的其他东西。很长一段时间以来,Passenger 一直是我开发+生产时选择的服务器。
Using rails 3.0.3 and passenger 3, I don't see this at all. If updating your app to 3.0.3 doesn't fix it, I'd move off of WEBrick.
I personally recommend using something other than WEBrick anyway. Passenger has been my server of choice for development + production for quite a while now.