轨道 6 + Zeitwerk,加载没有类的文件

发布于 2025-01-16 12:43:46 字数 226 浏览 2 评论 0原文

我们正在升级到 Rails 6,并且已经完成了使我们的应用程序能够与 Zeitwerk 配合使用的大部分工作。

我们确实有两个文件,它们是查询对象,存在于 app/queries 下,并且我们没有为它们定义类。它们仅与方法一起存在。

然而,Zeitwerk 希望该文件定义一个常量,但我们认为没有必要。

有什么方法可以告诉 Zeitwerk 我们要按原样加载这些文件吗?

We are upgrading to Rails 6, and have done most of the work to make our application work with Zeitwerk.

We do have two files, which are query objects, existing under app/queries and we don't define a class for them. They exists with methods only.

However, Zeitwerk expects the file to define a constant, but we don't deem it necessary.

Is there any way to tell Zeitwerk that we want to load these files as is?

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

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

发布评论

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

评论(1

凉城已无爱 2025-01-23 12:43:49

您可以告诉主自动加载器忽略它们:

# config/initializers/zeitwerk.rb
Rails.autoloaders.main.ignore(
  Rails.root.join("app/queries/foo.rb"),
  Rails.root.join("app/queries/bar.rb")
)

然后应用程序负责使用 require 或其他方式加载它们。

如果应忽略 app/queries 下的所有内容,您可以忽略目录本身:

# config/initializers/zeitwerk.rb
Rails.autoloaders.main.ignore(Rails.root.join("app/queries"))

You can tell the main autoloader to ignore them:

# config/initializers/zeitwerk.rb
Rails.autoloaders.main.ignore(
  Rails.root.join("app/queries/foo.rb"),
  Rails.root.join("app/queries/bar.rb")
)

And then the application is responsible for loading them using require or whatever.

If everything under app/queries should be ignored, you can ignore the directory itself:

# config/initializers/zeitwerk.rb
Rails.autoloaders.main.ignore(Rails.root.join("app/queries"))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文