Rails:4.2.5
我使用 STI 并需要获取所有子类。
但是在开发环境中,rails会进行延迟加载。因此Model.subclasses
和Model.descendants
始终返回[]
谷歌搜索发送给我问题:https://github.com/rails/rails/issues/3364
以上问题建议使用以下解决方案作为初始化程序:
ActionDispatch::Reloader.to_prepare do
Rails.application.eager_load!
end
在此之后,我得到了异常:
/Users/vmac1/.rvm/gems/ruby-2.2.3/gems/activemodel-4.2.5/lib/active_model/validations/validates.rb:120:in `rescue in block in validates': Unknown validator: 'PersenceValidator' (ArgumentError)
from /Users/vmac1/.rvm/gems/ruby-2.2.3/gems/activemodel-4.2.5/lib/active_model/validations/validates.rb:117:in `block in validates'
from /Users/vmac1/.rvm/gems/ruby-2.2.3/gems/activemodel-4.2.5/lib/active_model/validations/validates.rb:113:in `each'
from /Users/vmac1/.rvm/gems/ruby-2.2.3/gems/activemodel-4.2.5/lib/active_model/validations/validates.rb:113:in `validates'
from /Users/vmac1/rails_projects/survd/app/models/sub_service.rb:7:in `<class:SubService>'
from /Users/vmac1/rails_projects/survd/app/models/sub_service.rb:1:in `<top (required)>'
from /Users/vmac1/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:457:in `load'
from /Users/vmac1/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:457:in `block in load_file'
from /Users/vmac1/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:647:in `new_constants_in'
from /Users/vmac1/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:456:in `load_file'
from /Users/vmac1/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:354:in `require_or_load'
from /Users/vmac1/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:494:in `load_missing_constant'
from /Users/vmac1/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:184:in `const_missing'
from /Users/vmac1/rails_projects/survd/app/models/buffet_service.rb:1:in `<top (required)>'
from /Users/vmac1/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:457:in `load'
from /Users/vmac1/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:457:in `block in load_file'
from /Users/vmac1/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:647:in `new_constants_in'
from /Users/vmac1/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:456:in `load_file'
from /Users/vmac1/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:354:in `require_or_load'
from /Users/vmac1/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:317:in `depend_on'
from /Users/vmac1/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:233:in `require_dependency'
from /Users/vmac1/.rvm/gems/ruby-2.2.3/gems/railties-4.2.5/lib/rails/engine.rb:472:in `block (2 levels) in eager_load!'
from /Users/vmac1/.rvm/gems/ruby-2.2.3/gems/railties-4.2.5/lib/rails/engine.rb:471:in `each'
from /Users/vmac1/.rvm/gems/ruby-2.2.3/gems/railties-4.2.5/lib/rails/engine.rb:471:in `block in eager_load!'
from /Users/vmac1/.rvm/gems/ruby-2.2.3/gems/railties-4.2.5/lib/rails/engine.rb:469:in `each'
from /Users/vmac1/.rvm/gems/ruby-2.2.3/gems/railties-4.2.5/lib/rails/engine.rb:469:in `eager_load!'
from /Users/vmac1/rails_projects/survd/config/initializers/eagerload_subclasses.rb:2:in `block in <top (required)>'
from /Users/vmac1/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.5/lib/active_support/callbacks.rb:446:in `instance_exec'
from /Users/vmac1/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.5/lib/active_support/callbacks.rb:446:in `block in make_lambda'
from /Users/vmac1/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.5/lib/active_support/callbacks.rb:192:in `call'
from /Users/vmac1/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.5/lib/active_support/callbacks.rb:192:in `block in simple'
from /Users/vmac1/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.5/lib/active_support/callbacks.rb:504:in `call'
from /Users/vmac1/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.5/lib/active_support/callbacks.rb:504:in `block in call'
from /Users/vmac1/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.5/lib/active_support/callbacks.rb:504:in `each'
from /Users/vmac1/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.5/lib/active_support/callbacks.rb:504:in `call'
from /Users/vmac1/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.5/lib/active_support/callbacks.rb:92:in `__run_callbacks__'
from /Users/vmac1/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.5/lib/active_support/callbacks.rb:778:in `_run_prepare_callbacks'
from /Users/vmac1/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.5/lib/active_support/callbacks.rb:81:in `run_callbacks'
from /Users/vmac1/.rvm/gems/ruby-2.2.3/gems/actionpack-4.2.5/lib/action_dispatch/middleware/reloader.rb:83:in `prepare!'
from /Users/vmac1/.rvm/gems/ruby-2.2.3/gems/actionpack-4.2.5/lib/action_dispatch/middleware/reloader.rb:55:in `prepare!'
from /Users/vmac1/.rvm/gems/ruby-2.2.3/gems/railties-4.2.5/lib/rails/application/finisher.rb:50:in `block in <module:Finisher>'
from /Users/vmac1/.rvm/gems/ruby-2.2.3/gems/railties-4.2.5/lib/rails/initializable.rb:30:in `instance_exec'
from /Users/vmac1/.rvm/gems/ruby-2.2.3/gems/railties-4.2.5/lib/rails/initializable.rb:30:in `run'
from /Users/vmac1/.rvm/gems/ruby-2.2.3/gems/railties-4.2.5/lib/rails/initializable.rb:55:in `block in run_initializers'
from /Users/vmac1/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/tsort.rb:226:in `block in tsort_each'
from /Users/vmac1/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/tsort.rb:348:in `block (2 levels) in each_strongly_connected_component'
from /Users/vmac1/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/tsort.rb:429:in `each_strongly_connected_component_from'
from /Users/vmac1/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/tsort.rb:347:in `block in each_strongly_connected_component'
from /Users/vmac1/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/tsort.rb:345:in `each'
from /Users/vmac1/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/tsort.rb:345:in `call'
from /Users/vmac1/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/tsort.rb:345:in `each_strongly_connected_component'
from /Users/vmac1/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/tsort.rb:224:in `tsort_each'
from /Users/vmac1/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/tsort.rb:203:in `tsort_each'
from /Users/vmac1/.rvm/gems/ruby-2.2.3/gems/railties-4.2.5/lib/rails/initializable.rb:54:in `run_initializers'
from /Users/vmac1/.rvm/gems/ruby-2.2.3/gems/railties-4.2.5/lib/rails/application.rb:352:in `initialize!'
from /Users/vmac1/rails_projects/survd/config/environment.rb:5:in `<top (required)>'
from /Users/vmac1/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:274:in `require'
from /Users/vmac1/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:274:in `block in require'
from /Users/vmac1/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:240:in `load_dependency'
from /Users/vmac1/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:274:in `require'
from /Users/vmac1/.rvm/gems/ruby-2.2.3/gems/spring-1.6.3/lib/spring/application.rb:92:in `preload'
from /Users/vmac1/.rvm/gems/ruby-2.2.3/gems/spring-1.6.3/lib/spring/application.rb:143:in `serve'
from /Users/vmac1/.rvm/gems/ruby-2.2.3/gems/spring-1.6.3/lib/spring/application.rb:131:in `block in run'
from /Users/vmac1/.rvm/gems/ruby-2.2.3/gems/spring-1.6.3/lib/spring/application.rb:125:in `loop'
from /Users/vmac1/.rvm/gems/ruby-2.2.3/gems/spring-1.6.3/lib/spring/application.rb:125:in `run'
from /Users/vmac1/.rvm/gems/ruby-2.2.3/gems/spring-1.6.3/lib/spring/application/boot.rb:18:in `<top (required)>'
from /Users/vmac1/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /Users/vmac1/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from -e:1:in `<main>'
答案 0 :(得分:0)
错误persence
而不是presence
答案 1 :(得分:0)
正如其他人所说,实际错误是一个验证错误。但是对于原始问题,Rails 4.2急切加载生产by default中的所有类,因此您只需更改其他环境中的eager_load
和cache_classes
设置得到相同的行为。但是,这会使开发变得更加不愉快,因为每次进行代码更改时都必须重新启动服务器。
通过列出所有子类,您尝试解决的根本问题是什么?可能有另一种设计可以避免这种要求。