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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:52:58+00:00 2026-05-26T21:52:58+00:00

I have a script that creates a soccer fixture. Each club can play just

  • 0

I have a script that creates a soccer fixture.

Each club can play just one game with the other clubs and it could be at home or away.

This is my actual code which is not working with 20 clubs (It’s creating 145 matches instead of 190 and the clubs are not playing the same amount of games)

  clubs_to_play = all_clubs = Club.all #problematic line

  all_clubs.each do |club|
    home = true
    clubs_to_play.delete(club)
    clubs_to_play.each do |club_to_play|
      f = Fixture.new
      if (home)
        f.home = club
        f.away = club_to_play
        home = false
      else
        f.home = club_to_play
        f.away = club
        home = true
      end
      f.save
    end
  end
end

But if I change the first line to be:

clubs_to_play = Club.all
all_clubs = Club.all

The script works and generates 190 matches. Why is that?

  • 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-26T21:52:59+00:00Added an answer on May 26, 2026 at 9:52 pm

    When you do this:

    clubs_to_play = all_clubs = Club.all
    

    What you’re basically doing is this:

    all_clubs = Club.all
    clubs_to_play = all_clubs
    

    They are the same object (which you will see if you look at their object_ids).

    DataMapper does not materialize record sets until a kicker method is invoked. That basically means no SQL is executed until you start iterating. Of course, since these variables both point to the same object, when you start iterating all_clubs, you pull all the records from the database in both all_clubs and clubs_to_play, since they are the exact same object.

    Conversely, when you do this:

    all_clubs = Club.all
    clubs_to_play = Club.all
    

    Here all_clubs and clubs_to_play reference different objects. That is to say, they have different object_ids and when you iterate all_clubs all of its records are materialized, but clubs_to_play remains unaffected until you iterate it (and delete values from the other collection).

    Since your algorithm is assuming that all_clubs and clubs_to_play can be modified independently of one other, you are getting undesired behaviour when they are the same object.

    Try something like this (slightly more functional) approach:

    Club.all.combination(2).each_with_index do |(club1, club2), idx|
      Fixture.create(
        :home => idx.even? ? club1 : club2,
        :away => idx.even? ? club2 : club1
      )
    end
    

    There are technically more functional ways to maintain the alternation between home and away, but they are more convoluted. Enumerable#combination returns all possible (unique) combinations of values in the collection.

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

Sidebar

Related Questions

I have Ruby script that creates a proxy so that I can make HTTP
I have a PHP script that creates other PHP files based on user input.
I have this script that creates a new tab in JQuery. Can you check
Basically I have a PHP script that creates a div for each item in
I have php script that creates a temporary watermark image for users that are
I have a script that creates an image and calls imagepng to output it
Hi I have a script that creates a file and stores it on the
I have a PHP script that creates large complex tables. I am attempting to
Say I have a PHP script that creates a cookie that expires 10 days
In my extension, I have a content script that creates a <div> containing an

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.