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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:23:46+00:00 2026-06-17T17:23:46+00:00

I have two models, with those associations: class Product < ActiveRecord::Base has_many :side_orders has_many

  • 0

I have two models, with those associations:

class Product < ActiveRecord::Base
  has_many :side_orders
  has_many :garnishes, :through => :side_orders

  attr_accessible :garnishes_number
end

Garnishes are also products, so it’s a self-joined association:

class SideOrder < ActiveRecord::Base
  belongs_to :product
  belongs_to :garnish, :class_name => "Product", :foreign_key => "garnish_id"

  attr_accessible :list_number
end

One product may have many lists of garnishes (so that customer may choose different garnishes when they order one product). It’s one list per choice. One product don’t have more than “garnishes_number” lists of choices.

In my application, in the admin part, I have to do something like this:

<% Product.all.each do |product| %>
  <% for i in 0..(product.garnishes_number - 1) %>
     Lists of garnishes:
     <%= product.garnishes.where("side_orders.list_number = ?", i) %> <br />
  <% end %>
<% end %>

It’s just an example… Problem is that Rails do one query per value of “i”, so if a product has 8 lists, Rails will perform 8 queries to the database… And when there are more than hundreds of products, it becomes really slow…

Is there a way to optimize such a query? Includes doesn’t seem to work… If anyone has any idea or maybe a different logic, don’t hesitate to answer.

[Edit] The fact that I want to access each list of garnishes is important because I want them to appear in a table in such way:

<table>
  <thead>
    <td>Product</td>
    <td>List of garnishes </td>
  </thead>
  <tbody>
    <% Product.all.each do |product| %>
      <% for i in 0..(product.garnishes_number - 1) %>
        <tr>
          <td>product.name</td>
          <td>
              List number <%= i %>: 
              <%= product.garnishes.where("side_orders.list_number = ?", i) %>
          </td>
        </tr>
      <% end %>
    <% end %>
  </tbody>
</table>

I didn’t put that in the first place, because I didn’t want my question to be too long… But, I think I should have wrote it down already so that my problem could be more understandable… Sorry ^^’

Thanks a lot!
Kulgar.

  • 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-17T17:23:47+00:00Added an answer on June 17, 2026 at 5:23 pm

    you can do it by following way

    <% Product.all.each do |product| %>
     <% temp = Array.new %>
     <% for i in 0..(product.garnishes_number - 1) %>
       <% temp.push(i) %>
     <%end%>   
      Lists of garnishes:
      <%= product.garnishes.where("side_orders.list_number in(?)", temp ) %> <br />
    <% end %>
    

    This will call where query only one time for each product

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

Sidebar

Related Questions

I have two models, Landscape: class Landscape < ActiveRecord::Base has_many :images, :as => :imageable
I have two models connected by a join table: class Publication < ActiveRecord::Base attr_accessible
On my server, I have two models: Broadcast class Broadcast < ActiveRecord::Base validates_presence_of :content
I have two models: User and Car with the following associations: User has_many Car
I have two models, associated with a HABTM (actually using has_many :through on both
What I have now: class User < ActiveRecord::Base has_many :people end ... and... class
I relised I have lots of models view models with those two properties public
i have these two models public class Group { [Key] public int GroupID {
I have two models: class Contact(models.Model): name = models.CharField(max_length=255) class Campaign(models.Model): contact = models.ForeignKey(Contact,
I have two models like this: class ClassA(models.Model): ida = models.AutoField(primary_key=True) classb = models.ForeignKey(ClassB)

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.