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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:33:01+00:00 2026-06-01T11:33:01+00:00

I have the following model structure: class Group < ActiveRecord::Base has_many :group_products, :dependent =>

  • 0

I have the following model structure:

class Group < ActiveRecord::Base
  has_many    :group_products, :dependent => :destroy
  has_many    :products, :through => :group_products
end

class Product < ActiveRecord::Base
  has_many  :group_products, :dependent => :destroy
  has_many  :groups, :through => :group_products
end

class GroupProduct < ActiveRecord::Base
  belongs_to :group
  belongs_to :product
end

I wanted to minimize my database queries so I decided to use includes.In the console I tried something like,

groups = Group.includes(:products)

my development logs show the following calls,

Group Load (403.0ms)  SELECT `groups`.* FROM `groups`
GroupProduct Load (60.0ms)  SELECT `group_products`.* FROM `group_products` WHERE (`group_products`.group_id IN (1,3,14,15,16,18,19,20,21,22,23,24,25,26,27,28,29,30,33,42,49,51))
Product Load (22.0ms)  SELECT `products`.* FROM `products` WHERE (`products`.`id` IN (382,304,353,12,63,103,104,105,262,377,263,264,265,283,284,285,286,287,302,306,307,308,328,335,336,337,340,355,59,60,61,247,309,311,66,30,274,294,324,350,140,176,177,178,64,240,327,332,338,380,383,252,254,255,256,257,325,326))
Product Load (10.0ms)  SELECT `products`.* FROM `products` WHERE (`products`.`id` = 377) LIMIT 1

I could analyze the initial three calls were necessary but don’t get the reason why the last database call is made,

 Product Load (10.0ms)  SELECT `products`.* FROM `products` WHERE (`products`.`id` = 377) LIMIT 1

Any idea why this is happening?
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-01T11:33:02+00:00Added an answer on June 1, 2026 at 11:33 am

    I don’t think that fourth query is coming from this line of code (or the collection created by it). Even if you did something like groups.products.find(product_id) somewhere later, you would see a much more complex query, with the inner joins on the join table. It looks like there must be a separate Product.find(product_id) or something similar somewhere in your code.

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

Sidebar

Related Questions

I have a simple model class Ad < ActiveRecord::Base has_many :ad_items end class AdItem
I have the following model: class Deck < ActiveRecord::Base has_many :cards serialize :stats attr_accessible
i have following model setup class Category < ActiveRecord::Base has_ancestry :cache_depth => true, :depth_cache_column
I have the following models: require 'books_projects.rb' class Project < ActiveRecord::Base has_many :book_to_projects has_many
I have the following model structure: class Container(models.Model): pass class Generic(models.Model): name = models.CharacterField(unique=True)
I have the following model structure class User(db.Model) : nickname = db.StringProperty(required=True) fullname =
I have the following structure: class FeatureType(models.Model): type = models.CharField(max_length=20) def __unicode__(self): return self.type
I have the following two models: class Message < ActiveRecord::Base belongs_to :to_user, :class_name =>
Hi Stackoverflow people, in my current project, I have the following model structure: class
I have the following model structure below: class Master(models.Model): name = models.CharField(max_length=50) mounting_height =

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.