views:

70

answers:

1

Rails is caching the index method of one of my controllers. It's a very simple application and only has like 2 controllers and a handful of actions each. The weird thing is I don't have any caching in my application at all, at least not explicitly. The pages get uncached if I restart passenger. Does rails do some kind of automatic page caching?

  • There are no files in the public directory
  • The page is returning a 200 header
  • I have no caching blocks in my views (I use haml, if that matters)
  • I have no action, controller, or page caching defined
  • The request is hitting rails, verified by the production log

I have the following in my production.rb:

config.cache_classes = true
config.action_controller.consider_all_requests_local = false config.action_controller.perform_caching = true config.action_view.cache_template_loading = true

The problem is it isn't showing any Releases in the table in my view, yet it will if I restart passenger. The reason I know this is failing is because I can fire up the Rails console and do Release.ready, which is the named scope being displayed in the view, and I actually get some results back.


Here is the relevant code:

models/release.rb

class Release < ActiveRecord::Base
  default_scope :order => 'created_at DESC'
  named_scope :ready, :conditions => ['show_on <= ? AND is_deleted = ?', Time.now, false]
end

controllers/releases_controller.rb

class ReleasesController < ApplicationController
  def index
    @releases = Release.ready
  end
end

views/releases/index.html.haml

%table{:border => 1}
  %tr
    %th Date
    %th Title
    %th Artist
    %th Song
  - @releases.each do |release|
    - @release = release

    %tr[release]
      %td
        = link_to_remote "delete", :url => release, :method => :delete , :confirm => 'Remove?'
        = release.created_at.to_date.to_s(:long_ordinal)
      %td= link_to release.title, release.url
      %td= in_place_editor_field :release, :artist
      %td= in_place_editor_field :release, :song
+1  A: 

If there are no files in the public folder, I don't think your views are getting cached.

For a little while now, Rails has cached views source (just like models) so if you make changes to those views, in production, you have to restart passenger for those changes to take effect when you look at it through a browser.

Let me know if anything still seems unclear from my answer.

EDIT: Here's a link to the changelog from when I believe it happened: Ruby on Rails 2.3 Release Notes

theIV
Thanks, but I'm not trying to modify the views while the application is running. I'll post some code.
ryeguy
I'm not sure, but this could be more of an issue with using `Time.now` in a `named_scope`. As a rule, when using any sort of "Time" in `named_scope`s, you will want to use it inside of a lambda so that it get's evaluated when it's called, as opposed to once when your application starts and then never again. Here's a link to the railscasts episode because I can't find any of the relevant posts on SO: http://railscasts.com/episodes/108-named-scope
theIV
@theIV: Thanks so much! That was it.
ryeguy
@ryeguy: very glad that I could be of help, and that you figured it out. Cheers.
theIV