Search code examples
ruby-on-railsrubyrubygemsbundlertire

require 'tire' gem not being mentioned in a model class but usage of it still works in rails app


Im reviewing code of a rails application I inherited and I'm no expert in rails. In the gemfile for the application there is: gem 'tire'. Great, but I expect to use require 'tire' in models or controllers to be able to use the libraries this gem provides. According to the documentation of a particular gem it says it needs to be included as a mixin in a model class.

 include Tire::Model::Search
 include Tire::Model::Callbacks

Or the documentation mentions to use require 'tire' to the model class where you plan to use the tire gem. But in the project I don't see any of these require/includes in the models. The models are just using the api without requiring/including like so:

ret = Tire.index "icrd" do 
            import [icrdObj]

            refresh
        end

How does this work? Where should I look in the rails project to get an understanding of why require or include is not needed? Is there some kind of autoloading going on?


Solution

  • Bundler has a lot of nice methods. Bundler.setup ensures you can require gems easily in your app.

    Bundler.require(:default, :production) for example (it accepts group names) requires all gems in the gemfile automatically by performing require 'tire', require 'rails' and such, for you.

    You can skip auto-require for some gems by adding require: false near your gem row in gemfile, like

    gem 'rails', require: false

    Check Bundler.require documentation (which is placed under groups)