Cannot load models into a mounted motor on rails

I have a rails project that uses mongo db, and I wrote a mounted engine called "report_service".

I used it like this in the main rails project:

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

I do not want this pearl to load when the rails project is initialized, so I added the parameter :require => false .

But in my rails console after executing require 'report_service' I can not find the models in this stone.

 [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>' 

Here is my directory and gem.service code:

report_service / Library / report_service.rb

 require "active_record/railtie" require "report_service/engine" module ReportService end 

report_service / Library / report_service / engine.rb

 module ReportService class Engine < ::Rails::Engine isolate_namespace ReportService end end 

report_service / application / models / report_service / rs_exam.rb

 module ReportService class RsExam < ActiveRecord::Base end end 
+4
source share
1 answer

Discard this update. Just add require "report_service/rs_exam" to your report_service.rb .

 require "active_record/railtie" require "report_service/engine" require "report_service/rs_exam" module ReportService end 

My reasoning is that what happens is that you load the report_service/rs_exam , so you will get an uninitialized persistent error. Because I'm looking at the console exit.

Gem loading works great.

 require 'report_service' => true 

ReportService :: Engine loaded perfectly.

 [4] pry(main)> ReportService::Engine => ReportService::Engine 

But then when you try to load rs_exam

 [5] pry(main)> ReportService::RsExam NameError: uninitialized constant ReportService::RsExam from (pry):5:in `<main>' 

you get your uninitialized persistent error because it is not required. Try this and let me know how you get on

+4
source

Source: https://habr.com/ru/post/1496506/


All Articles