Instance variable life span in controller

I understand that rails drops all instance variables created in normal
controller action after rendering view templates.

But I have the following problem that a normal controller action has
several before_filter and after_filter, which all using same set of
variables fetching from database.
#######################################
Class AppSetting< ActionRecrod::Base
end

Class PeopleController < ApplicationController
before_filter :get_title
before_filter :get_subtitle
after_filter :update_some_properties_on_condition
after_filter :update_some_other_properties_on_condition

def index
   @name= Person.first.name
end

private

def get_title
   @title = AppSetting.first.title
end

def get_subtitle
   @subtitle = AppSetting.first.subtitle
end

def update_some_properties_on_condition
   do_something if condition1 == AppSetting.first.condition1
end

def update_some_other_properties_on_condition
   do_something if condition2 == AppSetting.first.condition2
end

end
#######################################################

As you can see, all the five methods all using “AppSetting.first”
object in the process.

Is there anyway that I can set some variables that all actions can use
in this process? just like the function of class variables in ordinary
ruby code.

You can do like

def get_title
@title = @first_setting.title
end

def first_app_setting
@first_setting ||= AppSetting.first
end

Sijo

I do not think I explained my problem clearly.

After you read the above post, you maybe think that I can just create
another before_filter which is at the very beginning of this process,
and just set an instance variable like @app_setting = App.first, and
all the following method can use this variable.

But because all the before_filter and after_filter are wrapped to
almost all actions in my app, so what I really want is that whether
there is something that can persists its value between different
request?

Sorry
def get_title
@title = first_app_setting.title
end

Sijo

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs