before filter in applicatino_controller.

If I do something like this:

class ApplicationController < ActionController::Base
  protect_from_forgery
  before_filter RubyCAS::Filter
  before_filter :fetch_operator
  include SessionsHelper

  private

  def fetch_operator
    @current_operator ||= session[:cas_user] &&
Operator.find_by_uid(session[:cas_user])
    rescue ActiveRecord::RecordNotFound
      redirect_to("/404.html")
  end

end

The fetch_operator action is always executed, before every action in
every controller?
I'd want to fetch the operator only at start and not before every
action but I need to have it available on every view and controller.

Hi,

The fetch_operator action is always executed, before every action in every controller?

Yes, all kind of filters implemented inside ApplicationController
class are always executed in every controller (whose parent is
ApplicationController) unless you change the behavior explicitly.

class ApplicationController < ActionController::Base
  ...
  before_filter :fetch_operator
...
end

class anotherController < ApplicationController
  before_filter :fetch_operator, :only => [:login]
end

For further information, see: http://rails.rubyonrails.org/classes/ActionController/Filters/ClassMethods.html

Exequiel.

Are you then using the @current_operator variable in places in your
controller? (like "if @current_operator.name == 'fred'..."?)

If so, instead use a helper method to return the operator (and
populate it if necessary), rather than the filter method:

class ApplicationController < ActionController::Base
  protect_from_forgery
  before_filter RubyCAS::Filter
  include SessionsHelper
  helper_method :current_operator

  def current_operator
    @current_operator ||= fetch_operator
  end

  private
  def fetch_operator
    session[:cas_user] &&
Operator.find_by_uid(session[:cas_user])
    rescue ActiveRecord::RecordNotFound
      redirect_to("/404.html")
  end

end

But if I want fetch the operator only once at application startup?

But if I want fetch the operator only once at application startup?

I guess you can set up a session variable to make sure to call only
once.

if !session[:operator_called]
  ...
  session[:operator_called] = true
end

In a method of application controller?

It’s just an example, you can add that piece of code in any method, for example:

class ApplicationController < ActionController::Base

before_filter :call_operator_once

def call_operator_once

if !session[:operator_called]

fetch_operator()

!session[:operator_called] = true

end

end

end

Yes but it executes the call_operator before each action of each
controller in my application.

Typo error, it should be:

def call_operator_once
    if !session[:operator_called]
      fetch_operator()
      session[:operator_called] = true
    end
end

Right, it will called before each action of each controller in your
application, but only one time the fetch_operator method will be
executed. I thought you want to make sure that fetch_operator method
is executed at least one time independent of controller. Now, if you
want to use a filter method and avoid to call it in other controllers
you can use "skip_before_filter :my_method" in each controller. And if
you definitely don't use this method in other controllers, I guess you
don't need define a filter method, just call it as usually in each
action that you need it, because any filter that you defined inside
the ActionController class will be inherited by other controllers.