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! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 On Apr 22, 2007, at 4:19 PM, Ehud Rosenberg wrote:> > 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! > > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 4/22/07, Ehud Rosenberg <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > 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!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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks! That''s what I was looking for... -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---