Search code examples
ruby-on-railsrubyruby-on-rails-3ruby-on-rails-4ruby-on-rails-5

RubyOnRails : read parameter from the database once


How does this happen in RubyOnRails:
The parameter is stored in the database.
The program needs to read it from the database once upon user login and save them in a global variable.
While the user is working, he uses only this variable, and the program does not read parameter again from the DB, only the next time the user logs on to the system.
How it is implemented.
(edited)
What if there is a solution without sessions?
home_controller : is only one time run in start of user session ?


Solution

  • You can store it in the session hash

    class ApplicationController
      def my_variable
        session[:my_variable] ||= MyFile.find(1).my_variable
      end
    end  
    

    The app will only need to retrieve it once, the first time a logged in user references the my_variable method. After that, it will use the retrieved value even if the database value is changed.