views:

345

answers:

4

I have a Rails app that uses STI to handle different types of Users, such as:

class Admin < User
  ...
end

I want to use memcached, but I keep getting the dreaded "unknown class/module" error. I've tried pre-loading all of my ActiveRecord models to no avail. The first request works as normal, but the first pull from memcached errors out. I've followed these instructions to no avail either.

Is using memcached possible with single table inheritance?

+1  A: 

I think this is due to lazy loading of the classes. When you call it from memcached the class definition isn't loaded, and then you get the error.

The workaround I use is:

Admin #lazily load the class definition

# do whatever that has cached Admins
DanSingerman
A: 

Thanks for your reply! I have a before_filter in application.rb that preloads all models before every request and it's unfortunately still not working.

I'm using (ahem, attempting to use) the cache-money gem which transparently overrides simple queries.

Matt Darby
I might be quite wrong but I am wondering if the problem is the before_filter not working as you think it is. Have you tried sticking the classes inline just to see if it works?
DanSingerman
Aha! I'll post the answer as, well, an answer so that it's immediately visible.
Matt Darby
A: 

The problem was that restful_authentication's login_from_session method was being loaded before the aforementioned before_filter was run. Pre-loading the STI user classes at the top of this method worked.

Matt Darby
A: 

just a quick comment, i've read that using require_dependency 'admin' is preferred now, not sure why

require_dependency 'region'

Also, I've found that this is unnecessary if you add

config.cache_classes = true

to your production.rb (or development.rb for testing), this seems to catch all the models for caching

Brian Armstrong