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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:57:45+00:00 2026-05-13T14:57:45+00:00

Given the following models: class Room < ActiveRecord::Base has_many :contracts end class Contracts <

  • 0

Given the following models:

class Room < ActiveRecord::Base  
  has_many :contracts
end

class Contracts < ActiveRecord::Base
  # columns
  # start_date Date
  # end_date Date

  belongs_to :room
end

The contracts of one room are not overlapping. My question is, how i am be able to find gaps between contracts. An Example:

room = Room.create
c1 = room.contracts.create(:start_date => Date.today, :end_date => 1.month.since)
c2 = room.contracts.create(:start_date => 2.months.since, :end_date => 4.months.since)
rooms = Room.with_contract_gaps #rooms == [room]

The bonus round would be the possibility to search for gaps with a specific date-range or even better to fetch all gaps as date-ranges in as hash or array.

gaps = Room.contract_gaps
gaps # {1 => [((1month.since+1.day)..(2.months.since-1.day))]}

I have already searched via google and found Inverting Date Ranges. But i have honestly not really an idea, how i can use it in this specific case.

It would be great if someone has a solution or some helpful tips to solve this problem.

  • 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-13T14:57:45+00:00Added an answer on May 13, 2026 at 2:57 pm

    The fastest method would be to find gaps using an SQL query. However, this would be complicated and not map well to an ActiveRecord model, so the next fastest way I can think of is to sort contracts chronologically in the database and find gaps in Ruby, iterating through each result and appending rooms and dates to an array as you encounter them.

    However, if you need faster access to gaps, or you’re doing a lot of gap manipulation, or if these gaps are in a sense the “product” being sold, you might be better off with a different model. What about a Slot, which is the minimum possible contract length (eg, one month)? You’d create slots for every room and every month for the next few years. Each has slot.available == true to start, and the after_save callback of the Contract model sets available = false where necessary. With this setup you could define your gap-finder (Room.with_available_slots) more easily:

    class Contract < ActiveRecord::Base
      has_many :slots
    end
    
    class Slot < ActiveRecord::Base
      belongs_to :room
      belongs_to :contract
    end
    
    class Room < ActiveRecord::Base
      has_many :slots
      has_many :contracts, :through => :slots
    
      named_scope :with_available_slots,
        :joins      => :slots,
        :conditions => {:slots => {:contract_id => nil}},
        :select     => "*, COUNT(*) AS num_slots",
        :group      => "room_id",
        :having     => "num_slots > 0"
    end
    

    This design has other useful features like the ability to prevent booking of certain dates, apply different pricing to certain slots, etc. It’s not as clean as your design, but in my experience it works better with real-world data because exceptions are more easily handled.

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

Sidebar

Ask A Question

Stats

  • Questions 357k
  • Answers 357k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You probably want to call setlocale() first, "LC_ALL" should do… May 14, 2026 at 9:06 am
  • Editorial Team
    Editorial Team added an answer Linux Ubuntu Desktop Jaunty Firebug FireCookie Pixel Perfect Web developer… May 14, 2026 at 9:06 am
  • Editorial Team
    Editorial Team added an answer Your code should look like this: var par = [];… May 14, 2026 at 9:06 am

Related Questions

I'm struggling with the design of a django application. Given the following models: class
Given the following models (cut down for understanding): class Venue(models.Model): name = models.CharField(unique=True) class
Given the following code: Models class Log { ... Ticket Ticket { get; set;
Given the following models:(don't mind the TextFields there're just for illustration) class Base(models.Model): field1
Given the following models adapted from http://www.djangoproject.com/documentation/models/generic_relations/ class TaggedItem(models.Model): A tag on an item.

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.