# Caching a model?

**URL:** <https://www.ruby-forum.com/t/caching-a-model/113232>\
**Category:** Rails\
**Created:** [August 29, 2007, 2:58am UTC](https://www.ruby-forum.com/t/caching-a-model/113232 "2007-08-29T02:58:09Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![deepa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/91b2a8/32.png) [@deepa](https://www.ruby-forum.com/u/deepa)\
**Post date:** [August 29, 2007, 2:58am UTC](https://www.ruby-forum.com/t/caching-a-model/113232/1 "2007-08-29T02:58:09Z")

</div>

Hi,

I am using memcahced\_client for caching. I also have cached\_model  
installed as a gem install. I have a user object that in turn has a  
hasmany relationship to a reviews model.

So, once user logs in, i have soemthing like

@reviews = @user\_profile.reviews

and reviews has been defined as follows in the user\_profile model

# Reviews

has\_many :reviews, :class\_name =\> “UserReviews”, :foreign\_key  
=\> :user\_id, :limit =\> 1000, :order =\> :review\_date

I then try to cache the @reviews using

Cache.put key,@reviews.

Next i try to retreive it using

Cache.get key

Here is my code for that.  
begin  
@reviews = Cache.get @user\_profile.user\_id.to\_s+’\_reviews’  
rescue  
@reviews = nil  
end  
if @reviews == nil  
@reviews = @user\_profile.reviews  
Cache.put @user\_profile.user\_id.to\_s+’\_reviews’, @reviews  
end

Neither the put nor the get seem to be working. I think it has  
something to do with the marshalling. Can anyone help?

Thanks in advance  
Deepa.
