2
votes

I'm defining a whole bunch of models in a gem, which I bundle into a Rails project. I want to be able to monkeypatch those model classes with code held in files in app/models AND have app/models/* reload on every request like usual.

I've actually solved the above problem. The issue I have now is that the solution makes my app SLOW -- screens taking 30-40 seconds to load in dev mode vs a second or two before. Here's what I've got now in an initializer:

model_init = lambda{
  path = File.expand_path(File.dirname(__FILE__) + "../../../app/models")

  Dir.glob("#{path}/*.rb").each do |path|
    require path
  end
}

ActionDispatch::Callbacks.to_prepare(&model_init)

Can anyone give info on how I can speed this up? I'm on Rails 3.1.3, but if the active_reload stuff in 3.2.0-rc1 might solve this, I'm happy to upgrade. Thanks in advance for any help!

1

1 Answers

1
votes

There is a known bug in a Rails 3.1 - every asset is recompiled on a request. So if your app has many assets, you get slow responses in dev mode. This is fixed in 3.2.