Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8854923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:59:52+00:00 2026-06-14T13:59:52+00:00

This might be a noob question. But I couldn’t figure it out by myself.

  • 0

This might be a noob question. But I couldn’t figure it out by myself.

In my application’ I’m using Rails 3.2.8 with Mongoid and MongoDB.

I have a instance variable like this:

ProgramsController < ApplicationController

@program = Program.find(params[:id])

In my view file, I need to use it many many times. For example;

@program.title
@program.content
@program.schedules (associated with Schedule model)
@program.articles (associated with Article model)

etc.

The issue is, I need to use that instance variable at DIFFERENT parts of the view. So it’s not something like doing a @program.each do |t| ....

BUT when ever I use @program at a different part of the view, that means a NEW query each time…

At the moment I have 31 queries for my view. Isn’t it too much?

So what is the best practice? How should I use instance variables effectively?

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T13:59:54+00:00Added an answer on June 14, 2026 at 1:59 pm

    Once the item has been stored in @program, it is indeed saved there and subsequent requests will not generate queries. If you only had references to @program.title and @program.content, you would probably be fine. Your issue is from the associations, like @program.schedules and @program.articles. The problem is that the schedules and articles are not being loaded from your @program = Program.find(params[:id]). This is referred to as the n+1 problem. The official docs state, under Eager Loading, that Eager loaded is supported on all relations with the exception of polymorphic belongs_to associations. and In order for eager loading to work, the Identity Map must be enabled.. If these two conditions are true, you should see a reduction in your queries. If you cannot accomodate those requirements, a good solution might be to do somethign like this in the controller, to grab them all ahead of time, in one query: @schedules = Schedule.find_by(program_id: @program.id)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This might be an easy question, but I cannot figure out why the compiler
This might be a noob question but i've been fiddling with it for hours
This might be a noob question, but can't find an answer anywhere. I have
This might sound like a noob question but this is the first time I'm
Hi there this might me a very noob question but I'm stuck, and I'm
This might be a noob question, but I'm wondering if there's a way to
This might sound like a noob question, but here it goes anyway. Can BlackBerry
This might be a noob question. but I'm really torned between adding documents to
I'm pretty new to php so this might be a noob question. I'm using
This might be a very noob question, but I can't seem to find the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.