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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:31:08+00:00 2026-06-10T18:31:08+00:00

Well, the problem is as follows. I have models hierarchy as follows: class A

  • 0

Well, the problem is as follows. I have models hierarchy as follows:

class A < ActiveRecord::Base
end

Class B < A
end

class C1 < B
end

class C2 < B
end

In one of the controllers I need to search for B, C1 or C2.
Rails does it if the related models are loaded.
So the sql query should (and will ideally) contain something like

B.find 224 
# => SELECT "bs".* FROM "bs" WHERE "bs"."type" IN ('B','C1','C2') AND "bs"."id" = 224 LIMIT 1

And that the exacly what is needed. However, it turns out that models are unloaded on each request and not loaded again(not sure for reasons of that).
In that case if the models are not in the memory, ActiveRecord will make query like

B.find 224 
# => SELECT "bs".* FROM "bs" WHERE "bs"."type" IN ('B') AND "bs"."id" = 224 LIMIT 1

If you will simply call for C1 or C2 before find their types will be included into sql

C1
B.find 224 
# => SELECT "bs".* FROM "bs" WHERE "bs"."type" IN ('B','C1') AND "bs"."id" = 224 LIMIT 1

I tried to load those models in the initializers. eval C1, require ‘app/models/c1.rb’, require_dependancy ‘app/models/c1.rb’ but neither of those works. Actually, they all worked for one time. Only one time after server was started. I suspect that there will be no such problem under production environment, but it very annoying.

An Ugly solution is to call C1 and C2 after B definition, in that case it worked as expected, But, as I said already, it is ugly

class B < A
end
C1, C2

Any better Ideas?

Update moved to answer.

  • 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-10T18:31:09+00:00Added an answer on June 10, 2026 at 6:31 pm

    Ok, I think I figured out some sort of solution. It is not very impressive, but it seems to be working:

    I requires to add a hack to the ActiveSupport::Dependencies.

    So here is the hack listing:

        #lib/system_hacks/active_support.rb
        module ActiveSupport #:nodoc:
          module Dependencies #:nodoc:
    
            #this array contains all the constants that have to be constantly loaded
            mattr_accessor :constantly_loaded_files
            self.constantly_loaded_files = []
    
            #redefining the method, with additional line
            def remove_unloadable_constants!
              autoloaded_constants.each { |const| remove_constant const }
              autoloaded_constants.clear
              Reference.clear!
              explicitly_unloadable_constants.each { |const| remove_constant const }
              #a hack
              constantly_loaded
            end
    
            #will simply make a call for each constants
            #in that case they will be updated... but still in the memory
            def constantly_loaded
              constantly_loaded_files.each do |file|
                file.to_s.classify.constantize
              end
            end
          end
        end
    

    And in the initializers a small file to load the hack and to assign an array of constants:

      #config/initializers/model_loader.rb
      require File.join(Rails.root, 'lib','system_hacks','active_support')
        module ActiveSupport #:nodoc:
          module Dependencies #:nodoc:
            self.constantly_loaded_files = [A, B, C1, C2]
          end
        end
    

    If you have any comments, I would happily listen for them.

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

Sidebar

Related Questions

Well, the problem is as follows: I have a WPF Application built using the
well I have that problem, im using a lightbox srcipt and im opening an
well i have most probably an extremly stupid problem but could not figure it
Well, here is my problem: I have an application that uses a custom Javascript
Well i have a web page hosted in a IIS 7.0 now the problem
I have a method as follows (from a class than implements TBB task interface
Hi friends hope all r doing well. I have a problem while reading xml
Hi friends hope all r doing well. I have a problem while reading xml
Hi everybody out there! My question is as follows: I have a template class
We have a well-defined problem that points to a problem with Microsoft's JDBC 2.0

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.