DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 966,904 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Seiei Miyagi
Seiei Miyagi

Posted on

How to define almost global variable and almost top level method in Rails

# app/models/application_container.rb
require 'singleton'

class ApplicationContainer
  include Singleton
end
# app/controllers/application_controler.rb
class ApplicationController < ActionController::Base
  delegate_missing_to :container

  cattr_accessor :container, default: ApplicationContainer.instance
end
# app/models/application_record.rb
class ApplicationRecord < ActiveRecord::Base
  self.abstract_class = true

  delegate_missing_to :container

  cattr_accessor :container, default: ApplicationContainer.instance
end

Top comments (0)

πŸ‘‹ Hey, my name is Noah and I’m the one who set up this ad. My job is to get you to join DEV, so if you fancy doing me a favor, I’d love for you to create an account.

If you found DEV from searching around, here are a couple of our most popular articles on DEV: