简体   繁体   中英

How do I autoload an external gem in Rails?

I recently externalized my rails models outside of my rails-app into a gem (models_gem).

When my rails project imports the models_gem, i have to do a: require 'model' , in order to use the model. How can I automatically import all the models that are in my models_gem ??

you can do it in Gemfile eg:

gem 'models_gem', require: 'models_gem'

or fix your gem and in lib/models_gem.rb

ActiveRecord::Base.send(:include, ModelsGem)

Thats depends how your gem is made.

When you require a gem, it requires lib/gem_name.rb. If you require all the models in that file, in theory they should be loaded in your application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM