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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T13:08:27+00:00 2026-05-20T13:08:27+00:00

In development I am trying to gather all models within my app by including

  • 0

In development I am trying to gather all models within my app by including a method within ActiveRecord::Base classes so they can configure the models and it will give me a hook to add that model to a global array.

module EngineName
  module ActiveRecordExtensions
    extend ActiveSupport::Concern

    included do
      def self.inherited(klass) #:nodoc:
        klass.class_eval do
          def self.config_block(&block)
            abstract_model = EngineName::AbstractModel.new(self)
            abstract_model.instance_eval(&block) if block

            EngineName::Models.add(abstract_model)
          end
        end
      end
    end
  end
end

My EngineName::Models class is just a wrapper that holds all models.

module EngineName
  class Models
    class << self
      def all
        @models ||= []
      end
      alias_method :models, :all

      def navigation
        @models - excluded_navigation_models
      end

      def routed
        @models - excluded_route_models
      end

      # Creates an array of models if none exists. Appends new models
      # if the instance variable already exists.
      def register(klass)
        if @models.nil?
          @models = [klass]
        else
          @models << klass unless @models.include?(klass) || excluded_models.include?(klass)
        end
      end
      alias_method :add, :register
    end
  end
end

On each refresh though, the config_block method within my model gets called and in turn appends the same model over and over within my global array of models.

As you can see down below, whenever I loop through all my models, it will keep on appending itself.

enter image description here

Is there any way to cache certain classes within my engine? Or is there a flaw within my approach of registering models with a hook within the model itself?

  • 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-20T13:08:28+00:00Added an answer on May 20, 2026 at 1:08 pm

    The issue is that in your development environment your models get reloaded on every request so that changes to these classes take effect and you don’t have to re-boot your server every time you make a change to your source code. You can see this behavior in the console:

    User.object_id
    => 2203143360
    reload!
    => true
    User.object_id
    => 2212653160
    

    What this means is that when you call @models.include?(klass) you are actually checking your current instantiation of that object against one from a previous request. What you’ll notice is that over time your memory gets bloated because these objects will not be deleted – since garbage collection will keep them around because of the reference to them in your @models instance variable. This won’t be a problem in production, because classes are only loaded once, but it will cause you problems in development.

    To get around this I would recommend doing something like this:

    module EngineName
      class Models
        class << self
          def all
            @models ||= {}
          end
          alias_method :models, :all
    
          def register(klass)
            if @models.nil?
              @models = {klass.name => klass}
            else
              @models[klass.name] = klass unless excluded_models.keys.include?(klass.name)
            end
          end
          alias_method :add, :register
        end
      end
    end
    

    Using a hash will let you keep track of models by their names, and whenever a new version of a model comes around it will replace the old out-dated version. This should help in your development environment. To get a list of all the models you’ll simply use @models.values and to get a list of the model names you’ll simply use @models.keys.

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

Sidebar

Related Questions

We're trying to get Sphinx to index all three of our servers (development, staging
I'm trying to find out whether Appcelerator's Titanium is good for iPad app development,
I’m thinking about trying some development for the iPhone, is it possible to install
Im trying to get into some basic JavaFX game development and I'm getting confused
I am trying to get my development environment up and running, and I am
Our development uses lots of open-source code and I'm trying to figure out what
I am trying to install PHP onto my development box (XP SP3 / IIS
Platform: Windows XP Development Platform: VB6 When trying to set an application title via
I'm trying to ensure a script remains running on a development server. It collates
I'm trying to move my database.mdf file from a development environment to a SQL

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.