views:

352

answers:

1

I'm working on a rails app that has a whole bunch of before filters in the users_controller which look up user's stateful roles provided by Acts as State Machine. They look something like this:

class UsersController < ApplicationController

before_filter :not_logged_in_required, :only => [:new, :create]
before_filter :find_user_or_current_user, :only => [:edit, :update]
before_filter :find_user, :only => [:suspend, :unsuspend, :destroy, :purge]
before_filter :admin_required, :only => [:suspend, :unsuspend, :destroy, :purge]
before_filter :check_administrator_role, :only [:index, :suspend, :destroy, :purge]

With all of this checking of states for each action controlled by the UsersController, performance is becoming an issue, with SQL queries to the Users Column taking upwards of 5ms on my machine.

I'm only assuming all of these filters play a part in the dragging performance, and I'm wondering how to best refactor these to improve the reading from the database.

+1  A: 

Abstract a lot of the privilege stuff into your Users model (flags, perhaps? A full Privilege model and linktable might be overkill, but maybe not). Store the currently-logged in user's ID (and maybe a couple of other often-used things) into a session. Leave one before_filter, called :auth, that does a User.find(session[:user_id]) and sets some well-known class variable, maybe @loggedinuser, to the User object. Then you do lookups on the @loggedinuser for privileges.

zenazn