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 7534595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:04:00+00:00 2026-05-30T06:04:00+00:00

I am using Ruby on Rails 3.1 and I would like to understand how

  • 0

I am using Ruby on Rails 3.1 and I would like to understand how to solve the following issue in the right way when I state constants.

I have 2 classes:

class Class1 < ActiveRecord::Base
  CONSTANT_CLASS_1_A = Class2::CONSTANT_CLASS_2_A # => 222
  CONSTANT_CLASS_1_B = 111
end

class Class2 < ActiveRecord::Base
  CONSTANT_CLASS_2_A = 222
  CONSTANT_CLASS_2_B = Class1::CONSTANT_CLASS_1_B # => 111
end

When I try to “use”/”load” the Class1 I get the following error:

NameError in ...
uninitialized constant Class1::CONSTANT_CLASS_2_B

However, if I state the below code (note the constant statement order) it will work:

class Class1 < ActiveRecord::Base
  CONSTANT_CLASS_1_B = 1
  CONSTANT_CLASS_1_A = CONSTANT_CLASS_2_A * CONSTANT_CLASS_3_B # => 3
end

I know that I can state the CONSTANT_CLASS_1_B before the CONSTANT_CLASS_1_B (at least to “solve” the problem at this time), but is it correct? That is, is there a way to solve my issue in a “right” and “performant” way (for example, loading all Class1 constants before to retrieve other constants)? Is it advisable to use the above code?

Bonus: There is a convention for this kind of issues? If so, what is it?

  • 1 1 Answer
  • 0 Views
  • 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-30T06:04:02+00:00Added an answer on May 30, 2026 at 6:04 am

    The right way to do this is probably have just one constant referencing the data you’re looking for. Doubling up definitions leads to problems like this and additionally will make your code hard to read and understand.

    If there isn’t a really compelling reason to have the same constant in two places — and there almost never is — don’t define CONSTANT_CLASS_2_B or CONSTANT_CLASS_1_A. If you want them, reference them from the other model like this:

    Class2::CONSTANT_CLASS_2_A
    Class1::CONSTANT_CLASS_1_B
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Ruby on Rails 3.0.7 and I would like to understand how
I am using Ruby on Rails 3 and I would like to state an
I am using Ruby on Rails 3.1 and I would like to have an
I'm using Authlogic with my Ruby on Rails App. I would like user to
I am using Ruby on Rails 3.1.0 and I would like to properly generate
I am using Ruby on Rails 3.1 and I would like to know (for
I am using Ruby on Rails 3.0.9 and I would like to seed the
I am using Ruby on Rails 3 and I would like to retrieve a
I am using Ruby on Rails 3 and I would like to disable and
I am using Ruby on Rails 3.1 and I would like to order a

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.