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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:45:41+00:00 2026-05-16T02:45:41+00:00

I have a Rails app representing a card game. There are several different types

  • 0

I have a Rails app representing a card game. There are several different types of card, and a few expansions for the game, each providing more cards. So I’m using a single cards table and STI, with each expansion having a models subdirectory and appropriately-named module. Like this:

app/models
- card.rb
+ base_game
  - foo.rb
    - class BaseGame::Foo < Card
  - bar.rb
    - class BaseGame::Bar < Card
+ exp_one
  - thing.rb
    - class ExpOne::Thing < Card

This works well.

I would like to be able to get a list of all the cards in each expansion. The constants method of Module provides a neat way of doing this –

class Card
  def self.base_game_cards 
    BaseGame.constants.map {|c| ("BaseGame::" + c).constantize}.select {|c| c.instance_of?(Class) and c.superclass == Card}
  end
end

…except that Rails only loads a given model definition when it’s first referenced, so Card.base_game_cards is usually [].

I’ve tried working round this by forcing a preload of all the models, by adding a file into config/initializers:

preloader.rb:

# Preload all card models from all expansions. This allows us to dynamically
# determine card types by reflection.
Dir.glob("#{RAILS_ROOT}/app/models/*/*.rb").each do |file| 
  require file
end

But this doesn’t seem to work either, and I can’t work out why. By use of raise and inpect, I’ve determined that when I call Card.base_game_cards from a controller, the result is still usually []. Very occasionally it’s correct just after server restart, but refreshing the page sends it back to []. If I use script\console, the list is always correct. I’ve tried moving the block of requires to the top of the controller class definition, but that makes no difference either.

Now, I’m perfectly willing at this stage to go for another solution (probably just inflecting the list of filenames returned by glob), but I’d at least like to know why this method isn’t working. Can anyone shed some light (or – even better – make it work)?

  • 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-16T02:45:41+00:00Added an answer on May 16, 2026 at 2:45 am

    yeah unfortunately active_support is “on demand” not “pre-demand” so you’ll probably have to use your preloader. Unfortunately if your app is in development mode it’s probably set to “reload classes with each request” so you’ll have to disable that convenient feature of rails, or make sure you re-load those files when needed.

    -r

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Rails app that uses STI to handle different types of Users,
In my Rails 3.2.6 app, I have a model representing a collection of data
I have a rails app, where Im having a drop box like feature-set. Each
I have rails app which has a list of users. I have different relations
I have rails app which are working perfectly in the local computer. But when
I have a Rails app running Mongoid on Heroku and I need to set
I have a Rails app with the usual application_controller, and a controller (and model)
I have a Rails app that is served by Apache and Passenger, using only
I have my rails app, which is filled with products, set up with a
I have a Rails app and trying to make a remote form, so far

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.