如何让 Rails 3 在开发模式下重新加载 STI 类? [英] How to make Rails 3 reload STI classes in development mode?

查看:48
本文介绍了如何让 Rails 3 在开发模式下重新加载 STI 类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

切换到 Rails 3 后,我注意到我必须重新启动服务器才能使 STI 模型类随每个请求重新加载.例如,假设我有这个:

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

如果我对 Vehicle 进行更改,则更改会在下一个请求中加载.但是,如果我对 Car 进行更改,则必须重新启动服务器才能加载.

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?

我正在运行 WEBrick,但我并不致力于它.

I'm running WEBrick, but I'm not committed to it.

推荐答案

我们发现我们需要 zetetic 的解决方案和一些额外的代码来完成这项工作(至少在 Rails 3.0.9 中).对于上述问题,解决方案如下:

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:

在 config/environments/development.rb 中:

In config/environments/development.rb:

  config.after_initialize do
    ["vehicle"].each do|dep|
      require_dependency( (Rails.root + "app/models/#{dep}").to_s )
    end
  end

在 app/controllers/application_controller.rb 中:

In app/controllers/application_controller.rb:

class ApplicationController < ActionController::Base
  if Rails.env == 'development'
    require_dependency( (Rails.root + "app/models/vehicle").to_s )
  end
...

development.rb 中的代码处理类的初始加载,ApplicationController 中的代码处理后续请求.

The code in development.rb handles the initial loading of the class, and the code in ApplicationController handles subsequent requests.

这篇关于如何让 Rails 3 在开发模式下重新加载 STI 类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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