Where to place a query that should be included in all or most views
- by Andrew
In my application I have a sidebar which I want to include a list of pages. Cheating on the MVC setup, I can pretty easily display this as follows (in HAML):
# layouts/_sidebar.html.haml
%h4 Pages
%ul.pages
- for page in Page.all
%li= link_to page.title, page
Now, this works just fine, but clearly it's against the convention. The problem is, this shared layout partial is present in most (but not all) views, and therefore to serve the pages from the controller layer would mean needing to inject an instance variable into almost every controller action in the application. That isn't very clean or DRY.
So, how would you handle this kind of situation? Is there a clean, DRY place to put this kind of a simple query that respects Rails MVC convention better?