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

  • Home
  • SEARCH
  • 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 8168011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:22:53+00:00 2026-06-06T20:22:53+00:00

In my application I have a complex database structure. In order to generate the

  • 0

In my application I have a complex database structure. In order to generate the JSON response for a call, I need to get data from many joined tables.

I created a SQL query that fetched all the data I need (using rails’ includes). however, now that I have the result in my view, I can’t figure what is the right way to actually render it.

A simplified example, not my actual code: Let’s say I have Project that contains SubProjects that contain Tasks and that the query returns some nested structure of them all, filtered according to some logic.

class Project < ActiveRecord::Base
  ...
  def with_sub_projects_visible_by(user)
    includes(...complex join with sub projects and tasks...)
    .where(...complex condition...)
  end
end

In the controller I have something like this:

  def show
    @project_with_full_details = Project.find(params[:id]).with_sub_projects_visible_by(current_user)
  end

Is there a way to write simple view code that will render a page that contains all the data from @project_with_full_details without calling the database again? From what I tried, simply calling @project_with_full_details.sub_projects doesn’t do the trick. It calls the database and gets me the full list of related sub_projects instead of the filtered one that should be contained in the query results.

(I’m using RABL for my view, since I’m rendering JSON, but it’s less important, ERB example will be totally fine)

  • 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-06T20:22:55+00:00Added an answer on June 6, 2026 at 8:22 pm

    ActiveRecord provides a preloader for associations which causes Rails to only load the objects from the database once (it also happens before the page is loaded).

    For your example, it would probably look something like this:

    @project = Project.find(params[:id])
    ActiveRecord::Associations::Preloader.new(
      @project, [ :subprojects, subprojects: :tasks]
    )
    

    That would at least give you the desired effect of only running the SQL once.

    If your models look something like this:

    class Project
      has_many :users
      has_many :sub_projects
      has_many :tasks, through: :sub_projects
      ...
    end
    

    Then perhaps you could define a method like subprojects_for(user). Then you should be able to do something along these lines:

    # controller
    @project = Project.find(params[:id])
    @subprojects = @project.subprojects_for(current_user)
    
    # haml
    .project
      = @project.title
      - @project.subprojects_for(current_user).each do |subproject|
        = Something for subprojects...
        - subproject.tasks.each do |task|
          = You get the idea
    

    The real magic should have happened in the preloader, though. I assume you’ve looked at the server logs to see that the SQL is being run more than once?

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

Sidebar

Related Questions

My application loads lots of data from a database into a complex data structure.
I have a multithreaded C++ application which holds a complex data structure in memory
I have an Oracle 9 database from which my Delphi 2006 application reads data
I have a complex input form within a PHP based web application. To structure
In your typical MVC application, you have many layers. Database layer Model layer (possibly
I have a complex database conversion console app that reads from an old database,
I have a LAMP application that needs to accept external data sources from our
I have complex GUI application written in Python and wxPython. I want it to
I have a moderately complex application using POJOs, and now come to migrated it
We have a rather large and complex application written in Java which is running

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.