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

240 views Asked by At

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?

2

There are 2 answers

11
Francesco Belladonna On BEST ANSWER

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)

4
Prakash Murthy On

When a gem is included in a Gemfile, the associated require .. statement is automatically called.

See http://www.justinweiss.com/blog/2014/10/13/how-does-rails-handle-gems/ for an explanation of how rails handles gems.

From that blogpost:

So, Bundler will look in your Gemfile for gems belonging to each of those groups, and call require on each of the gems it finds. If you have a gem nokogiri, it’ll call require "nokogiri" for you. And that’s why your gems usually just work in Rails, without any extra code on your part.