Ask A Question

Notifications

You’re not receiving notifications from this thread.

Checking for uniqueness

Jon Ray Favorito asked in Rails

Hi people. I am working on a project that deals with enrollment activities such as generation of subject offerings and such. I have a model called Offering which has a has_many relationship with the model Schedules. Basically, an offering has many schedules. In order to check for conflicts the room_id which is an attribute of the model Offering has to be unique along with the attributes time and days in Schedule, any suggestion on how should i do this, without using a tedious brute force approach?

Reply
Reply

Hi, thanks for the response but I would like to clarify that I have to check columns coming from two models, I am not sure how it can be implemented using validators only, but as of now i am trying a brute force approach

Reply

You can create a custom validations to do anything you need (it runs arbitrary code you place in it)
Just make sure it's efficient with proper indexes etc.

I'm not sure if I got the model perfectly but if you inherent the details.
As in , pass the Schedule the room_id and than you can run a simple unique index on it without going through up/down relations.

Yes, you'll have an extra column (room_id on Schedule) you might not need and can get by association , but that's not a large price for simpler design.

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 76,990+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more. Icons by Icons8

    © 2023 GoRails, LLC. All rights reserved.