我的所有模型中都混有一个类方法.在评估模型类时调用该方法.不幸的是(对我来说),这似乎是按需的,只要在开发环境中需要模型.如何让 Rails 在启动时加载所有模型?这甚至是可取的吗?
I have a class method mixed in to all my models. the method gets called when the model class is evaluated. unfortunately (for me), this seems to be on-demand, whenever the model is needed in development env. how can have rails load all the models at start up? is this even advisable?
class Foo < ActiveRecord::Base include Acl register_acl # i need this to be called for all models at start up end基本上, register_acl 接受一些模型想要访问控制的动作"参数.假设 Foo 的一个动作是管理",并且系统需要在启动时意识到这个动作.我认为在模型中是最自然的地方有这个逻辑.
Basically, the register_acl takes a few arguments of "actions" that the model would like to be access controlled. Suppose one of the action of Foo is "manage" and the system needs to be aware of this action at start up. I think in the model is the most natural place to have this logic.
谢谢!
推荐答案在应用程序范围内执行此操作的正确方法是在您的配置中打开 cache_classes.默认情况下,它在开发中处于关闭状态,但在生产中处于打开状态.
The correct way to do this application-wide is to turn on cache_classes in your configuration. By default it's off in development but on in production.
如果你想偶尔这样做:
Rails.application.eager_load!更多推荐
Rails 3 在启动时加载所有模型
发布评论