简体   繁体   English

哪个更好:Rails中的常量或模型

[英]Which is better: Constants or Models in Rails

Suppose I have a model called Staff. 假设我有一个名为Staff的模型。 The staff object is attached to a user and has a "title". staff对象附加到用户并具有“标题”。 Now, this title can either be a manager, doctor, engineer, nurse. 现在,这个头衔可以是经理,医生,工程师,护士。 Which is a better practice? 哪个是更好的做法?

Declare a constant and have a title attribute on the Staff model itself 声明一个常量并在Staff模型本身上具有title属性

ROLES = ["manager", "doctor", "engineer", "nurse"]

or create a model called Role and set it as a relationship to the Staff model? 或者创建一个名为Role的模型并将其设置为与Staff模型的关系? This model will ONLY have one attribute called title . 该模型只有一个名为title属性。

I'm pretty much aware that both will work, but then I just want to know you guys' perspectives/ideas regarding this one moving forward. 我非常清楚这两种方法都有效,但后来我只想知道你们对这一前进的看法/想法。

Thanks! 谢谢!

If your role system is so simple - there is no need to create another model with one field. 如果您的角色系统如此简单 - 则无需使用一个字段创建另一个模型。

To implement that feature I recommend you to look at rails ActiveRecord#enum : http://edgeapi.rubyonrails.org/classes/ActiveRecord/Enum.html 要实现该功能,我建议您查看rails ActiveRecord#enumhttp//edgeapi.rubyonrails.org/classes/ActiveRecord/Enum.html

This completely depends on the use case. 这完全取决于用例。 If you can foresee that users in you application will only ever have one role - then use an enum as suggested by Sergii. 如果您可以预见您应用程序中的用户将只有一个角色 - 那么请使用Sergii建议的枚举。 It is going to be the simplest and most performant option. 它将是最简单,最高效的选择。

It also depends on if you actually need a role system for authorization or if you are simply checking that users can not title themselves whatever they please. 它还取决于您是否确实需要一个角色系统进行授权,或者您只是检查用户无论他们喜欢什么都不能自我标题。

If you need a flexible system where users can have multiple roles than you will want to use a database table. 如果您需要一个灵活的系统,用户可以拥有多个角色而不是您想要使用数据库表。

Another question is how roles are created, is it good enough that it is a developer concern? 另一个问题是如何创建角色,它是否足够好以至于它是开发人员关注的问题? Could there be a need to be able to create role definitions from a GUI? 是否需要能够从GUI创建角色定义?

This example shows a common setup with a table for role definitions and a join table containing the roles assigned to users. 此示例显示了一个常见设置,其中包含用于角色定义的表和包含分配给用户的角色的连接表。

class User < ActiveRecord::Base
  has_many :user_roles
  has_many :roles, through: :user_roles

  def has_role?(name, resource = nil)
    scope = user_roles.joins(:role).where(
      role: { name: name }
    )
    scope = scope.where(resource: resource) if resource
    scope.any?
  end
end

# join table with roles assigned to users
class UserRole < ActiveRecord::Base
  belongs_to :user
  belongs_to :role
  belongs_to :resource, polymorphic: :true
end 

# Role definitions
class Role < ActiveRecord::Base
  has_many :user_roles
  has_many :users, through: :roles
end

Sounds like you just want title attribute on Staff . 听起来你只想在Stafftitle属性。

If you want to ensure that title is one of 'permitted' values, you can validate inclusion on model level: 如果要确保标题是“允许”值之一,可以在模型级别验证包含:

validates :title, inclusion: { in: %w(manager doctor engineer nurse) }

Always start small, get your tests green, then refactor when need arises. 始终从小处着手,让测试变为绿色,然后在需要时进行重构。

If you only want to define a role for people, that's just as simple as you thought: 如果您只想为人们定义一个角色,那就像您想象的那样简单:

  • new attribute title to the Staff model table Staff模型表的新属性title
  • new constant TITLES in the Staff model Staff模型中的新常量TITLES
  • new validation in the Staff model, as suggested by @petr-gazarov 正如@ petr-gazarov所建议的那样,在Staff模型中进行了新的验证

Should the need arise for a new model, be sure, you will have no choice but refactoring into that. 如果需要新模型,请务必确定,您将别无选择,只能进行重构。 As long as you have a choice, the answer is always the simplest. 只要你有选择,答案总是最简单的。

Soon though, you may start asking yourself what you want to do with this role differentiation -- spoiler, you probably want to learn more about authorization (eg "what can do", vs. authentication "who is"). 不久之后,您可能会开始问自己,您希望如何处理这种角色差异化 - 剧透,您可能希望了解更多有关授权的信息 (例如“可以做什么”,而不是“身份验证”)。
So I'll kill two birds with one stone and link you a good guide on role-based authorization , from a very easy and functional gem for defining role-based authorization: cancancan . 因此,我将一举两得,并为您提供基于角色授权的良好指南,从一个非常简单且功能齐全的宝石中定义基于角色的授权: cancancan

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM