如何使用 ActiveRecord 存储/建模用户/facebook 用户/linkedin 用户等?
我的应用程序有
- “普通”用户:通过典型注册页面访问的用户
- facebook(FB) 用户:来自 Facebook 连接的用户
- “FB 普通”用户:可以使用电子邮件/密码登录的用户 * FB 连接
此外,还有很多其他类似 openID 的登录方法(我不认为 openID 本身是可以接受的,因为它不链接帐户并允许第 3 方特定功能(发布到 twitter、添加 FB 帖子等) ))
那么,我该如何建模呢?
现在我们有带有 #facebook_user 的 User 类?已定义 - 但对于“FB-普通”用户来说它会变得混乱 - 而且所有验证都变得非常棘手且难以解释。另外,还有像#deliver_password_reset这样的方法!对于仅使用 Facebook 的用户来说,这毫无意义。 (这很蹩脚)
我已经考虑过 STI(User::Facebook、User::Normal、User::FBNormal 等),这使得验证超级流畅,但它无法扩展到其他连接类型,以及它们之间的所有排列... User::FacebookLinkedInNormal(wtf?) 用一堆模块来做这件事我认为会很糟糕。
还有其他想法吗?
My app has
- "normal" users: those which come through a typical signup page
- facebook(FB) users: those which come from Facebook connect
- "FB-normal" users: a user that can log with both email/password * FB connect
Further, there's the a slew of other openID-ish login methods (I don't think openID itself will be acceptable since it doesn't link up the accounts and allow the 3rd party specific features (posting to twitter, adding a FB post, etc etc))
So, how do I model this?
Right now we have User class with #facebook_user? defined -- but it gets messy with the "FB-normal" users - plus all the validations become very tricky and hard to interpret. Also, there are methods like #deliver_password_reset! which make no sense in the context for facebook-only users. (this is lame)
I've thought out STI (User::Facebook, User::Normal, User::FBNormal, etc.) This makes validations super slick, but it doesn't scale to other connection types, and all the permutations between them... User::FacebookLinkedInNormal(wtf?) Doing this with a bunch of modules I think would suck a lot.
Any other ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会分解这些问题,似乎在一个表中保留具有截然不同字段的记录是没有意义的。
只需有一个不保存任何凭据的用户模型/表,并使其与帐户/登录方法具有 has_many 关系,您可以在其中使用 STI 来对不同类型进行建模。
I would split up the concerns, seems like it doesn't make sense to keep records that have vastly different fields in one table.
Just have one User model/table that doesn't save any credentials, and make that have a has_many relationship to an Account/Login Method, where you could use STI to model the different types.
您遇到了对象关系阻抗不匹配地雷之一。
Facebook 和 LinkedIn 都通过使用非关系键/值存储来解决这个问题。 Facebook 使用 Cassandra,LinkedIn 使用 伏地魔计划。
当您不受关系模型约定的约束时(例如,给定表的所有行都具有相同的列),您可以获得更大的灵活性并改变每行的属性。但这样做会牺牲关系模型的一些优势(例如,给定表的所有行都具有相同的列)。
在 RDBMS 中,我将使用类表继承来构建它。
You're running into one of those object-relational impedance mismatch landmines.
Facebook and LinkedIn each solve this by using non-relational key/value stores. Facebook uses Cassandra, and LinkedIn uses Project Voldemort.
When you're not bound by the conventions of the relational model (e.g. all rows of a given table have the same columns), you can gain more flexibility and vary the attributes per row. But doing this, you sacrifice some strengths of the relational model (e.g. all rows of a given table have the same columns).
In an RDBMS, I would architect this using Class Table Inheritance.