简体   繁体   中英

Rails STI for User model with associations and authentication

I am developing a site where doctors can manage their patients, appointments, etc. A doctor can have multiple assistants which help them with these activities, and an assistant can belong to multiple doctors.

Both doctors and assistants should be able to sign in from the same login form but for obvious reasons they have different permissions and associations depending on their role, for example patients are associated to the doctors, not to the assistant.

How you suggest would be the best way to approach this? I am contemplating CanCan for the authorization part, and maybe STI for a User model which Doctor and Assistant can inherit from for the authentication part, but can STI handle the HABTM between these 2, plus the other associations each model might have?

Thanks a lot in advance

This should do the trick.

Just make sure you use 'type' to set which the user is. You can then use things like:

current_user.is_doctor?

To determine access to areas.

module User

  def is_doctor?
   true if self.type == Doctor
  end

  def is_assistant?
   true if self.type == Assistant
  end
end


module Doctor < User
  has_and_belongs_to_many :assistants, :through => "assistant_assignments"
end


module Assistant < User
  has_and_belongs_to_many :doctors, :through => "assistant_assignments"
end

Shout if you need more info.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM