3

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.

rlkw1024
  • 6,455
  • 1
  • 36
  • 65

3 Answers3

2

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.

Andrew H
  • 517
  • 6
  • 14
1

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

zetetic
  • 47,184
  • 10
  • 111
  • 119
  • I believe require_dependency is part of a private API. Also, it seems to me that this shouldn't be necessary, as STI and autoloading are theoretically supported out of the box. I'm hoping there's some solution that involves a global config or fixing my development server. – rlkw1024 Jan 11 '11 at 04:07
  • I agree it shouldn't be necessary, but it is. STI has a few quirks that need workarounds -- this is one of them. – zetetic Jan 11 '11 at 07:09
0

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.

BM5k
  • 1,210
  • 10
  • 28
  • definitely not server related... unicorn and thin here... rails 3.0.9 similar issues... – todd Jul 07 '11 at 02:15