我有一个使用 mongo db 的 rails 项目,并且我编写了一个名为“report_service”的可挂载引擎。
我在 main rails 项目中这样使用它:
gem 'report_service', :git => 'git@xx.com:report_service.git', :branch => :master, :require => false
我不想在 rails 项目初始化时加载这个 gem,所以我添加了这个:require => false
选项。
但是在我的 rails 控制台中,在我执行之后require 'report_service'
,我在这个 gem 中找不到模型。
[1] pry(main)> ReportService
=> ReportService
[2] pry(main)> ReportService::Engine
NameError: uninitialized constant ReportService::Engine
from (pry):2:in `<main>'
[3] pry(main)> require 'report_service'
=> true
[4] pry(main)> ReportService::Engine
=> ReportService::Engine
[5] pry(main)> ReportService::RsExam
NameError: uninitialized constant ReportService::RsExam
from (pry):5:in `<main>'
这是我的 report_service gem 目录和代码:
report_service/lib/report_service.rb
require "active_record/railtie"
require "report_service/engine"
module ReportService
end
report_service/lib/report_service/engine.rb
module ReportService
class Engine < ::Rails::Engine
isolate_namespace ReportService
end
end
report_service/app/models/report_service/rs_exam.rb
module ReportService
class RsExam < ActiveRecord::Base
end
end