kirlev
kirlev

Reputation: 700

rails: how to validate uniqueness of a field in two tables?

I have two tables in my app's database for two kinds of users, therapists and patients, both sign in/out with a username and password, I want to make sure that there can't be a therapist and patient with the same username, how can I do that?

also, is it possible to use the same sessions controller for both therapists and patients?

Upvotes: 0

Views: 96

Answers (1)

Todd A. Jacobs
Todd A. Jacobs

Reputation: 84343

Refactor Your Design

You shouldn't have two different tables for users. You should probably have a single User table, with a field that designates the role of the User. This makes it particularly easy to select the user's role using radio buttons or a drop-down in the web application, and also allows you to enforce uniqueness on aspects of a User record.

However, why is it impossible for a patient named "John Smith" to have a therapist named "John Smith?" I'd think about that before enforcing that particular uniqueness constraint.

Finally, a session is generally shared across controllers, and a controller can certainly read from more than one model at a time. In your case, refactoring to a common User model will reduce the need to do something complex in this regard.

Upvotes: 1

Related Questions