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 9029709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:18:40+00:00 2026-06-16T07:18:40+00:00

I have a very complex query at a beginner level which I am not

  • 0

I have a very complex query at a beginner level which I am not able to solve out. It takes my page load to around 6 7 seconds on local machine.

The Query is something like for user

self.groups.collect(&:reports).flatten.paginate

Now I run a loop which is something like :

reports.each do |report|
  report.name

  report.user.name
  report.user.image.attachment.url(:thumb)   #User has_one :image , Image belongs_to :user

  report.questions.each do |question|
    question.name 

    question.answers.each do |answer|
      answer.name
    end
  end

  report.comments.each do |comment|
    comment.name 
    comment.user.name
    comment.user.image.attachment.url(:thumb)   #User has_one :image , Image belongs_to :user
  end

end

It has a lot of nested looping which requires a lot of use of includes but me not able to figure out which way should i proceed. My earlier question was N+1 in rails relations with active records?

——————EDIT——————

I am able to do

self.groups.includes(:reports => [:user, :questions]) 

or

self.groups.includes(:reports => [:user => [:image]]) 

but not

 self.groups.includes(:reports => [:user => [:image], :questions]) 

whats wrong with this ??

——————EDIT——————

self.groups.includes(:reports => [:questions, :user => [:image]]).collect(&:reports).flatten.paginate(:page => page, :per_page => 6)    

This is what seems to work for me with no extra/less eager loading according to bullet gem( https://github.com/flyerhzm/bullet ) but still it takes some 10 seconds to load up the queries and its like a huge huge load. Any further improvements suggested ??

——————EDIT——————

 self.groups.includes(:reports => [:questions, .........]).collect(&:reports).paginate

changed to

self.group_reports.includes(:questions, ....... ).paginate

helped a lot

In User Model defined something like

has_many :reports
has_many :group_reports, :through => :groups, :source => :reports
  • 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-16T07:18:41+00:00Added an answer on June 16, 2026 at 7:18 am

    Try to use hash:
    self.groups.includes(:reports => [:questions, {:user => :image}])

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

Sidebar

Related Questions

I have a very complex Linq to SQL query that returns a result set
I have a very complex MySQL query that includes use of the same subquery
The problem: we have a very complex search query. If its result yields too
I have a very large and complex MySQL query that gets run from a
I have a very complex controller with a very complex set of views and
I have a very complex web app project I want to re-structure. Naturally, it
We have a very complex Oracle package here (over 4,100 lines of code) that's
I have a very complex Microsoft Access report. This report is run for multiple
I have a rather complex data source for my UITableView. I am very carefully
I have very very big html page/data. I need to fetch data under h1

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.