[英]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
的所有模型上,这些模型的类型为: food
, beverages
和apparel_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.