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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T01:48:28+00:00 2026-06-17T01:48:28+00:00

In my Rails app I have classes that depend on one another, ParkingSpace and

  • 0

In my Rails app I have classes that depend on one another, ParkingSpace and Parking Lot.

A ParkingLot has many ParkingSpaces and the ParkingSpace belongs to a ParkingLot.

I want to ensure that the same ParkingSpace can’t be inserted into the ParkingLot again.

Typically, I would use a uniqueness validation to ensure that the ParkingSpace can’t be repeated parking space have very generic names

ParkingLot with id:1 can have ParkingSpaces 1, 2 and 3 on it.

ParkingLot with id:2 can also have ParkingSpaces 1, 2 and 3 on it.

But the above parking lots should not be able to have two parking spaces with the same name.

How can I validate this in Rails?
How can I validate this at the database level?

I’m considering joining the two fields together in a unique way and then calling unique on that. Is this a valid approach? (Not exactly sure how to do this at the database level, but all my validations need to be present there.)

(using Postgres)

  • 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-17T01:48:30+00:00Added an answer on June 17, 2026 at 1:48 am

    Assuming your parking space and an identifier (like a name), you would do something in the ParkingSpace class like:

    class ParkingSpace < ActiveRecord::Base
    
      belongs_to :parking_lot
    
      validates_uniqueness_of :name, :scope => :parking_lot_id
    
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 2 classes in my rails app. App, and User. A User has
I have built a simple rails app with three classes that inherit from ActiveRecord
In a rails 3 app I'm building I have these three classes: class Instrument
I am creating a rails app and have used this code in one of
I have a Ruby script in my Rails app that I use to load
I have a rails setup that is something like this. app/service/TestService.rb class TestService def
I have a Rails app which has 2 databases. Legacy DB with table called
I am working within a Ruby on Rails app. I have a loop that
I have a Rails app with a few model classes (e.g. Category , Subcategory
I have a Rails 3 app that uses RSpec2 as my testing framework, and

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.