简体   繁体   English

Rails 3.如何为人建模?

[英]Rails 3. How to model people?

I have this Invoicing App. 我有这个发票应用程序。

Currently I have these 4 models: 目前,我有以下4种型号:

People: 人:

  • Contacts (they're like clients, they will be sent invoices to) 联系人(他们就像客户一样,会将发票发送给)
  • Users (people that send invoices) 用户(发送发票的人)
  • Company (users belong to company) 公司(用户属于公司)
  • Organization (contacts belongs to organization). 组织(联系人属于组织)。

This current setup doesn't really click. 当前的设置并没有真正点击。 It doesn't feel natural. 感觉不自然。

The other choice I thought about was to create a People model that belongs to Organization. 我想到的另一个选择是创建一个属于组织的人员模型。

People, will have a column type_id (types: client, admin, add more in the future) 人员,将会有一列type_id(类型:客户端,管理员,以后再添加)

But I don't know, somehow it feels like the type_id column shouldn't be there to reference just a two row table. 但是我不知道,以某种方式感觉type_id列不应只引用两行表。

What model setup would you use in this case? 在这种情况下,您将使用哪种模型设置?

---- Added for clarification: ---- ---- 为澄清起见: ----

Remember that in the near future some clients will have ability to login. 请记住,不久的将来某些客户端将能够登录。 Note: if the client it's just an individual, then he won't belong to an organization. 注意:如果客户只是个人,那么他就不会属于组织。

User represents the person that logs in and sends the invoice. 用户代表登录并发送发票的人。 He belongs to a company. 他属于一家公司。

Contact represents the person that is being invoiced, he may or may not belongs to an organization. 联系人代表正在开发票的人,他可能属于或可能不属于组织。

楷模

Polymorphic associations may help in your situation. 多态关联可能对您的情况有所帮助。

Since all that entities are people, you can implement something like this: 由于所有实体都是人,因此您可以实现以下内容:

模型

In my experience the single-table inheritance approach fits multiple user classes if they have differing logic. 以我的经验, 单表继承方法适合具有不同逻辑的多个用户类。

But if you have a completely separate Contact and User, and Contacts can't even log in or don't share fields with Users, you should make it a separate model. 但是,如果您拥有完全独立的“联系人”和“用户”,并且“联系人”甚至无法登录或不与“用户”共享字段,则应将其设置为单独的模型。

Your current setup doesn't seem too bad to me. 您当前的设置对我来说似乎还不错。

If the Contacts and Users are going to be substantially different with different data requirements (which my guess is they would), Single Table Inheritance poses more challenges. 如果“联系人”和“用户”将因数据需求不同而大不相同(我想是这样),那么“单表继承”将带来更多挑战。 Think of it like this: if you're going to have a bunch of empty columns depending on whether it's a user or contact, then don't use STI. 可以这样想:如果要根据用户或联系人使用一堆空列,则不要使用STI。 If very few empty fields, then it could be ok. 如果空白字段很少,那就可以了。

How important are the Company and Organization fields? 公司和组织领域有多重要? Can the contacts who will be sent invoices just have a text field for company name and address? 将要发送发票的联系人能否仅在文本字段中输入公司名称和地址? Maybe that will help simplify things. 也许这将有助于简化事情。

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

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