Raylar geliştirme ortamında, cache_classes
kapalıdır; böylece kodu app/
altında değiştirebilir ve sunucuyu yeniden başlatmadan değişiklikleri görebilirsiniz.Etki alanı nesnelerini cache_classes ile middleware'den nasıl kullanırım?
Tüm ortamlarda, ara yazılım sadece bir kez oluşturulur. Böyle ortakatmanını var ise:
class MyMiddleware
def initialize(app)
@app = app
end
def call(env)
env['model'] = MyModel.first
end
end
ve ben config/environments/development.rb
bunu:
config.cache_classes = false # the default for development
config.middleware.use MyMiddleware
sonra hep aşağıdaki hatayı alırsınız:
A copy of MyMiddleware has been removed from the module tree but is still active!
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:414:in `load_missing_constant'
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:96:in `const_missing'
/Users/me/projects/my_project/lib/my_middleware.rb:8:in `call'
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.2/lib/action_controller/middleware_stack.rb:72:in `new'
...
sorun olduğunu Sistem yükleme zamanında bir kezörneği oluşturulur, ancak her çağrıya MyModel
sınıfı yeniden yüklenir.
'MyModel'.constantize.first
çalıştı, ama o yenisiyle sorunu değiştirir:
You have a nil object when you didn't expect it!
The error occurred while evaluating nil.include?
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/active_record/attribute_methods.rb:142in `create_time_zone_conversion_attribute?'
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/active_record/attribute_methods.rb:75:in `define_attributes_methods'
...