Forum: Ruby on Rails Caching in Rails

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.
6d465bb8df6bda89904c7aafea0dda6c?d=identicon&s=25 Don French (Guest)
on 2009-03-17 07:57
(Received via mailing list)
I have a page that requires a lot of DB access to create a section of
it, but that section does not change very often. I know how to cache
the fragment in the view, but the db is still accessed in the
controller. I watched the railscasts on fragment caching and he does
it by moving the logic to the model.  Is that the only way to do it?
The part of the page that changes on every display is the advertising
so I can not cache the entire page.
36ed63ef68c172f148c491418395d0b5?d=identicon&s=25 rubyguy@ymail.com (Guest)
on 2009-03-17 08:07
(Received via mailing list)
On 17 Mar., 07:56, Don French <dhf0...@gmail.com> wrote:
> I have a page that requires a lot of DB access to create a section of
> it, but that section does not change very often. I know how to cache
> the fragment in the view, but the db is still accessed in the
> controller. I watched the railscasts on fragment caching and he does
> it by moving the logic to the model.  Is that the only way to do it?
> The part of the page that changes on every display is the advertising
> so I can not cache the entire page.

You can use the #fragment_exists? method in your controller action.
This allows you to avoid querying the database if it's not neseccary.

def action
  unless fragment_exists? "fragment"
    # Execute SQL here...
  end
end

--
Cheers,
David Knorr
http://twitter.com/rubyguy
This topic is locked and can not be replied to.