在開發環境中,使用 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 中的以下模型,我的 gem app/models/identification.rb 的路徑:
class Identification < ApplicationRecord
self.table_name = 'pub_identification'
belongs_to :city, foreign_key: 'idcity'
end
但是,如果我更改代碼中的任何內容,我需要重新啟動我的服務器,因為我的 gem 中的模型沒有重新打開。我得到錯誤:
undefined local variable or method `has_paper_trail' for Identification:Class
該has_paper_trail方法是來自 mygem 的方法。它只有在我重新啟動時才有效。對此有什么建議嗎?
編輯:
寶石結構:
mygem/
├─ app/
│ ├─ models/
├─ config/
├─ lib/
uj5u.com熱心網友回復:
您不能在自動加載路徑中有兩個檔案定義相同的常量。
您的引擎定義了一個模型,而應用程式想要裝飾它。對于此用例,請查看此檔案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/452365.html
