I have three models all associated with eachother via has_many :through method.
ProgramCategory
ProgramSubcategory
Program
Inside my application, I need to use ProgramCategory.title, ProgramSubcategory.title and Program.title very often. Lets say, there’ll be a dynamic sidebar menu and it will look like this:
|- Shows (ProgramCategory)
|- Action (ProgramSubcategory)
|- Lost (Program)
|- Games of Thrones (Program)
|- Dexter (Program)
Since I’m aware of power of application_controller, application_helper and partials; I feel lost about combining all of these together to find the most appropriate way.
Where and how should I call my models? Where should I build my methods to have access it through all of my controllers? Should I simply create a partial and render it at application layout?
I need some expert enlightment, please…
Thanks.
This navigation bar is not a core part of the data you are showing, it will be more or less the same for all pages. Thus, it does not belong to the controller.
Make it a helper method, and cache its results in the view:
app/helpers/sidebar_helper.rbapp/controllers/your_controller.rb(or put the helper method in your application helper to have it available everywhere)
app/views/application/_sidebar.html.hamlor
app/views/application/_sidebar.html.erband include the partial where appropriate