Polymorphic association or STI?

Currently I have two types of users, Doctors and Patients. And both need to sign in. The database figures who has signed in. And depending on that a different web page is displayed. I have defined two different controllers DoctorsController and PatientsController. But Patients and Doctors are only present in the users table. If in the future I had to separate them into their own different tables then will I have to use polymorphic associations between Users and Doctors?

I mean having a users table which stores the password, name,email etc. and then putting doctor related info in the doctors table.
Will i be using polymorphic associations because a user could be a doctor or a patient but not both
Or will have to put a foriegn id in the doctors tables to link them back to the users table?

Thanks

@charlieanna, I recently faced a similar situation.

You can take a look at this gist: Polymorphic user with Devise · GitHub

I’ve modified a lot of it since then, but I hope it helps

Goose gave an awesome solution saying that in the future I could create extra table like DoctorsProfile and put in the extra data there and then have associations between the user table and the Profile table.

Yes, but is is more or less the same thing. Instead of having the data in the Doctor model, you’ll have in DoctorProfile. You’ll still be setting an polymorphic association between user and xProfile, I think.

No No, he told me to avoid polymorphic associations in such cases. He told to use something like this.

class Patient < User
has_one :profile, class_name: 'PatientProfile'
end

@halogenandtoast, could you explain why you’d advise against polymorphism? I’d love to hear your thoughts on this

There’s a chapter in Ruby Science on this that I highly recommend reading, but in general it’s better to prefer polymorphic associations over STI. It’s easier to make changes over the course of a model’s lifetime, and it helps to avoid ending up with a god class.

@seangriffin If I did follow polymorphism in this case then what I do call the method for getting the type of users down the tree, User.roleable_type and User.roleable_id using @user.role? And what name should go in the Doctors model class? Suppose when someone logs in I search the Users table to see if the user is present and then depending on the role of the user I will have to redirect to the home page showing the info of the doctor or the patient. So in this case should I get the doctor from the doctors table following the users table and then store that instance in the @doctor variable?

Currently I get the user from the users table and depending on the type of the user I render pages accordingly.