Rails 3 用户数据库设计问题 - 单表继承还是?
我正在开发一个点对点借贷应用程序,该应用程序有多种类型的用户(即借款人、贷方、赞助商等),这些用户具有不同的领域。此外,其中一些用户可以属于两件事(即贷方也可以是赞助商)。那么,在这种情况下单表继承是一个好主意吗?如果是这样,一个用户是否可以属于两个只有一个“类型”字段的组?如果性传播感染不是解决办法,那么最好的办法是什么?毕竟,使用不同的表需要将相同的信息保存到多个数据库,这似乎效率不高。
预先感谢您的帮助!
I am working on a peer lending application that has several types of users (ie. borrowers, lenders, sponsors, etc) that have different fields. Moreover, some of these users can belong to two things (ie. lenders can also be sponsors). So, is single table inheritance a good idea in this instance? And if so, can one user belong to two groups with only one "type" field? And if STI is not the way to go, what's the best way to do it? After all, using different tables will require saving the same info to multiple databases, which doesn't seem efficient.
Thanks in advance for any help!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为可以安全地假设每个用户的角色是由租赁/交易的类型决定的。也就是说,如果我们有一笔交易
D1
和两个用户U1
和U2
,那么正是该交易定义了U1< /code> 是发起人或借款人。
我的建议是将用户层次结构保留为 STI 并引入另一个类,例如
DealUserRole (Deal, User, Role)
。I think it's safe to assume that a role of each user is determined by the type of a lease/deal. That is, if we have a deal
D1
and two usersU1
andU2
, then it is the deal that defines whetherU1
is a sponsor or a borrower.What I suggest is to leave users hierarchy as an STI and introduce another class, say
DealUserRole (Deal, User, Role)
.你可以使用我一直在研究的 gem (CITIER) - http://peterhamilton.github.com/citier 进行继承,仍在开发中,但目前运行良好。如果不同的类别有很多独特的字段,那么比 STI 好得多。 STI 将使它们为空。另外,MTI 以后的可扩展性更强。我同意您还需要为用户引入一个角色。但我认为使用这个你可以将它链接到父类(用户类),这样它就适用于用户的所有子类型。
ETC
You could use the gem I have been working on (CITIER) - http://peterhamilton.github.com/citier to do your inheritance, still in development but currently functioning fine. Much nicer than STI if the different classes have lots of unique fields. STI would leave them Null. Plus MTI is more extensible later down the line. I agree you also need to introduce a role for the users. But I think using this you could link it to the parent (User class) so it would apply to all subtypes of user.
etc