views:

30

answers:

2

Hi -

I'm trying to provide a HTTP api to my app that queries a db that's read-only (for replication purposes). I find that my app crashes repeatedly when making a request b/c the call is trying to update the sessions table whenever I query the db. This doesn't happen when I return some text without hitting the database for info.

class APIController < AplicationController

  def view
    data = Product.find(params[:id]).to_json # will fail
    data = { :one => 1, :two => 2 }.to_json  # will succeed

    respond_to do |format|
      format.html { render :json => data }
    end
  end  

end

How do I restrict it from touching the sessions table on this request (it's currently issuing an UPDATE on the updated_at field for that session). thanks.

+1  A: 

Are you using the session in this request? eg for authentication. That might be a problem.

You can disable the session for controller and/or action:

class MyController < ApplicationController
  session :off, :only => :view

  ...

end

Works a lot like the regular controller filters.

Toby Hede
"session :off" was deprecated in Rails 2.3. I wish it wasn't, because this is a lot cleaner than policing plugins for code that accesses a session value. http://guides.rubyonrails.org/2_3_release_notes.html
jdl
Ah, I see. Damn.
Toby Hede
+1  A: 

What version of Rails is this? In Rails 2.3, sessions are lazy-loaded, so check in ApplicationController and your plugins for any code (probably in a before_filter) that is accessing any value in your session. You want to avoid that filter for this view action.

jdl
yup, I'm running 2.3.5. thanks
sa125
turned out to be one of the before_filter calls - thanks!
sa125
You are welcome.
jdl