Rails 中的自引用集合模型
我用 Rails 2.3.5 编写了一个在线服装店的应用程序。我想在客户查看“产品详细信息”页面时显示相关产品。
例如,如果客户查看一套西装的详细信息页面,我想显示与该服装相匹配的配饰产品,例如背心、鞋子和腰带。我将相关产品命名为 Ensemble。然而,背心、鞋子和腰带也是产品,这让我很挣扎。
我让它按如下方式工作,但我知道这不是 Rails 的方式。我有一个包含所有产品的产品表。这里并不重要,但我还有一个 ProductDetails 表。我有一个 Ensembles 表,其中包含以下列:
- Product_id - 主要或原始产品,显示在详细信息页面上的
- outfit_id - 相关或配件产品
在设置数据时,在产品列表中,对于每个产品,我有一个合奏链接。此链接将带您进入 Ensembles 控制器中的索引操作。
使用“主”产品中的 id,我可以按 Product_id 查找所有关联的 Ensemble 行,或者创建一个新的集合并将主产品中的 id 指定为 Product_id。我希望能够执行 @product.lated_products 来获取 Ensemble 集合。
另外,在索引页面上,我列出了主要产品的列,以便用户可以确定他们的主要产品是他们从列表中选择的产品。我还有其他产品的选择列表,其中包含“添加到整体”操作。
最后,在同一索引页面上,我有一个表,显示集合中已有的产品,并且在该列表中,每一行都有一个销毁链接,用于从集合中删除特定产品。如果给定单个 Ensemble 行 @ensemble 我可以执行 @ensemble.product 来获取与 ensemble 行的outfit_id 相关的产品,那就太好了。
我已经让它在没有关联的情况下工作,但我必须在控制器中运行查询来构建我自己的 @product、@ensemble 和 @ensembles 集合。另外,我发现销毁集成行的唯一方法是通过 Ensemble.connection.delete(sql to delete),简单的 @ensemble.destroy 不起作用。
任何人都知道我将如何设置关联或有一个解释类似设置的网站的链接。我发现的示例都没有使用同一个表。他们有 A 通过 C 与 B 相关。我希望 A 通过 B 与其他 A 相关。
I have written an application for an online clothing store in Rails 2.3.5. I want to show related Products when a customer views the Product Detail page.
For example, if the customer views the detail page for a suit, I'd like to display the accessory products that match the dress such as a vest, shoes, and belt. I have named the related products an Ensemble. However, the vest, shoes, and belts are also Products which is what has me struggling.
I have it working as follows but I know it's not the Rails way. I have a Products table for all of the products. Not important here but I also have a ProductDetails table. I have an Ensembles table that has the following columns:
- product_id - the main or origination product, the one displayed on the detail page
- outfit_id - the related or accessory product
In setting up the data, on the Products list, for each Product I have an Ensemble link. This link takes you to the index action in the Ensembles controller.
Using the id from the "main" Product, I find all of the associated Ensemble rows by product_id or I create a new ensemble and assign the id from the main product as the product_id. I'd like to just be able to do @product.related_products to get an Ensemble collection.
Also on the index page I list the columns of the main product so the user can be sure their main product was the one they selected from the list. I also have a select list of the other products, with an Add to Ensemble action.
Finally on the same index page, I have a table that displays the products that are already in the ensemble and in that list each row has a destroy link to remove a particular product from the ensemble. It would be nice if given a single Ensemble row @ensemble I could do @ensemble.product to get the Product related to the outfit_id of the ensemble row.
I've got it working without associations but I have to run queries in the controller to build my own @product, @ensemble, and @ensembles collections. Also the only way I found to destroy an ensemble row is by Ensemble.connection.delete(sql to delete), simple @ensemble.destroy doesn't work.
Anyone know how I would set up the associations or have a link to a site explaining a similar setup. None of the examples I found use the same table. They have A related to B through C. I want A related to other A through B.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
按如下方式设置您的关联:
现在您可以执行以下操作:
Set your associations as follows:
Now you can do the following: