# How to access a model from application.rb

**URL:** <https://www.ruby-forum.com/t/how-to-access-a-model-from-application-rb/97738>\
**Category:** Rails\
**Created:** [July 10, 2006, 7:28am UTC](https://www.ruby-forum.com/t/how-to-access-a-model-from-application-rb/97738 "2006-07-10T07:28:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jgeggatt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jgeggatt](https://www.ruby-forum.com/u/jgeggatt)\
**Post date:** [July 10, 2006, 7:28am UTC](https://www.ruby-forum.com/t/how-to-access-a-model-from-application-rb/97738/1 "2006-07-10T07:28:34Z")

</div>

Hi All

How do I access a model from within the application.rb?

What im tying to do is pull some data from a model (news in this case)  
that must be displayed on each page within the entire application. I  
know I could drop this into each controller, have each view pull it from  
the controller, but thats repeating myself…

So whats the best way to do this (as I dont think models are visible  
from within application.rb)?

Many thanks

Jonathan

---

<div class="post-metadata">

**Author:** ![jgeggatt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jgeggatt](https://www.ruby-forum.com/u/jgeggatt)\
**Post date:** [July 10, 2006, 7:43am UTC](https://www.ruby-forum.com/t/how-to-access-a-model-from-application-rb/97738/2 "2006-07-10T07:43:44Z")

</div>

On Mon, Jul 10, 2006, Jonathan G. wrote:

> from within application.rb)?  
> Sure they are. Consider application.rb to be just another controller  
> that happens to get included in all of your other controllers.

Just access it like normal and you should be fine.

Ben

---

<div class="post-metadata">

**Author:** ![jgeggatt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jgeggatt](https://www.ruby-forum.com/u/jgeggatt)\
**Post date:** [July 10, 2006, 11:24am UTC](https://www.ruby-forum.com/t/how-to-access-a-model-from-application-rb/97738/3 "2006-07-10T11:24:28Z")

</div>

Hi All,

Just to report back, I worked out the problem I had. Just needed to  
read more and get pushed in the right direction. I ended up doing it  
with a partial that pulls uses an object pulled from the model.

Thanks to all for the push in the right direction

Regards

Jonathan

---

<div class="post-metadata">

**Author:** ![jgeggatt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jgeggatt](https://www.ruby-forum.com/u/jgeggatt)\
**Post date:** [July 10, 2006, 7:52am UTC](https://www.ruby-forum.com/t/how-to-access-a-model-from-application-rb/97738/4 "2006-07-10T07:52:55Z")

</div>

On 7/10/06, Jonathan G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> So whats the best way to do this (as I dont think models are visible  
> from within application.rb)?

application.rb is a controller that all other controllers inherit from.  
Models are just as visible in the application controller as any other.

There are a couple of ways to do this. Helper methods, before\_filters  
and a  
couple of more obscure ones.

Probably using both would be benificial. You can define the method in  
the  
controller and then tell it to act as a helper method as well. Then you  
get  
it in the controller and the views.

First, use a before\_filter in the application controller

class ApplicationController \< ActionController::Base

before\_filter :set\_some\_variable  
helper\_methods :some\_variable

def some\_variable  
@variable  
end

protected  
def set\_some\_variable  
@variable = MyModel.find(:first)  
end  
end

This will provide a method some\_variable to both views and controllers.  
The  
@variable will be set every time the controller is fired up.

Hope this helps

Cheers

Many thanks
