[英]rails when/how to use nested resources
I have a rails app. 我有一个Rails应用程序。 Users can create products that will be listed on products index page (including some data about the user who posted it) and everybody can see the list on
app/products.html
. 用户可以创建将在产品索引页面上列出的产品(包括有关发布该产品的用户的一些数据),每个人都可以在
app/products.html
上看到该列表。
What is the best way to implement this? 实现此目的的最佳方法是什么? Should I do it with nested resources (user has many products) in which case I can use
product.user.name
for displaying the user name or should I create an independent class so when user creates a product, some user attributes (name, etc.) will get saved in the product table. 我应该使用嵌套资源(用户拥有许多产品)来执行此操作,在这种情况下,我可以使用
product.user.name
来显示用户名,还是应该创建一个独立的类,以便在用户创建产品时显示一些用户属性(名称等) 。)将保存在产品表中。
Your mixing together quite a few different concepts here. 您在这里混合了很多不同的概念。
In REST you have a concept of nested resources which is expressed though URIs such as: 在REST中,您有一个嵌套资源的概念,它通过URI来表示,例如:
posts/:post_id/comments # comment that belong to a resource.
Which tells us that there is a "has many" relation between post
and comments
. 这就告诉我们
post
和comments
之间存在“很多”的关系。
posts/:post_id/comments/:comment_id/replies
for example should be comments/:comment_id/replies
. posts/:post_id/comments/:comment_id/replies
应该是comments/:comment_id/replies
。 Domain modeling on the other hand is how your models fit together. 另一方面,领域建模是您的模型如何组合在一起。 In ActiveRecord each model class is backed by a database table.
在ActiveRecord中,每个模型类都有一个数据库表支持。
Each model should correspond to a single type of object in your problem domain. 每个模型应对应于您的问题域中的一种对象。 So in your case you would have a User class and Products class.
因此,在您的情况下,您将具有User类和Products类。
They would be linked by a products.user_id
column. 他们将通过一个链接
products.user_id
列。 So no - you should not store users attributes in the products table. 因此,不-您不应该将用户属性存储在产品表中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.