mardi 29 octobre 2019

Rails3 create a global variable throughout application upon initialization

Currently we have a method in application_controller.rb which initialises a global variable or returns its value if it has been initialized.

Problem is this is being called upon each request, and its redundant because the variable is initialized after the first request.

How can I move this method to application.rb and have it run once upon Rails' initialisation

    config.after_initialize do
      begin
        @global_user = User.find(100)
      rescue => e
        Rails.logger.info "Error finding the Global User"
      end
    end

Aucun commentaire:

Enregistrer un commentaire