Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 190925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:17:51+00:00 2026-05-11T16:17:51+00:00

Someone who understands how rails caching works can really help me out here. Here’s

  • 0

Someone who understands how rails caching works can really help me out here. Here’s the code, nested inside of the Rails::Initializer.run block:

config.after_initialize do
  SomeClass.const_set 'SOME_CONST', 'SOME_VAL'
end

Now if I run script/server and make a request, everything is dandy. However, on the second request to my Rails app, all goes to hell with an unitialized constant error. In production mode, I can make the second request successfully, meaning the constant is still there.

I’ve fixed the problem by changing the above to:

config.after_initialize do
  require 'some_class' # in RAILS_ROOT/lib/some_class.rb
  SomeClass.const_set 'SOME_CONST', 'SOME_VAL'
end

But now that means whenever I make a change to some_class.rb, I have to restart the server. Is there any way to set constants in an environment file and have them work correctly in development mode? Why does the constant exist on the first request, but not the following request?

UPDATE: Since environment.rb is only read when the Rails app is booted and I want both my lib files and models to be reloaded on each request, I was forced to move the constants into the some_class.rb file as follows:

if Rails.env.development?
  const_set 'SOME_CONST', 'SOME_DEVELOPMENT_VAL'
end

And in environments/production.rb, I have the old const_set code.

UPDATE: An even better method using config.to_prepare is detailed below.

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T16:17:51+00:00Added an answer on May 11, 2026 at 4:17 pm

    It only works on the first request in development mode because the classes are reloaded on each request. So on the first request the constant is set in the initializer, and all is good. Then on the next request, it reloads the class WITHOUT rerunning the bit from your initializer, so the constant isn’t set from there on out.

    It works in production mode because the classes aren’t reloaded for each request, so you don’t lose that bit of class state each time.

    So you might want to set the constant either in the model, or in a config.to_prepare instead config.after_initialize. to_prepare is called before each request.

    In the model:

    class SomeClass < ActiveRecord::Base
      MY_CONST = "whatever"
    
      # You can access MY_CONST directly, but I tend to wrap them in a class 
      # method because literal constants often get refactored into the database.
      def self.my_const
        MY_CONST 
      end
    end
    

    In the config:

    # This will run before every single request. You probably only want this in
    # the development config.
    config.to_prepare do
      SomeClass.const_set 'SOME_CONST', 'SOME_VAL'
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I hope there's someone who can help me suggest a suitable data model to
Please would someone who understands VBA Arrays (Access 2003) help me with the following
I need some help from someone who understands JBoss Hostname Binding. I think the
Can someone who is way smarter than I tell me what I'm doing wrong..
How can I redirect someone who types http://domain.com/+abc (this should only apply to alphanumeric
I'm asking this question as someone who works for a company with a 70%
What's an efficient way for someone who knows PHP and Ruby on Rails to
I'd really appreciate if someone who ever dealt with Fortune's algorithm for generating Delaunay-triangulations
Could someone who is familiar with webkit please explain or point me in the
As someone who comes from the world of Object Orientation, I find it rather

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.