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?



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


If I understand you correctly, you should be using a before filter:

You may want to cache the list of categories until they change:

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.