I want to make a rollOver menu for my project’s main page. This will be shown in all pages of the site. Main menu items are SHOWS and PROGRAMS. When someone rolls over those buttons, all programmes will be listed inside a popping up div. (Program is name of my model and it’s a general name I use for both shows, movies, tv programs etc.)
Now…
Here are my associations:
Program.rb
class Program
include Mongoid::Document
include Mongoid::Timestamps
field :title
field :content
belongs_to :program_category
end
program_category.rb
class ProgramCategory
include Mongoid::Document
include Mongoid::Timestamps
field :title
field :content
has_many :programs
end
I need to get program.titles to show inside my menu. Like this:
SHOWS PROGRAMS
\- Lost \- Ellen
\- Dexter \- Marta Stuart
\- Alf \- Jey Leno Show
Well… I’m doing this like this now:
home_controller.rb
class HomeController < ApplicationController
def index
@menu_programs = Program.all.where(program_category_id: "50a7c373ce3a6bcc0a000006").limit(10) #Yes, i think it's lame too...
@menu_shows = Program.all.where(program_category_id: "50a67f36ce3a6b840d000007").limit(10) # And yes I'm using MonoDB and Mongoid
end
Yeah… They are program_category_ids and very hard coded!!
I feel like…
This is not the best practice!
QUESTION is:
What is best practice to show menu items from associated models? I heard somethign called ‘Eager Loading’ can someone explain it please? Because of n + 1 problem I have 36 queries…
Any suggestion is welcomed.
Thank you.
The answer of
What is best practice to show menu items from associated models?question is:Using Fragment Caching, I think. It’s even more essential than eager loading IMHO.