2011-11-25 87 views
2

我很期待創建兩種模式:培訓師&客戶端。在多個用戶類中使用魔法(auth)的最佳方法是什麼?

註冊這兩種類型的模型時,共享基本身份驗證信息,例如電子郵件&密碼。

因此,我想使用Sorcery爲我進行身份驗證,該默認情況下創建用戶模型。

通過StackOverflow搜索我明白我可以使用單表繼承,這是大多數人發現有問題。

是否有更好/更簡單的解決方案讓這兩種類型的用戶共享基本身份驗證信息,但是是包含角色特定數據的單獨模型?


我很抱歉,如果我混淆了事情。

回答

1

你的兩個用戶有什麼樣的「角色特定數據」?

我和你正在開發一個應用程序的情況非常相似。我選擇了使用CanCan的基於角色的方法。

class User < ActiveRecord::Base 
    has_one :client_profile # or whatever a client has here 
    has_one :trainer_profile # or whatever a trainer has here 
end 

然後,你需要定義自己的能力

class Ability 
    include CanCan::Ability 

    def initialize(user) 
    user ||= User.new # anonymous user 
    if user.is? :trainer 
     can :create, TrainerProfile 
     # some other trainer specific roles here, like editing his/her profile 
    elseif user.is? :client 
     can :create, ClientProfile 
     # some other client specific roles here, like editing his/her profile 
    end 
    end 
end 

當然,上面的代碼假定的是什麼?方法在User類中檢查用戶角色。

有關CanCan的更多信息,請參見CanCan wiki以及Railscast on CanCan

+0

首先感謝您的回覆!具體的數據是,例如:一個教練有一個專業化,max_clients,vanity_url等,一個客戶端可能有一個trainer_id,starting_weight,goal_weight等。我希望在它們之間創建的關係是has_many和belongs_to。希望這可以稍微澄清一點。今天晚些時候我會坐下來看看CanCan。然而,對於我的情況,我正在考慮構建自定義身份驗證系統,因此培訓師和客戶可以創建單獨的帳戶等。不是非常幹,但可能工作。讓我知道你的想法,再次感謝。 –

+1

在這種情況下,我不會太害怕單表繼承。另一個可能更好的建議是創建兩個模型,「Trainer」和「Client」,它們都調用'authenticates_with_sorcery!'。 – Feech

相關問題