简体   繁体   English

向 Ruby on Rails 应用程序添加类别

[英]Adding categories to a Ruby on Rails application

I've had some issues with this before when creating applications and I think I'm starting to run into it again, hence I'm asking this on StackOverflow to save me a lot of time.我之前在创建应用程序时遇到过一些问题,我想我又开始遇到它了,因此我在 StackOverflow 上问这个问题来节省我很多时间。 I've spent the last few weeks setting up a perfected product model for my system.在过去的几周里,我已经为我的系统设置了一个完善的产品 model。 The model performs exactly as I want it to and has several complex features (such as search via sunspot). model 的性能完全符合我的要求,并且具有几个复杂的功能(例如通过太阳黑子进行搜索)。 I wanted to setup the category to product structure before I started this heavy development - however struggling with this kind of thing was just putting me off creating the application so I got straight into the product structure.在我开始这个繁重的开发之前,我想将类别设置为产品结构 - 但是在这种事情上挣扎只会让我无法创建应用程序,所以我直接进入了产品结构。 Now I've got the product model setup - what would be the easiest way to add a category ownership to encompass the products?现在我已经安装了产品 model - 添加类别所有权以包含产品的最简单方法是什么? (All products have a category_id column which store their father category id) My plan is to have the category index to be a list of all the categories, the category show to be a list of the products inside that category and the product show being the view of the actual product. (所有产品都有一个 category_id 列来存储它们的父类别 ID)我的计划是让类别索引成为所有类别的列表,类别显示是该类别内产品的列表,产品显示是实际产品的视图。 This would eliminate the product index and so I'll have to come up with a way to port the search feature (sunspot) from my index view to the category show somehow.这将消除产品索引,因此我必须想出一种方法,以某种方式将搜索功能(太阳黑子)从我的索引视图移植到类别显示。 As for the actual listing of the products - I assume I'll have to do some kind of partial?至于产品的实际清单 - 我想我必须做一些部分? (I don't know a lot about it). (我对此了解不多)。

Most basically, my relationships are planned to be: category:基本上,我的关系计划是: 类别:

has_many :products

product:产品:

has_one :category

My products then have a category_id column to store the ID of it's parent category.然后我的产品有一个 category_id 列来存储它的父类别的 ID。

Any tips on how to accomplish the relationships (category show to list the products etc)?有关如何完成关系的任何提示(类别显示以列出产品等)?

Best Regards,此致,

Joe

Relationships like the one you're wanting are built into ActiveRecord support . ActiveRecord support中内置了您想要的那种关系。 Understanding the model relationships in Rails is critical to doing anything in Rails that's non-trivial, so study up.了解 Rails 中的 model 关系对于在 Rails 中做任何重要的事情都至关重要,因此请继续学习。

Also, the relationship you're looking for is something like:此外,您正在寻找的关系类似于:

product: belongs_to:category产品: belongs_to:category

category: has_many:products类别: has_many:products

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM