I'm still learning Rails and how to best design my resource structure. Could someone help me with this? Here's my problem:
我还在学习Rails以及如何最好地设计我的资源结构。有人可以帮我吗?这是我的问题:
I am designing a specialized social network with profiles - every account has one profile. At the moment, an Account stores basic info about the user (username, password, etc.) from when they first sign up. A Profile stores other thinks like a picture link, answers to personality questions (linked back to a Profile via foreign key), and maybe more in the future. There is a one-to-one relationship between Account and Profile. Users can view/edit their own profile and view profiles belonging to others.
我正在设计一个带有个人资料的专业社交网络 - 每个帐户都有一个个人资目前,帐户会在首次注册时存储有关用户的基本信息(用户名,密码等)。个人资料存储其他人认为像图片链接,个性问题的答案(通过外键链接回个人资料),以及将来可能更多。帐户和个人资料之间存在一对一的关系。用户可以查看/编辑自己的个人资料并查看属于他人的个人资料。
My questions:
我的问题:
-
Is it a good idea to split these two into separate resources altogether (i.e. have two different models) or collapse them into one model with two controllers? I have tried the latter, and it almost seems like more trouble than it's worth. Am I, in this case, fighting against the framework? I'm not sure yet whether users should be able to have more than one account in the future.
将这两者完全分开(即有两个不同的模型)或将它们合并为一个带有两个控制器的模型是一个好主意吗?我尝试了后者,它似乎比它的价值更麻烦。在这种情况下,我是否反对该框架?我不确定用户将来是否应该拥有多个帐户。
-
If I do split them, should I use a singular or plural resource for the profile? I noticed in the language at guide.rubyonrails.org that get (show) and put (update) work with the ONE and ONLY profile resource. My question with respect to my current situation is: "one and only" with respect to what? The "one and only" profile for the current user's account or with respect to the entire site? If so, how could I have the current user view profiles of other users - should I use a URL parameter like so:
如果我拆分它们,我应该为配置文件使用单数或复数资源吗?我在guide.rubyonrails.org的语言中注意到,获取(显示)和放置(更新)使用ONE和ONLY配置文件资源。关于我目前的情况,我的问题是:“唯一的”关于什么?当前用户帐户或整个网站的“唯一”配置文件?如果是这样,我怎么能拥有其他用户的当前用户视图配置文件 - 我应该使用这样的URL参数:
http://www.example.com/profile?id=x, where x is the other user's account id
http://www.example.com/profile?id=x,其中x是其他用户的帐户ID
If I go the plural route, does index showing "all profiles" intend to mean all for the current user or for everyone across the site? Or is this up to my own interpretation?
如果我选择复数路线,那么显示“所有配置文件”的索引是否意味着对当前用户或网站上的每个人都意味着什么?或者这取决于我自己的解释?
I would appreciate any help I can get with this, as I feel like I am starting to understand REST and RoR conceptually but am just trying to put it into practice. Thanks!
我很感激我能得到的任何帮助,因为我觉得我开始从概念上理解REST和RoR,但我只是想把它付诸实践。谢谢!
1 个解决方案
#1
1
I would personally keep Account and profile as two separate resources or at the lest 2 separate controllers accessing different parts of the same model. This allows easy routes like http://www.example.com/profiles/2 for viewing other users and http://www.example.com/accounts/2 for managing your own account without having to add custom routes.
我个人会将Account和profile保持为两个独立的资源,或者至少2个独立的控制器访问同一模型的不同部分。这样可以通过http://www.example.com/profiles/2轻松查看其他用户,使用http://www.example.com/accounts/2管理您自己的帐户,而无需添加自定义路径。
If you have the possibility of adding more profiles to an account, then this is a form of future proofing too.
如果您可以向帐户添加更多配置文件,那么这也是未来验证的一种形式。
#1
1
I would personally keep Account and profile as two separate resources or at the lest 2 separate controllers accessing different parts of the same model. This allows easy routes like http://www.example.com/profiles/2 for viewing other users and http://www.example.com/accounts/2 for managing your own account without having to add custom routes.
我个人会将Account和profile保持为两个独立的资源,或者至少2个独立的控制器访问同一模型的不同部分。这样可以通过http://www.example.com/profiles/2轻松查看其他用户,使用http://www.example.com/accounts/2管理您自己的帐户,而无需添加自定义路径。
If you have the possibility of adding more profiles to an account, then this is a form of future proofing too.
如果您可以向帐户添加更多配置文件,那么这也是未来验证的一种形式。