# Caching a model that belongs to another model

**URL:** <https://www.ruby-forum.com/t/caching-a-model-that-belongs-to-another-model/92684>\
**Category:** Rails\
**Created:** [April 23, 2007, 1:19am UTC](https://www.ruby-forum.com/t/caching-a-model-that-belongs-to-another-model/92684 "2007-04-23T01:19:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ehudros](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3ec8ea/32.png) [@ehudros](https://www.ruby-forum.com/u/ehudros)\
**Post date:** [April 23, 2007, 1:19am UTC](https://www.ruby-forum.com/t/caching-a-model-that-belongs-to-another-model/92684/1 "2007-04-23T01:19:00Z")

</div>

Hi everyone…  
I have a user model that stores the currently logged in user in a  
session variable so a db query won’t be created for every access to it.  
However, I have another model (user\_properties) that belongs\_to the user  
model. Question is, how can I achieve the same caching effect when I  
access  
current\_user.user\_properties (which is a dynamic finder I think)?

It currently creates a query for the user\_properties based on the cached  
user - but I want that data to be cached as well.

Any thoughts?  
Thanks!

---

<div class="post-metadata">

**Author:** ![ehudros](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3ec8ea/32.png) [@ehudros](https://www.ruby-forum.com/u/ehudros)\
**Post date:** [April 23, 2007, 8:09am UTC](https://www.ruby-forum.com/t/caching-a-model-that-belongs-to-another-model/92684/2 "2007-04-23T08:09:36Z")

</div>

If you cache much user data, you run the security risk of a session  
hijack exploit (to name one). Additionally, these queries tend to be  
lightweight and databases optimize for recent queries … so if the  
cost of a query is 1, the cost of 100 queries all exactly alike is  
significantly less than 100.

If you really, really want to cache this stuff then do something like  
this to your user model:

class User

# This solution has some naivety about your problem. If your user

has\_many properties, then you’ll have to

# create an array to hold them instead of a nil scalar.

@@\_user\_properties = nil

def user\_properties  
@@\_user\_properties = self.send(user\_properties) if  
@@\_user\_properties.nil?  
@@\_user\_properties  
end  
end

My belief is that doing this will not reduce your server load  
meaningfully, and it opens a pretty big security hole.

HTH

---

<div class="post-metadata">

**Author:** ![ehudros](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3ec8ea/32.png) [@ehudros](https://www.ruby-forum.com/u/ehudros)\
**Post date:** [April 23, 2007, 10:28am UTC](https://www.ruby-forum.com/t/caching-a-model-that-belongs-to-another-model/92684/3 "2007-04-23T10:28:06Z")

</div>

Thanks!  
That’s what I was looking for…

---

<div class="post-metadata">

**Author:** ![ehudros](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3ec8ea/32.png) [@ehudros](https://www.ruby-forum.com/u/ehudros)\
**Post date:** [April 23, 2007, 8:33am UTC](https://www.ruby-forum.com/t/caching-a-model-that-belongs-to-another-model/92684/4 "2007-04-23T08:33:43Z")

</div>

On 4/22/07, Ehud R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> user - but I want that data to be cached as well.
> 
> Any thoughts?  
> Thanks!

It already is cached. tail -f log/development.log and then open up  
the console. Watch the log file as you run these queries  
u = User.find 1 # SELECT …  
u.user\_properties # SELECT …  
u.user\_properties # no query

Rails caches the association, so you don’t have to worry about it.

You can reduce the number of queries down to one by using the :include  
option to find.

u = User.find 1, :include =\> :user\_properties

Rails will do a join and pull all the info in one query.

Pat
