[LRUG] Are before_filters abused in Rails?

mudge mudge at mudge.name
Thu Mar 22 05:18:58 PDT 2012


Good afternoon all,

I posted this on Twitter a while back but seeing as the list has been
a flurry of activity recently, I thought I'd post it here to gather
more debate.

Do you use before_filters in your controllers to DRY up the
instantiation of variables like so?

class MyController < ApplicationController
  before_filter :load_model, :only => [:show, :edit, :update, :destroy]

  def show
  end

  def edit
  end

  private

  def load_model
    @model = Model.find(params[:id])
  end
end

It's a pattern that I've seen quite a bit and something we had taken
to using until a member of the team began challenging it during code
review. The gist of the issue is that he believed it to be an abuse of
filters which should really only deal with things like authentication;
it was his assertion that the actual loading of a model should be made
much more explicit.

The full discussion can be found at https://gist.github.com/2007669
and I've added a few alternatives (including one inspired by the
latest Ruby Rogues discussion about Objects in Rails
http://rubyrogues.com/046-rr-objects-in-rails-part-2/) but I'd be keen
to see what people here think and whether it is an issue or not.



More information about the Chat mailing list