Running the same model search in multiple controllers

So I have a layout (application.rhtml) that contains a list of
categories that are stored within the database. To get retrieve the
list I overwrite the initialize method within the application
controller (application.rb) as such:

def initialize
@categories = Category.find(:all, :order => :position)
end

Is this the best way I should be doing this? I don’t want to have
this code in my layout breaking MVC idioms, but is there a more
preferred way in best practice then by doing it this way?

Thanks,

Blaine

Personally I’d be writing a before filter to call a method in my
application.rb

Cam

On Aug 16, 5:31 am, blaine [email protected] wrote:

this code in my layout breaking MVC idioms, but is there a more
preferred way in best practice then by doing it this way?

If I understand you correctly, you should be using a before filter:
http://api.rubyonrails.com/classes/ActionController/Filters/ClassMethods.html

You may want to cache the list of categories until they change:
http://ap.rubyonrails.com/classes/ActionController/Caching/Fragments.html

Excellent - I thought about doing it with a before_filter, but again
wasn’t sure. I’ve adjusted the code to use a before_filter instead
and it’s all working great, thanks a lot.

Blaine