Rails 3 在启动时加载所有模型

编程入门 行业动态 更新时间:2024-10-26 14:28:23
本文介绍了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 在启动时加载所有模型

本文发布于:2023-11-27 22:58:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1639958.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:启动时   模型   加载   Rails

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!