问题描述
我现在在应用程序的设计阶段。大多做的东西在纸上获得的应用模式的深入了解,确定什么,我需要等。而且我发现有一个问题,我不知道如何解决,或者更具体地说,我不知道如何来实现它,以保持干燥。因此,这里的交易:
I'm now in the design phase of application. Mostly doing stuff on the paper to get the insight of the application models, define what I will need and so on. And I found there one problem, which I don't know how to solve, or be more specific, I don't know how to implement it to keep it dry. So here is the deal:
我有应用程序,其中将有三种不同类型的用户/帐户:
I have application where will be three different types of users/accounts:
-
常规帐户 - 帐户与应用程序中的基本权利的基本配置文件,用户与普通帐户可以看到每个人的公众形象,可以修改自己的空间,只能有1个人资料图片,可以搜索,等等。
Regular account - Account has basic profile with basic rights in the application, User with regular account can see everyone's public profile, can modify own profile, can have only 1 profile picture, can search, etc.
高级帐户 - 拥有先进的配置文件,这意味着用户可以指定联系人信息,外观细节,有预订页面,就可以得到优惠,有日历
Advanced account - has advanced profile, which means that user can specify contact informations, appearance detail, has booking page, can get offers, have calendar
公司帐户 - 具有包含联系信息公司公司简介,可以提供给高级用户,并可以在应用程序浏览所有配置文件
Company account - has company profile which contains contact information for company, can make offers to Advanced users and can browse all profiles in the application
因此,基本上,我有一个用户模式共同为所有的用户和我决定去与设计的验证。在创建用户后,我想给他/她一个屏幕,在这里,他/她可以选择帐户类型 - 普通,高级,公司。而在这之后,他/她将在系统相应的权限,也相应的配置文件。他将不能在未来的帐户类型之间切换。
So basically, I have one User model together for all my Users and I decided to go with Devise for the authentication. After the User is created I want to show him/her a screen where he/she can choose account type - Regular, Advanced, Company. And after that he/she will have appropriate rights in the system and also appropriate profile. He will not be able to change between the account type in the future.
我想还留着code清洁,干燥,我很挣扎与寻找最佳的解决方案。现在我撞我的头靠在墙上,因为我无法弄清楚如何做到这一点。这里有一个问题:
I want also keep the code clean and dry and I'm quite struggle with finding the best solution for. And now I banging my head against the wall, because I can't figure out how to do it.Here is the question:
应该如何寻找最终的模型图这个。它应该是多态关联?还是应只是一般的:
How should look the final model diagram for this. Should it be polymorphic association? OR should it be just general:
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
attr_accessible :email, :password, :password_confirmation, :remember_me, :account_type
has_one :account
has_one :entertainer
has_one :company
after_create :create_account
def create_account
if self.account_type == 'Advanced'
self.entertainer = AdvancedAccount.create(user_id: self.id)
elsif self.account_type = "Company"
self.company = CompanyAccount.create(user_id: self.id)
else
self.account = RegularAccount.create(user_id: self.id)
end
end
end
或者我是完全错误的道路上?什么是这个问题的最佳实践?谢谢您的建议!
Or I'm on the totally wrong path? What are the best practices for this matter?Thank you for your advices!
推荐答案
,我发现的宝石是管理用户的能力和授权非常有用的。我用这个博客帖子过去来进行设置与色器件,虽然它可能已被人相当过时。
Instead of managing separate models for the different types of user, I've found the cancan gem to be very useful for managing user abilities and authorization. I used this blog post in the past to set it up with devise, though it may be fairly outdated by now.
这篇关于Rails应用程序有3种不同类型的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!