Paul
Paul

Reputation: 113

How to prevent rails from caching Model.all query

I don't understand why in development mode, the query is getting cached even after inserting a new record. What I mean is that, after creating a new record, my Model.all doesn't pull that record from the db. I checked with IRB and the record is persisted. Once I restart the server, the new record shows up.

Here is the detail...

First time after starting the server, when i hit the pagecontroller#index, the logs are

Processing by PagesController#index as HTML
  Page Load (14.9ms)  SELECT "pages".* FROM "pages" 
  Rendered pages/index.html.erb within layouts/application (4.1ms)
Completed 200 OK in 237ms (Views: 167.5ms | ActiveRecord: 16.9ms)

After creating a page, user gets redirected to index action and the logs are

Started GET "/pages" for 127.0.0.1 at 2012-06-13 09:40:27 -0400
 Processing by PagesController#index as HTML
 Rendered pages/index.html.erb within layouts/application (1.6ms)
Completed 200 OK in 5ms (Views: 5.2ms | ActiveRecord: 0.0ms)

My action

def index
  @pages = Page.all
end

def create
  @page = Page.new(params[:page])
  if @page.save
    flash[:notice] = "Page saved Successfully"
    redirect_to :action => "index"
  else
    flash[:alert] = "Errors on saving the page"
    render :action => "new"
  end
end

I am pretty new to rails and couldn't find anything in my research of the problem. Can someone help me.

GEMS

Upvotes: 0

Views: 215

Answers (1)

Dan
Dan

Reputation: 326

Rails 3.2.4 had an issue with caching findall. I suggest you upgrade to a newer version of Rails, as per this question: Rails 3.2.4 SQL query is caching results on find(:all)

Upvotes: 1

Related Questions