0

I have this in my controller:

def frontpage
  @sections = Section.all
end

In production environment, the query seems to be cached between requests. Until I restart an app, @sections is always the same. Why and how do I fix this? I should also mention, I use Postgres in production, could it be the reason?

orion3
  • 9,797
  • 14
  • 67
  • 93

3 Answers3

1

I believe that in your production environment, you have enabled caching somewhere. While I'm not able to tell you why and where, I can give you a solution:

def frontpage
    uncached do
        @sections = find(:all)
    end
end

At least it should do the trick

e: The benefit of this, is that it only affects the code inside the block. Everything else should still get cached.

NeroS
  • 1,179
  • 1
  • 12
  • 28
0

Queries are not cached between requests, but actions could be. Do you have cache_action or other caching mechanism defined in your controller?

EDIT: to figure out if it's your action that's being cached, put this in your action:

def frontpage
  return render :text => Time.now.to_s
  ...
end

And see it it changes.

Oscar Del Ben
  • 4,485
  • 1
  • 27
  • 41
0

Have a look at this answer from a related question. If your configuration is set up to cache, then you should switch that off (in development at least.) Otherwise, if you want to invalidate the cache of a specific page, you can look into defining a method to do so in your controller:

private
def invalidate_cache
     Rails.cache.delete("/sections")
end

You would need to specify when that is called, but it would do the trick.

Community
  • 1
  • 1
Chris
  • 11,819
  • 19
  • 91
  • 145