Checking for uniqueness
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?
Create a unique index + add a scoped validation.
https://stackoverflow.com/questions/4123610/how-to-implement-a-unique-index-on-two-columns-in-rails
https://stackoverflow.com/questions/34424154/rails-validate-uniqueness-of-two-columns-together
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
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.