# How to access multiple models in a single controller

**URL:** <https://www.ruby-forum.com/t/how-to-access-multiple-models-in-a-single-controller/184948>\
**Category:** Rails\
**Created:** [March 10, 2010, 8:40pm UTC](https://www.ruby-forum.com/t/how-to-access-multiple-models-in-a-single-controller/184948 "2010-03-10T20:40:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![richardsugg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/5f9b8f/32.png) [@richardsugg](https://www.ruby-forum.com/u/richardsugg)\
**Post date:** [March 10, 2010, 8:40pm UTC](https://www.ruby-forum.com/t/how-to-access-multiple-models-in-a-single-controller/184948/1 "2010-03-10T20:40:11Z")

</div>

I am creating a customer database. The navigation menu will have  
sections for supported customers, trial customers, unsupported  
customers, etc. The nav menu will appear on every page.

I need to get the list of customers (i.e., supported customers), but  
since I’m not using the company controller, I don’t have access to the  
methods that return the Company model data. How can I get the  
Application controller to access the Company model?

Thanks,  
Richard

---

<div class="post-metadata">

**Author:** ![richardsugg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/5f9b8f/32.png) [@richardsugg](https://www.ruby-forum.com/u/richardsugg)\
**Post date:** [March 10, 2010, 9:52pm UTC](https://www.ruby-forum.com/t/how-to-access-multiple-models-in-a-single-controller/184948/2 "2010-03-10T21:52:46Z")

</div>

You should be able to access your Company model from any controller.

---

<div class="post-metadata">

**Author:** ![richardsugg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/5f9b8f/32.png) [@richardsugg](https://www.ruby-forum.com/u/richardsugg)\
**Post date:** [March 15, 2010, 6:54pm UTC](https://www.ruby-forum.com/t/how-to-access-multiple-models-in-a-single-controller/184948/3 "2010-03-15T18:54:40Z")

</div>

So in my CompanyController, I have functions index, new, edit, create,  
show, etc… My understanding is that one of these gets called  
depending on the page. If I’m creating a new company, then the new/  
create is called, etc. How do I create a function that is called  
everytime, regardless of the context?

---

<div class="post-metadata">

**Author:** ![richardsugg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/5f9b8f/32.png) [@richardsugg](https://www.ruby-forum.com/u/richardsugg)\
**Post date:** [March 15, 2010, 6:58pm UTC](https://www.ruby-forum.com/t/how-to-access-multiple-models-in-a-single-controller/184948/4 "2010-03-15T18:58:46Z")

</div>

On Mar 15, 5:53 pm, richardsugg [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> So in my CompanyController, I have functions index, new, edit, create,  
> show, etc… My understanding is that one of these gets called  
> depending on the page. If I’m creating a new company, then the new/  
> create is called, etc. How do I create a function that is called  
> everytime, regardless of the context?

Sounds like you are looking for a before\_filter

Fred
