Ruby on rails 3 无法在轨道上的可安装引擎中加载模型

Ruby on rails 3 无法在轨道上的可安装引擎中加载模型,ruby-on-rails-3,mongodb,bundle,rails-engines,Ruby On Rails 3,Mongodb,Bundle,Rails Engines,我有一个rails项目,它使用mongo db,我编写了一个名为“report_service”的可装载引擎 我在main rails项目中这样使用它: gem'report\u service',:git=>'git@xx.com:report_service.git',:branch=>:master,:require=>false 我不想在rails项目初始化时加载这个gem,所以我添加了:require=>false选项 但是在我的rails控制台中,在执行require'report\

我有一个rails项目,它使用mongo db,我编写了一个名为“report_service”的可装载引擎

我在main rails项目中这样使用它:

gem'report\u service',:git=>'git@xx.com:report_service.git',:branch=>:master,:require=>false

我不想在rails项目初始化时加载这个gem,所以我添加了
:require=>false
选项

但是在我的rails控制台中,在执行
require'report\u 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\u service/lib/report\u service/engine.rb

module ReportService
  class Engine < ::Rails::Engine
   isolate_namespace ReportService
  end
end
模块报告服务
类引擎<::Rails::引擎
隔离命名空间报告服务
结束
结束
report_service/app/models/report_service/rs_exam.rb

module ReportService
  class RsExam < ActiveRecord::Base
  end
end
模块报告服务
类RsExam
放弃该更新。只需在您的
报告服务.rb
中添加
要求“报告服务/rs\u考试”

require "active_record/railtie"
require "report_service/engine"
require "report_service/rs_exam"
module ReportService
end
我的推理是,正在发生的事情是,加载模型
报告\u服务/rs\u检查
,这就是为什么会出现未初始化的常量错误。因为查看控制台输出

加载gem工作正常

require 'report_service'
=> true
ReportService::引擎加载正常

[4] pry(main)> ReportService::Engine
=> ReportService::Engine
但是当你尝试加载rs_考试时

[5] pry(main)> ReportService::RsExam
NameError: uninitialized constant ReportService::RsExam
from (pry):5:in `<main>'
[5]pry(main)>报告服务::RsExam
NameError:未初始化的常量ReportService::RsExam
from(pry):5:in`'

您得到未初始化的常量错误,因为它不是必需的。试试看,让我知道你是如何进入
类引擎的:Rails::Engine
是由
Rails插件new blorgh--mountable
默认生成的,当我在
Rails::Engine
之前删除分号时,它会得到相同的错误,这不是问题,因为
之后需要“报告服务”
,我可以加载
ReportService::Engine
,问题是我无法在Engine中加载模型,如以下错误
[5]pry(main)>ReportService::RsExam name错误:未初始化的常量ReportService::RsExam
您是否正确安装了它,它是否插入到您的路由中?如果我使用
gem'report\u service',:git=>'git@xx.com:report_service.git',:branch=>:master
,一切正常,但我添加了:required=>false”选项,因为我不希望在rails项目中加载此geminitialized@RichieMin我的另一件事是你正在做的
:require=>false
。我希望你知道这是怎么回事。你说的是在你的gem文件中,你将gem注册为一个依赖项,而不需要它。据我所知,如果我是正确的,那么您之所以这样做是因为您只想注册它,而不是加载它。所以无论如何,在你的生产模式中,你需要它。再次添加我建议的require行。@RichieMin如果这个答案对你的问号有帮助,那么它就完成了。你试过在初始化时加载你的宝石吗?然后尝试?如果我删除“:required=>false”就可以了,但这不是我想要的
[5] pry(main)> ReportService::RsExam
NameError: uninitialized constant ReportService::RsExam
from (pry):5:in `<main>'