如何让Rails 3在开发模式下重新加载STI类?

发布于 2024-10-10 09:32:00 字数 363 浏览 9 评论 0原文

切换到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

下雨或天晴 2024-10-17 09:32:00

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

在 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 中:

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

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:

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

In app/controllers/application_controller.rb:

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

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

ˉ厌 2024-10-17 09:32:00

我相信这个问题可以通过在控制器中添加 require_dependency 'vehicle' 来解决。

I believe this can be solved by adding require_dependency 'vehicle' in the controller.

随风而去 2024-10-17 09:32:00

使用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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文