你的兩個用戶有什麼樣的「角色特定數據」?
我和你正在開發一個應用程序的情況非常相似。我選擇了使用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。
首先感謝您的回覆!具體的數據是,例如:一個教練有一個專業化,max_clients,vanity_url等,一個客戶端可能有一個trainer_id,starting_weight,goal_weight等。我希望在它們之間創建的關係是has_many和belongs_to。希望這可以稍微澄清一點。今天晚些時候我會坐下來看看CanCan。然而,對於我的情況,我正在考慮構建自定義身份驗證系統,因此培訓師和客戶可以創建單獨的帳戶等。不是非常幹,但可能工作。讓我知道你的想法,再次感謝。 –
在這種情況下,我不會太害怕單表繼承。另一個可能更好的建議是創建兩個模型,「Trainer」和「Client」,它們都調用'authenticates_with_sorcery!'。 – Feech