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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:52:07+00:00 2026-05-27T01:52:07+00:00

I am programming a booking system. I want my users to be able to

  • 0

I am programming a booking system.
I want my users to be able to book only one (or a defined number of) resources at a time. However, I do not want to remove “past” reservation for my database, since it will be used for invoicing purposes. On reservation creation, I need to validate that a user has not exceeded its reservation quota, which means has not more than “quota” reservations in the future.

class User < ActiveRecord::Base
    has_many :reservations

    def active_reservations
        #maybe worth to rewrite with a "find"?
        my_list = []
        reservations.each do |reservation|
        if (not reservation.past?)
            my_list.push(reservation)
        end
        return my_list
    end

class Reservation < ActiveRecord::Base
    belongs_to :user
    validate :respect_user_quota
    def past?
        return (date < Date.now)

    def respect_user_quota
        if (user.active_reservations.count > user.quota)
            errors.add(:user, "User quota exceeded!")

Is this the right way to implement this validation? What could be wrong there (I never see the error message). Should the quota validation be moved to the user class?

  • 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-27T01:52:08+00:00Added an answer on May 27, 2026 at 1:52 am

    I would try and do this more simply and move the validation to user.

    class User < ActiveRecord::Base
      has_many :reservations
    
      validate :reservation_quota
        if sum(reservations.active) > quota  # User.quota is implied here
          errors.add(:user, "User quota exceeded!")
        end
    
    class Reservation < ActiveRecord::Base
      belongs_to :user
      def active
        active? 1 : 0  
         # If there's a boolean 'active' flag the ? method gets created automatically.    
         # This could be (reservation_date < Date.now)? ? 1 : 0 for you.
         # Using `(expression)? ? true : false` is using the Ternary operator. 
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm developing a parents evening booking system as part of my A level programming
Which programming language or a library is able to process infinite series (like geometric
Programming is so new to me that I apologize for not knowing how to
I know this question isn't directly programming related, but since I want to be
Programming Clojure is currently available only in electronic form. I see it's available in
I am trying to develop a system where this application allows user to book
Programming In Scala explains that tuples' _N numbers are one-based, instead of zero-based, because
assembly programming in Emacs how to? I want Emacs to do following things 1.
Programming a Python web application, I want to create a text area where the
Programming Student here...trying to work on a project but I'm stuck. The project is

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.