Zeitwerk 需要重新加载每个更改
在开发环境中,使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能有两个文件在自动加载路径中定义相同常数。
您的引擎定义了模型,并且该应用程序希望装饰它。对于此用例,请查看此文档 。
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.