在 Rails 中,我曾经想过“Foo.all”被执行,然后“models/foo.rb”被执行。已加载?

发布于 2024-09-28 10:30:20 字数 531 浏览 1 评论 0原文

在 Rails 中,如果我创建一个脚手架 foo,并执行 rake db:migrate,那么现在

app/models/foo.rb

就被创建了。所以如果我去

script/console   (or rails console for Rails 3)

输入

$".grep /foo/i

它是空的。 $" 是加载的文件。所以 foo.rb 还没有加载。

现在我做了一个 Foo.all,并且 foo.rb 现在应该加载了。

但是当我重复 grep,它仍然显示空数组 - 这是为什么?

(另一方面,我可以 grep for pp,但它什么也没显示,但在我之后require 'pp',然后 grep 显示它)

In Rails, if I create a scaffold foo, and do rake db:migrate, then now

app/models/foo.rb

is created. So if I go to

script/console   (or rails console for Rails 3)

and type

$".grep /foo/i

it is empty. $" is the loaded files. So foo.rb is not loaded yet.

Now I do a Foo.all, and foo.rb should be loaded now.

But when I repeat the grep, it is still showing empty array -- why is that?

(On the other hand, I can grep for pp, and it shows nothing, but after I require 'pp', then the grep shows it)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

归途 2024-10-05 10:30:20

这取决于你使用的环境。

在开发时,只有当您需要时才需要并加载,并且每次都重新加载。
在生产模式下,只需要一次并加载一次。它在您的应用程序启动时加载。应用程序/模型上的所有模型均已加载。

执行此操作的配置是:config.cache_classes = true

It's depend which environement you use.

In development time, it's require and load only when you want and reload each time.
In production mode, it's only require once and load once. It's load on start of you application. All model on app/models are load.

The configuration to do that is : config.cache_classes = true

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文