Skip to main content
Ask A Question
You’re not receiving notifications from this thread.

Checking for uniqueness

Rails • Asked by Jon Ray Favorito

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?

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.

Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 30,242+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.