Forum: Ruby on Rails Caching a model that belongs to another model

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Ehud R. (Guest)
on 2007-04-23 03:19
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
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?
Steve R. (Guest)
on 2007-04-23 10:09
(Received via mailing list)
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

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

Pat M. (Guest)
on 2007-04-23 10:33
(Received via mailing list)
On 4/22/07, Ehud R. <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.

Ehud R. (Guest)
on 2007-04-23 12:28
That's what I was looking for...
This topic is locked and can not be replied to.