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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:26:24+00:00 2026-05-20T18:26:24+00:00

I am using Memcached as an Object Store with my Rails application where I

  • 0

I am using Memcached as an Object Store with my Rails application where I store search results which are User objects in memcached

Now when I fetch the data out I get the Memcached Undefined Class/Module Error. I found a solution for this problem in this blog

http://www.philsergi.com/2007/06/rails-memcached-undefinded-classmodule.html

 before_filter :preload_models
  def preload_models
    Model1
    Model2
  end

which recommends pre-loading the models before hand. I would like to know if there is a more elegant solution to this problem and are there any drawbacks in using the preloading technique.

Thanks in advance

  • 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-20T18:26:24+00:00Added an answer on May 20, 2026 at 6:26 pm

    I had this problem as well and I think i came up with a nice solution.

    You can overwrite the fetch method and rescue the error and load the right constants.

    module ActiveSupport
      module Cache
        class MemCacheStore
          # Fetching the entry from memcached
          # For some reason sometimes the classes are undefined
          #   First rescue: trying to constantize the class and try again.
          #   Second rescue, reload all the models
          #   Else raise the exception
          def fetch(key, options = {})
            retries = 2 
            begin
              super
            rescue ArgumentError, NameError => exc         
              if retries == 2
                if exc.message.match /undefined class\/module (.+)$/
                  $1.constantize
                end
                retries -= 1
                retry          
              elsif retries == 1
                retries -= 1
                preload_models
                retry
              else 
                raise exc
              end
            end
          end
    
          private
    
          # There are errors sometimes like: undefined class module ClassName.
          # With this method we re-load every model
          def preload_models     
            #we need to reference the classes here so if coming from cache Marshal.load will find them     
            ActiveRecord::Base.connection.tables.each do |model|       
              begin       
                "#{model.classify}".constantize 
              rescue Exception       
              end     
            end       
          end
        end
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

can I store objects in the servers memory to cache data using django, or
I'm using Devart to execute DB object and Store Procedure mapping. Now i want
I'm considering using memcached (at some point) in my application i'm currently developing. Eventually,
Is there any way of using Memcached for fragment caching in Rails?
Now that NOSQL or object only storage systems like MongoDB or memcached are really
We have a data driven ASP.NET website which has been written using the standard
we're using memcache in several of our Rails applications. Now I was wondering: Is
My Python application currently uses the python-memcached API to set and get objects in
We're using memcached from PHP. I've been trying to figure out why we're seeing
I'm using Memcached in a CakePHP application to cache MySQL result sets. One result

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.