Rails 3 在启动时加载所有模型 [英] Rails 3 loading all models on startup

查看:37
本文介绍了Rails 3 在启动时加载所有模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的所有模型中都混有一个类方法.在评估模型类时调用该方法.不幸的是(对我来说),这似乎是按需的,只要在开发环境中需要模型.如何让 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 在启动时加载所有模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆