caching - Rails.cache.fetch exception: TypeError (<ModelName> can't be referred to) - Stack Overflow

Ask questions Research chat →

https://stackoverflow.com/questions/7664730/rails-cache-fetch-exception-typeerror-modelname-cant-be-referred-to · scraped

rails

Scraped Content

— 275 words · 2026-02-14 17:43:28 UTC ·

Excerpt

![](https://prod-files-secure.s3.us-west-2.amazonaws.com/871f1661-80b8-4d0c-ac3b-2adfc6ff4c66/e5db7028-4f14-49f0-9d9b-5d93ec4acc88/apple-touch-icon2.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=ASIAZI2LB466ZJWYZFPW%2F20260214%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Date=20260214T174328Z&X-Amz-Expires=3600&X-Amz-Security-Token=IQoJb3JpZ2luX2VjEEEaCXVzLXdlc3QtMiJHMEUCIBbuJpda4XpPT1zH43arw0%2B6f6ya%2F8S%2Fk2rNFkA%2BH0IpAiEAvFjiT2JCr3dMSErSUV4K%2FH3qyRAFja7%2FcqPqwEztlGAq%2FwMIChAAGgw2Mzc0MjMxODM4MDUiDPiBVH624aUbKH377SrcA1ik81cFNwrftJQKnTp1UvORmyOJrDMQnZ8zeMZtAUsgHz19R9LvvDoAVbohLNKD30CiWFOstVmK5orNYUgUY14G%2BTAwIlZS8WTNCY0%2BFsVS7seFvv%2B16A%2Fj6I4KPK8cjxj4aRd0PcoZou%2BSlsWSYaNUX%2FwW9UIyNUwQNSMbgj4Udbg6j7rDXJVBQ3By3ajFZyIgHnkioCJ0Q6MOO%2BFYRwbgq4Ug%2FkC59BsK0osE8bpn6DnDju7X8OgQnNq76zab645aMEoxQ2yMNZ3H3A1btA%2FBqfaUQ0m2RXiDjc9LaRG6gCyv4PkCWmRTT1Bfhc521KLUzbHdC1BXJVPiSVuSq7TlkVQ8yLxs66ocPZFHitR0l160NxYLJVCg0eUj%2FZ1GfZnFKVqh5Tk31Li2GKFYHdvtEoUvg
![](https://prod-files-secure.s3.us-west-2.amazonaws.com/871f1661-80b8-4d0c-ac3b-2adfc6ff4c66/e5db7028-4f14-49f0-9d9b-5d93ec4acc88/apple-touch-icon2.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=ASIAZI2LB466ZJWYZFPW%2F20260214%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Date=20260214T174328Z&X-Amz-Expires=3600&X-Amz-Security-Token=IQoJb3JpZ2luX2VjEEEaCXVzLXdlc3QtMiJHMEUCIBbuJpda4XpPT1zH43arw0%2B6f6ya%2F8S%2Fk2rNFkA%2BH0IpAiEAvFjiT2JCr3dMSErSUV4K%2FH3qyRAFja7%2FcqPqwEztlGAq%2FwMIChAAGgw2Mzc0MjMxODM4MDUiDPiBVH624aUbKH377SrcA1ik81cFNwrftJQKnTp1UvORmyOJrDMQnZ8zeMZtAUsgHz19R9LvvDoAVbohLNKD30CiWFOstVmK5orNYUgUY14G%2BTAwIlZS8WTNCY0%2BFsVS7seFvv%2B16A%2Fj6I4KPK8cjxj4aRd0PcoZou%2BSlsWSYaNUX%2FwW9UIyNUwQNSMbgj4Udbg6j7rDXJVBQ3By3ajFZyIgHnkioCJ0Q6MOO%2BFYRwbgq4Ug%2FkC59BsK0osE8bpn6DnDju7X8OgQnNq76zab645aMEoxQ2yMNZ3H3A1btA%2FBqfaUQ0m2RXiDjc9LaRG6gCyv4PkCWmRTT1Bfhc521KLUzbHdC1BXJVPiSVuSq7TlkVQ8yLxs66ocPZFHitR0l160NxYLJVCg0eUj%2FZ1GfZnFKVqh5Tk31Li2GKFYHdvtEoUvgCFUzLF9P%2BwUblz%2B5Vt3B5pScNKMxsuhn62WqADmYYxs%2FsfAfm6aH1QFqW%2BPRV2qPNqnw8LFX28T0IPBBfnY1K2ENgEiQ83%2BMlj60PtCrHDztuG6vBn2YDUWIySbV9Uxf9GZd%2FJxYcAK8YKJEcKtkjYzF6Bhs0vatAM857hfuzSVnH%2FzuxzPPgTNpBLWyR54VmOlzIYQp1SNlW0h%2BxSA6l0VqRRJS42bMNDRwswGOqUBVBkLyqIb%2F1oc7EY7zvxQ%2Fxk0%2FyE2WgFZgp0hymSO15heDfM6hUNGTrtdY4vS5KknBZm6GKxkxn8H6mC8E%2BfGBrtUFbi5AfWuHDLy%2FM7ZOrC1FtqlaSjT7Wn7GXyagiHYYPiFUpZXQHA6TO0DZD9dsmq%2BQnLVSEbk1npLaukT6F9IkoX1rE%2FhITLu0wVVAryIjU1cz33ud9faQ8BFjrCYLC%2BfBdRj&X-Amz-Signature=00c0d05e2c4c8bc0c1a340e345765088af9f078e3a1cc31ea2d7638670e3ebc3&X-Amz-SignedHeaders=host&x-amz-checksum-mode=ENABLED&x-id=GetObject) I'm implementing some caching by using the nifty Rails.cache.fetch. However, in one particular instance, sometimes I encounter an exception: ```plain text TypeError in EmloController#index Emlo can't be referred to app/controllers/emlo_controller.rb:320:in `get_employees' app/controllers/emlo_controller.rb:356:in `prepare_json_response' app/controllers/emlo_controller.rb:23:in `block (2 levels) in index' app/controllers/emlo_controller.rb:15:in `index' ``` It seems the fetch will always explode (with the above) on the first try, and then work fine as long as the fetch is within the expiration. I know I'm missing something, so a fresh pair of eyes would be nice. Here's the method which invokes the cache fetch: ```plain text def get_employees # This is for a AJAX refresh loop, so a 5-second cache actually helps quite a bit Rails.cache.fetch('emlo_all', :expires_in => 5.seconds, :race_condition_ttl => 1) do conditions = (params[:id]) ? {:user_id => params[:id]} : nil selections = [ 'employee_locations.id AS emlo_id', 'employee_locations.status_id', 'employee_locations.notes', 'employee_locations.until', 'employee_locations.updated_at', 'employee_locations.user_id', 'location_states.id AS state_id', 'location_states.title AS status_string', 'location_states.font_color', 'location_states.bg_color', 'users.displayname', 'users.email', 'users.mobile', 'users.department', 'users.extension', 'users.guid', 'users.dn' ].join(', ') Emlo.all( :select => selections, :joins => 'LEFT JOIN users ON employee_locations.user_id=users.id LEFT JOIN location_states ON employee_locations.status_id=location_states.id', :conditions => conditions, :order => 'users.displayname ASC' ) end end ``` This problem arises in development mode when config.action_controller.perform_caching = true AND config.cache_classes = false -- it seems ActiveRecord objects cannot be stored with Rails.cache. But if you need to enable config.action_controller.perform_caching in development mode for testing caching, then you must also enable config.cache_classes. This would be temporary, though, because then you'd have to restart the development server after changing classes or files in the asset pipeline. With caching disabled, I would use Rails.cache.write(some_name, some_value) if Rails.env.production? to prevent caching from blowing up in development. Rails.cache.read() doesn't seem to be affected.

Visibility

Visible to everyone

Reading Status

Related Bookmarks

My Note


Saved!

Annotations

Agent findings

info URL returned 403 (likely bot-blocked, not necessarily broken) health · Jul 20
Export as Markdown

Attachments

+ Annotate selection

Add Annotation