Caching in Rails

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