[Rails] Re: Getting data from the database only once

Dan Kohn
Tue, 25 Jul 2006 06:44:43 -0000

On Jul 24, 2006, at 11:04 PM, Daniel N wrote:

> Would this cause issues if say, different options need to be
> selected based on object values or different defaults for different
> views?
>
> Wouldn''t a fragment cached version be permenantly selected on one
> option by default?

Now I''m confused.  I was so impressed by a trick someone mentioned
this week for fragment caching separately by user that I saved the
message.  But the message was from you!

> Yes.  When you cache you should use fragment cache. Include in the
> cache call a user id.  the cache method accepts a hash like url for
> but it can be anything.  You''re not restricted to real things
> (controllers/actions etc).  If you don''t include somthing to
> seperate it by user, each user may get the same cached fragment.
>
> an eg might be
>
> <% cache( :controller => ''bill'', :action =>
''show_last_10'', :user
> => current_user.id ) do -%>
> <%= all your cached frag -%>
> <% end %>
>
> This will scope the fragment to those parameters, and you can re-
> use it elsewhere if you want.  Just include the same url in the cache.
>
> Be sure to expire the fragment if the data changes.  expire_fragment
> ( same_url_hash_used_in_cache_call )

        - dan
--
Dan Kohn <mailto:dan@dankohn.com>
<http://www.dankohn.com/>  <tel:+1-415-233-1000>

-------------- next part --------------
An HTML attachment was scrubbed...