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

  • SEARCH
  • Home
  • 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 7833679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T12:50:08+00:00 2026-06-02T12:50:08+00:00

I have some domain data in my rails application that I’m trying to create

  • 0

I have some domain data in my rails application that I’m trying to create some constants for. This is something I came across in Dan Chak’s Enterprise Rails Chapter 7. I have done the following:

G = Rating.find_by_rating_code('G')

then when I use Rating::G the appropriate Rating Record is returned. This works great. My problem arises due to the fact that I have 150 ratings codes. So instead of typing the above line of code for each of my ratings codes, I was hoping to use a little meta programing to avoid cluttering up my model with a lot of redundant code. Therefore I tried the following.

RATINGSCODES = %w(G A AB TR P ...)

class << self
RATINGSCODES.each do |code|
code.constantize = Rating.find_by_rating_code(code)
end
end

Unfortunately, I’m getting an uninitialized constant error and can’t figure out where I’m going wrong. Am I approaching this the right way. I also tried using const_get but that didn’t seem to work either.

At the suggestion below, I have also tried using

code.const_set = Rating.find_by_rating_code(code)

This yielded the following error:

undefined method `const_set=' for "G":String
  • 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-06-02T12:50:32+00:00Added an answer on June 2, 2026 at 12:50 pm

    Use const_set:

    class Rating
       RATINGSCODES = %w{ G A AB TR P }
    
       RATINGSCODES.each do |code|
         const_set code, code
       end
    end
    #=> ["G", "A", "AB", "TR", "P"] 
    
    p Rating::G
    #=> "G" 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assume some domain and view objects (that have no common base class) public class
I have been spending some time on this. My problem involves bringing back domain
I have cached some domain instances and queries in my grails application. I would
Most projects have some sort of data that are essentially static between releases and
I have this js file serving from some domain say foobar.com at http://foobar.com/static/js/main.js: $(document).ready(function()
I have some data in time domain, the time separation between each element is
I have some pdf files located on a http server: Like: http://domain.com/files/file1.pdf http://domain.com/files/file1.pdf http://domain.com/files/file1.pdf
I have seen on the web some domain names having prefix of ww2 or
In a RIA Domain service I have added some utility functions. For instance we
I have been making some research in the domain of servers for a website

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.