2011-11-25 90 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

相关问题