你的两个用户有什么样的“角色特定数据”?
我和你正在开发一个应用程序的情况非常相似。我选择了使用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