Rails 3. 如何为人建模?
我有这个发票应用程序。
目前我有以下 4 个模型:
人员:
- 联系人(他们就像客户,将向他们发送发票)
- 用户(发送发票的人)
- 公司(用户属于公司)
- 组织(联系人属于组织)。
当前的设置并没有真正起作用。感觉不自然。
我想到的另一个选择是创建一个属于组织的人员模型。
人们,将有一个列 type_id (类型:客户端、管理员,将来添加更多)
但我不知道,不知怎的,感觉 type_id 列不应该在那里引用一个两行表。
在这种情况下您会使用什么模型设置?
---- 添加澄清: ----
请记住,在不久的将来,一些客户端将能够登录。 注意:如果客户只是个人,那么他不属于某个组织。
用户代表登录并发送发票的人。他属于一家公司。
联系人代表要开具发票的人,他可能属于也可能不属于某个组织。
I have this Invoicing App.
Currently I have these 4 models:
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)
But I don't know, somehow it feels like the type_id column shouldn't be there to reference just a two row table.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
多态关联可能对您的情况有所帮助。
由于所有实体都是人,因此您可以实现如下所示的内容:
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.
你目前的设置对我来说似乎还不错。
如果联系人和用户因不同的数据要求而有很大不同(我猜他们会的),那么单表继承会带来更多挑战。可以这样想:如果您将有一堆空列(具体取决于它是用户还是联系人),那么不要使用 STI。如果空字段很少,那么就可以了。
公司和组织领域有多重要?将收到发票的联系人是否可以只包含公司名称和地址的文本字段?也许这将有助于简化事情。
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. 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.