如何在开发环境中的STI中重新加载app / models / subdirectory中的文件 [英] How to reload files in app/models/subdirectory in dev environment for STI
问题描述
我使用了 Alex Reisner的博客文章。我已经让我的所有子类都使用超类的控制器,并通过序列化/存储来保存额外的属性。我在超类中添加了 model_name
和 self.select_options
方法,以及Alex博客中的预加载初始化程序。我还更改了_form视图助手和属性验证中的 collection_select
以使用 self.select_options
方法。我的所有子类都位于app / models / subfolder的单个文件中,尽管它们的名称空间不像SubFolder :: Subclass。
I implemented STI in one of my models, using some tips from Alex Reisner's blog post. I already had all my subclasses use the superclass's controller, with serialize/store to hold the extra attributes. I added the model_name
and self.select_options
methods to the superclass, and the preload initializer from Alex's blog. I also changed my collection_select
in the _form view helper and attribute validation to use the self.select_options
method. All my subclasses are in individual files in a app/models/subfolder, though they're not namespaced like SubFolder::Subclass.
然后我开始遇到问题。更改任何代码后, self.select_options
停止返回所有子类。它仅返回一个小子集或不返回任何子集。因此,由于验证和_form匹配,代码更改后,我无法编辑/更新模型。据我所知,当我更改任何代码时,Rails会重新加载环境,而不是子文件夹中的模型。
Then I start running into problems. Upon changing any code, self.select_options
stops returning all of the subclasses. It only returns a small subset or none. Thus due to the validation and _form tie-in, I can't edit/update my models after a code change. From what I could tell, when I change any code, Rails reloads the environment, but not the models in the subfolder.
我尝试将路由添加到config.autoload_paths < a href = https://stackoverflow.com/questions/3356742/best-way-to-load-module-class-from-lib-folder-in-rails-3/9819938#9819938>就像很多人建议的那样,但最终还是没用。
I tried adding the routes to config.autoload_paths like many suggest, but ultimately that didn't work.
所以最终,我想要:
- 可以解决自动加载的问题,因此我不必在每次更改后重新启动服务器
- 将其基于包含所有子项的子目录,以避免手动维护数组
- Rails 3.2.11,ruby 1.9.3p125,ubuntu 12.04.01,rvm
推荐答案
我最终结合了此 answer 和此一个和知识l = nofollow noreferrer>不可思议的底部的博客文章。 config.autoload_paths
似乎从没有帮助过,但我把它们留在了那里。关键部分是初始化程序,该初始化程序需要在启动时重新加载子目录中的所有文件。我在 require_dependency
上尝试了 load
,该方法不起作用。不必每次都重新加载确实很不错。
I ended up combining the code from this answer and this one and knowledge gleaned from the wondible blog posts at the bottom. The config.autoload_paths
never seemed to help anything, but I kept them in there. The key part is the initializer that requires all the files in the subdirectory on startup and then at each reload. I tried load
over require_dependency
, which didn't work. It's definitely been nice not having to reload all the time.
在application.rb
In application.rb
config.autoload_paths += %W(#{config.root}/app/models/configuration)
在开发中。rb
config.autoload_paths += Dir["#{config.root}/app/models/configuration/**"]
在preload_sti_models.rb
In preload_sti_models.rb
if Rails.env.development?
Dir.entries("#{Rails.root}/app/models/subfolder").each do |c|
require_dependency File.join("app","models", "subfolder", "#{c}") if c =~ /.rb$/
end
ActionDispatch::Reloader.to_prepare do
Dir.entries("#{Rails.root}/app/models/subfolder").each do |c|
require_dependency File.join("app","models", "subfolder", "#{c}") if c =~ /.rb$/
end
end
end
某些博客文章具有有用的信息
Some blog posts with useful information
- http: //wondible.com/2012/01/13/rails-3-2-autoloading-in-theory/
- > http://wondible.com/2011/12/30/rails-autoloading-cleaning-up- the-mess /
- > http://wondible.com/2011/12/23/give-rails-autoloading-a-boot-to-the-head/
- http://www.williambharding.com/blog/technology/rails-3-au toload-modules-and-classes-in-production /
- http://wondible.com/2012/01/13/rails-3-2-autoloading-in-theory/
- http://wondible.com/2011/12/30/rails-autoloading-cleaning-up-the-mess/
- http://wondible.com/2011/12/23/give-rails-autoloading-a-boot-to-the-head/
- http://www.williambharding.com/blog/technology/rails-3-autoload-modules-and-classes-in-production/
编辑:这是已知的东西。
这篇关于如何在开发环境中的STI中重新加载app / models / subdirectory中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!