简体   繁体   English

如何列出属于另一个模型的所有模型?

[英]How to list all the models that belong to another model?

I'm building a small system for a restaurant. 我正在为餐厅建立一个小型系统。

I'm currently stuck at listing efficiently all the models that belong to a product_type the types are: food , beverages , and apparel_items . 我目前停留在有效地列出属于product_type的所有模型上,这些模型的类型为: foodbeveragesapparel_items

Each product Type has multiple kinds of products that belong to them for example food has many wraps , many salads , many sandwiches , etc. 每种产品类型都有属于它们的多种产品,例如food有很多wraps ,很多salads ,很多sandwiches等。

The point of this is to have a button for each of the Product Types which when clicked, depending on the id of the type selected, lists all the models that belong to it as buttons so in turn you can click any of them and then list the database records for the model in question. 这样做的目的是为每个产品类型都有一个按钮,在单击该按钮时,根据所选类型的ID,会将所有属于它的模型作为按钮列出,因此您可以依次单击其中任何一个,然后列出该模型的数据库记录。

Example: Food, Beverages => click food => shows buttons for: wraps, salads and sandwiches => click wraps => Lists all the wraps 示例:食物,饮料=>单击食物=>显示用于包装,沙拉和三明治的按钮=>单击包装=>列出所有包装

class ProductType < ActiveRecord::Base
  has_many :apparel_items
  has_many :chapata_items
  has_many :combos
  has_many :croissants
  has_many :fruits
  has_many :green_challenges
  has_many :hot_drinks
  has_many :infusions
  has_many :juices
  has_many :molletes
  has_many :omelettes
  has_many :salads
  has_many :sandwiches
  has_many :shakes
  has_many :sincronizadas
  has_many :slices
  has_many :smooth_drinks
  has_many :super_drinks
  has_many :water_drinks
  has_many :wrap_items
end

How can I list all the models that belong to another one so that I can program this efficiently? 如何列出属于另一个模型的所有模型,以便可以高效地进行编程?

Would it be better to create a model for each of the Product Types (Food, Beverages, Apparel Items) and make only those three belong to the Product Type model? 为每种产品类型(食品,饮料,服装项目)创建一个模型并仅使这三个属于产品类型模型会更好吗?

You can use ActiveRecord::Reflection to inspect the associations for any model. 您可以使用ActiveRecord :: Reflection检查任何模型的关联。

So Food.reflect_on_all_associations(:has_many) returns an array of ActiveRecord::Reflection::HasManyReflection objects, with one for each food type that your Food model has_many of. 所以Food.reflect_on_all_associations(:has_many)返回数组ActiveRecord::Reflection::HasManyReflection对象,一个关于各种食物,你的Food模型has_many的。 Each of these reflections has a name, which is the name you set when you declare the has_many relationship. 每个反射都有一个名称,这是在声明has_many关系时设置的名称。

You can then use the names of these reflections to build the list of products like so: 然后,您可以使用这些反射的名称来构建产品列表,如下所示:

food_product_types = Food.reflect_on_all_associations(:has_many).map(&:name)

# 'food_product_types' equals [:wraps, :salads, :sandwiches, etc...]
# Now, create buttons for each food product type

Of course, if your product types have associations that are not products, you will need to filter them out. 当然,如果您的产品类型具有不是产品的关联,则需要将它们过滤掉。

Your schema could certainly benefit from some denormalizing. 您的架构当然可以从一些非规范化中受益。 There are many ways to approach the design, including suggested by you separation of models for product groups. 有许多方法可以进行设计,包括由您建议为产品组分离模型。

But, more importantly, you should consider why are you trying to put all the various products into separate models. 但是,更重要的是,您应该考虑为什么要将所有各种产品放入单独的模型中。 Are they really that different from the standpoint of the workflow that uses them? 从使用它们的工作流程的角度来看,它们真的有很大不同吗?

Maybe it worth it to just keep the truly shared set of fields in the Product model and put all the additional attributes into Attribute model? 仅仅在Product模型中保留真正共享的字段集并将所有其他属性放入Attribute模型中,是否值得? The product_type then will become just a field of Product and you will be able to do lots of wonderful stuff in a straightforward and performant manner. 然后product_type将成为Product一个领域,您将能够以直接且高效的方式完成许多精彩的工作。

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

相关问题 在 rails 中列出属于另一个模型的所有模型 - List all models belonging to another model in rails 尝试列出所有属于此项目模型的用户 - Trying to list all users belong to this project model Rails —如何设置可以属于3种不同模型之一的模型 - Rails — How to setup model that can belong to either of 3 different models 如何访问所有属于三个模型嵌套在另一个模型下的属于2个模型的模型? - How to access a model that belongs_to 2 models with all 3 models nested under another model? 如何在显示页面列出属于另一条记录的所有记录? - How can I list all records which belong to another record at the show page? Rails:如何使一个模型“属于”另一个模型? - Rails: How to get one model to “belong” to another model? 包含模型,必须属于其他2个模型 - model include, must belong to 2 other models 属于同一模型的2个模型之间的ActiveRecord关联 - ActiveRecord associations between 2 models that belong to the same model 如何通过Rails中的另一个模型列出与模型相关的所有对象 - how to list all objects releated to a model via another model in rails Model 属于另一个中的几个属性 model - Model to belong to several attribute in another model
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM