I have 3 models in my rails application, User, Course, and CourseTemplate.
A Course belongs to a User and a CourseTemplate belongs to a Course.
What I want to do is to validate the uniqueness between the CourseTemplate name and the User id.
Is this possible?
Without denormalization of data
With denormalization of data
If you're ok with some denormalization of your data, you could add
user_idto CourseTemplate, and then simply use the scope feature of validates uniqueness.Below I show how to use callbacks to maintain the user_id in the CourseTemplate. Note that it assumes a course cannot be moved to a different user.
If the course can be moved to a different user, you should add a callback on Course: