i i have different class and controller.
and i need that one instance of model will be available in controller/
now i’m doing something like this:
def method1
inst = @MyClass.new(params)
inst.action
....
def method2
inst = @MyClass.new(params)
inst.action
....
but i want something like this
def method1
@inst.action
....
def method2
@inst.action
or self.inst i’t doesn’t matter
how i can do it?
def self.inst
MyClass.new(params)
end
doesn’t work…
You can use a before_filter callback.
Heres how you use it
class YourController < ApplicationController before_filter :find_resource def action1 @some_instance.magic end def action2 @some_instance.magic end private def find_resource @some_instance = YourModel.find end endYou can also specify the actions that callback is run on with :only or :except options
HTH