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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:43:14+00:00 2026-05-25T23:43:14+00:00

I have an STI-based model called Buyable, with two models Basket and Item. The

  • 0

I have an STI-based model called Buyable, with two models Basket and Item. The attributes of concern here for Buyable are:

  • shop_week_id
  • location_id
  • parent_id

There’s a parent-child relationship between Basket and Item. parent_id is always nil for basket, but an item can belong to a basket by referencing the unique basket id. So basket has_many items, and an item belongs_to a basket.

I need a method on the basket model that:

Returns true of false if there are any other baskets in the table with both the same number of and types of items. Items are considered to be the same type when they share the same shop_week_id and location_id.

For ex:

Given a basket (uid = 7) with 2 items:

item #1

  • id = 3
  • shop_week_id = 13
  • location_id = 103
  • parent_id = 7

item #2

  • id = 4
  • shop_week_id = 13
  • location_id = 204
  • parent_id = 7

Return true if there are any other baskets in the table that contain exactly 2 items, with one item having a shop_week_id = 13 and location_id = 103 and the other having a shop_week_id = 13 and location_id = 204. Otherwise return false.

How would you approach this problem? This goes without saying, but I am looking for a very efficient solution.

  • 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-05-25T23:43:15+00:00Added an answer on May 25, 2026 at 11:43 pm

    To clarify my query, and somewhat vague description of the table columns of the “buyable” table, The “Parent_ID” is the basket in question. The “Shop_Week_ID” is the consideration for baskets to be compared… don’t compare a basket from week 1 to week 2 to week 3. The #ID column appears to be a sequential ID in the table, but not the actual ID of the item to be compared… The Location_ID appears to be the common “Item”. In the scenario, assuming a shopping cart, Location_ID = 103 = “Computer”, Location_ID = 204 = “Television” (just for my interpretation of the data). If this is incorrect, minor adjustments may be needed, in addition to the original poster showing a list of say… a dozen entries of the data to show proper correlation.

    So, now, on to my query.. I’m doing a STRAIGHT_JOIN so it joins in the order I’ve listed.

    The first query for alias “MainBasket” is exclusively used to query how many items are in the basket in question ONCE, so it doesn’t need to be re-joined/queried again for each possible basket to match. There is no “ON” clause as this will be a single record, and thus no Cartesian impact, as I want this COUNT(*) value applied to EVERY record in the final result.

    The NEXT Query is to find a DISTINCT OTHER Basket where at LEAST ONE “Location_ID” (Item) within the same week as the parent in question… This could result in other baskets having 1, same or more entries than the basket. But if there are 100 baskets, but only 18 have at least 1 entry that matches 1 item in the original basket, you’ve just significantly cut down the number of baskets to do final compare against (SameWeekSimilar alias result).

    Finally is a Join to the buyable table again, but based on a join for the SameWeekSimilar, but only on per “other” basket that had a close match… No specific items, just by the basket. The query used to get the SameWeekSimilar already pre-qualified the same week, and at least one matching item from the original basket in question, but specifically excluding the original basket so it doesn’t compare to itself.

    By doing a group at the outer level based on the SameWeekSimilar.NextBasket, we can get the count of actual items for that basket. Since a simple Cartesian join to the MainBasket, we just grab the original count.

    Finally, the HAVING clause. Since this is applied AFTER the “COUNT(*)”, we know how many items were in the “Other” baskets, and how many in the “Main” basket. So, the HAVING clause is only including those where the counts were the same.

    If you want to test to ensure what I’m describing, run this against your table but DO NOT include the HAVING clause. You’ll see which were all the POSSIBLE… Then re-add the HAVING clause and see which ones DO match same count…

    select STRAIGHT_JOIN
          SameWeekSimilar.NextBasket,
          count(*) NextBasketCount,
          MainBasket.OrigCount
       from 
          ( select count(*) OrigCount
               from Buyable B1
               where B1.Parent_ID = 7 ) MainBasket
    
          JOIN
    
          ( select DISTINCT
                  B2.Parent_ID as NextBasket
               from
                  Buyable B1
                     JOIN Buyable B2
                        ON B1.Parent_ID != B2.Parent_ID
                       AND B1.Shop_Week_ID = B2.Shop_Week_ID
                       AND B1.Location_ID = B2.Location_ID
               where
                  B1.Parent_ID = 7 ) SameWeekSimilar
    
           Join Buyable B1
              on SameWeekSimilar.NextBasket = B1.Parent_ID
    
        group by
           SameWeekSimilar.NextBasket
    
        having
           MainBasket.OrigCount = NextBasketCount
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have STI models in my Rails application. The ancestor model has validations with
I have two models that are not related (in the db sense) but have
I am using STI for my user models. I have an User class, and
I understand how STI works, in that I have say a Post model that
My Rails application uses STI where I have two different types of Customers. One
I have a model object which subclasses ActiveRecord. Additionally, using STI, I have defined
I have a Model Property which has subclasses using STI, and which I would
I am using Rails and postgres. I have a couple of models using STI
I have STI implementation as follows: class Automobile < ActiveRecord::Base end class Car <
I have a Rails app that uses STI to handle different types of Users,

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.