简体   繁体   English

用于具有关联和身份验证的用户模型的Rails STI

[英]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? 我正在考虑为授权部分使用CanCan,对于身份验证部分可以使用Doctor和Assistant可以继承的User模型的STI,但是STI是否可以处理这两个模型之间的HABTM,以及每个模型可能具有的其他关联?

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. 喊,如果您需要更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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