# Global objects?

**URL:** <https://www.ruby-forum.com/t/global-objects/97595>\
**Category:** Rails\
**Created:** [January 26, 2006, 2:09pm UTC](https://www.ruby-forum.com/t/global-objects/97595 "2006-01-26T14:09:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![justinj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b38774/32.png) [@justinj](https://www.ruby-forum.com/u/justinj)\
**Post date:** [January 26, 2006, 2:09pm UTC](https://www.ruby-forum.com/t/global-objects/97595/1 "2006-01-26T14:09:05Z")

</div>

Forgive the term global if that’s not applicable in rails (i come from a  
php background)

I have a browse controller with a couple of different methods

def method1  
@properties = Property.find\_all  
#other stuff for that method  
end

def method2  
@properties = Property.find\_all  
#other stuff for that method  
end

def method3  
@properties = Property.find\_all  
#other stuff for that method  
end

I know RoR is all about not repeating yourself if you don’t have to. Is  
there a way to say @properties = Property.find\_all 1 time and have it be  
available to all methods in that particular controller?

---

<div class="post-metadata">

**Author:** ![justinj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b38774/32.png) [@justinj](https://www.ruby-forum.com/u/justinj)\
**Post date:** [January 26, 2006, 2:25pm UTC](https://www.ruby-forum.com/t/global-objects/97595/2 "2006-01-26T14:25:51Z")

</div>

On second thought there are some points on which I can elaborate…you  
can pass in a block as I showed in my first response, and that also  
allows you to pass in a method. So you can do something like

class FooControlller \< ApplicationController  
before\_filter :my\_filter

def my\_filter  
@properties = Property.find\_all  
# Do whatever other stuff you want to do for each action  
end  
end

You can also have more control over which actions get filtered, using  
:only or :except

class FooControlller \< ApplicationController  
before\_filter :my\_filter, :except =\> :baz

def foo() end # This gets filtered  
def bar() end # This gets filtered  
def baz() end # NOT filtered

def my\_filter  
@properties = Property.find\_all  
# Do whatever other stuff you want to do for each action  
end  
end

class FooControlller \< ApplicationController  
before\_filter :my\_filter, :only =\> :baz

def foo() end # NOT filtered  
def bar() end # NOT filtered  
def baz() end # This gets filtered

def my\_filter  
@properties = Property.find\_all  
# Do whatever other stuff you want to do for each action  
end  
end

Of course this is ruby, so it all makes sense anyway. Just a couple  
neat little features that may come in handy.

Pat

---

<div class="post-metadata">

**Author:** ![justinj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b38774/32.png) [@justinj](https://www.ruby-forum.com/u/justinj)\
**Post date:** [January 26, 2006, 2:19pm UTC](https://www.ruby-forum.com/t/global-objects/97595/3 "2006-01-26T14:19:48Z")

</div>

Hey Justin,

You can use before\_filter to do what you want.

class FooControlller \< ApplicationController  
before\_filter do  
@properties = Property.find\_all  
end

def foo  
…  
end  
end

---

<div class="post-metadata">

**Author:** ![justinj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b38774/32.png) [@justinj](https://www.ruby-forum.com/u/justinj)\
**Post date:** [January 26, 2006, 2:44pm UTC](https://www.ruby-forum.com/t/global-objects/97595/4 "2006-01-26T14:44:08Z")

</div>

pat,

that’s awesome (and definatley understandable) I’ll give that a try  
later today.
