Zeitwerk 需要重新加载每个更改

发布于 2025-01-17 19:45:33 字数 865 浏览 3 评论 0原文

在开发环境中,使用Zeitwerk时,Ruby代码的每个更改都需要重新启动服务器,因为Zeitwerk没有加载MyGEM中定义的类。我有一个使用Rails型号的公司的宝石。其中一些模型已重新打开,我们添加了一些方法。

我的应用中有以下模型。

require_dependency("#{Mygem::Engine.root.join('app','models','identification.rb')}")

class Identification
  include Cacheable
  has_paper_trail

  def some method
  end
end

以及mygem中的以下模型,我的宝石应用程序/型号/识别的路径。rb:

class Identification < ApplicationRecord
  self.table_name = 'pub_identification'

  belongs_to :city, foreign_key: 'idcity'
end

但是,如果我更改代码中的任何内容,则需要重新启动服务器,因为我的宝石中的模型不会重新打开。我收到错误:

undefined local variable or method `has_paper_trail' for Identification:Class

has_paper_trail方法是MyGem的方法。而且只有在我重新启动时才有效。有什么提示吗?

编辑:

宝石结构:

mygem/
├─ app/
│  ├─ models/
├─ config/
├─ lib/

In development enviroment, when using zeitwerk, every change in ruby code needs to restart the server, because zeitwerk isn't loading classes defined in mygem. I have a gem, used by my company that uses rails models. And some of that models are reopened, and we add some methods.

I have the following model in my app.

require_dependency("#{Mygem::Engine.root.join('app','models','identification.rb')}")

class Identification
  include Cacheable
  has_paper_trail

  def some method
  end
end

And the following model in mygem, path of my gem app/models/identification.rb:

class Identification < ApplicationRecord
  self.table_name = 'pub_identification'

  belongs_to :city, foreign_key: 'idcity'
end

But if I change anything in my code, I need restart my server, because model from my gems aren't reopened. I get the error:

undefined local variable or method `has_paper_trail' for Identification:Class

The has_paper_trail method is a method from mygem. And it only works if I restart. Any tips for this?

EDIT:

Gem Structure:

mygem/
├─ app/
│  ├─ models/
├─ config/
├─ lib/

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

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

发布评论

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

评论(1

强辩 2025-01-24 19:45:33

您不能有两个文件在自动加载路径中定义相同常数。

您的引擎定义了模型,并且该应用程序希望装饰它。对于此用例,请查看此文档

You cannot have two files defining the same constant in the autoload paths.

Your engine defines a model, and the application wants to decorate it. For this use case, please have a look at this documentation.

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