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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
6fe135329ce89e00b74839a41c8e3872?d=identicon&s=25 Ehud Rosenberg (ehudros)
on 2007-04-23 01: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
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!
Ef0db53920b243d6758c2f6b1306df0d?d=identicon&s=25 Steve Ross (cwd)
on 2007-04-23 08: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
@@_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
42172acdf3c6046f84d644cb0b94642c?d=identicon&s=25 Pat Maddox (pergesu)
on 2007-04-23 08:33
(Received via mailing list)
On 4/22/07, Ehud Rosenberg <rails-mailing-list@andreas-s.net> 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
6fe135329ce89e00b74839a41c8e3872?d=identicon&s=25 Ehud Rosenberg (ehudros)
on 2007-04-23 10:28
Thanks!
That's what I was looking for...
This topic is locked and can not be replied to.