簡體   English   中英

用於具有關聯和身份驗證的用戶模型的Rails STI

[英]Rails STI for User model with associations and authentication

我正在開發一個站點,醫生可以管理他們的病人,約會等。一個醫生可以有多個助手來幫助他們進行這些活動,並且一個助手可以屬於多個醫生。

醫生和助手都應該能夠使用相同的登錄表單登錄,但是出於明顯的原因,他們根據角色具有不同的權限和關聯,例如,患者與醫生關聯,而不與助手關聯。

您如何建議將是解決此問題的最佳方法? 我正在考慮為授權部分使用CanCan,對於身份驗證部分可以使用Doctor和Assistant可以繼承的User模型的STI,但是STI是否可以處理這兩個模型之間的HABTM,以及每個模型可能具有的其他關聯?

提前謝謝

這應該可以解決問題。

只要確保您使用“類型”來設置用戶身份即可。 然后,您可以使用以下內容:

current_user.is_doctor?

確定進入區域。

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

喊,如果您需要更多信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM