# Creating objects in application.rb

**URL:** <https://www.ruby-forum.com/t/creating-objects-in-application-rb/114388>\
**Category:** Rails\
**Created:** [September 12, 2007, 4:33am UTC](https://www.ruby-forum.com/t/creating-objects-in-application-rb/114388 "2007-09-12T04:33:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vanik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ac8455/32.png) [@vanik](https://www.ruby-forum.com/u/vanik)\
**Post date:** [September 12, 2007, 4:33am UTC](https://www.ruby-forum.com/t/creating-objects-in-application-rb/114388/1 "2007-09-12T04:33:59Z")

</div>

I’m trying to generate a simple navigation pane to include in my  
application.rhtml. The nav pane consists of dynamic link\_to tags that  
are generated from data in a model.

I’m now used to doing trivial MVC operations in a template:

```
def index
    @departments = Department.find(:all)
end

<!-- view (cheesy example) -->
<% @departments.each { |d|
<%= d.name +"<br>" %>
<% } %>

```

But what if I want to generate the @departments in application.rb? I  
have no action to place it in (like index), and it does not seem to work  
at the class level:

class ApplicationController \< ActionController::Base  
@departments = Department.find(:all)  
end

Should this work and be available in application.rhtml as  
@departments? If so then thats good news and my syntax is just wrong.

if not, is there an alternative way to populate an object in  
application.rb and use it in application.rhtml?

Thx  
Gary “still a newb” H.

---

<div class="post-metadata">

**Author:** ![vanik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ac8455/32.png) [@vanik](https://www.ruby-forum.com/u/vanik)\
**Post date:** [September 12, 2007, 4:52am UTC](https://www.ruby-forum.com/t/creating-objects-in-application-rb/114388/2 "2007-09-12T04:52:00Z")

</div>

> end
> 
> Should this work and be available in application.rhtml as  
> @departments? If so then thats good news and my syntax is just wrong.
> 
> if not, is there an alternative way to populate an object in  
> application.rb and use it in application.rhtml?

Well, if you want to generate that every time you could do this:

class ApplicationController \< ActionController::Base  
before\_filter :load\_departments

def load\_departments  
@departments = Department.find(:all)  
end  
end

Or if you only want to load it once per application startup, do  
something  
similar in environment.rb and load it up as a constant or some such…

---

<div class="post-metadata">

**Author:** ![vanik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ac8455/32.png) [@vanik](https://www.ruby-forum.com/u/vanik)\
**Post date:** [September 12, 2007, 5:33pm UTC](https://www.ruby-forum.com/t/creating-objects-in-application-rb/114388/3 "2007-09-12T17:33:10Z")

</div>

On 9/11/07, Gary H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> @departments = Department.find(:all)
> 
> ```
> 
> end
> 
> Should this work and be available in application.rhtml as  
> @departments? If so then thats good news and my syntax is just wrong.

This would only run when the controller class is first loaded, which  
is probably not what you want. It’s also an instance variable of the  
_class_ and not of the _request_, so you can’t access it from the view  
in the same way.

> if not, is there an alternative way to populate an object in  
> application.rb and use it in application.rhtml?

The MVC police might arrest me, but I see no problem with just putting  
the find directly in the view in this case. If the find became  
elaborate, I would refactor the complexity back into a model class  
method (much easier to test, and keeps the view code as simple as  
possible).

I don’t like the before\_filter approach. It would unecessarily  
retrieve the departments even if you weren’t rendering the view (e.g.  
an xhr response or a post-redirect), it pollutes the request  
namespace, and it prevents optimization through fragment caching.
