简体   繁体   English

复杂模型关联-Rails

[英]Complex Model Associations - Rails

So I have a bit of a complex model association setup that I can't seem to get quite right. 因此,我有一些复杂的模型关联设置,似乎不太正确。

I have: 我有:

                           Owners     Doors     
    Clients -> Orders -> <    |     /
                           Property - Windows
                                    \
                                      Heating/Cooling

So the thought process is that each property can have many doors, windows, heating/cooling systems, and owners , and each owner can have many property . 因此,思考过程是, 每个财产可以拥有许多门,窗户,供暖/制冷系统和所有者 ,而每个所有者可以拥有许多财产

When an order is submitted with only the owner and property info (address, etc), we go and find how many doors, windows, heating/cooling systems the property has , update the order , and then sent it back to the client . 当提交仅包含所有者财产信息(地址等)的订单时 ,我们去查找该财产拥有多少门,窗户,供暖/制冷系统 ,更新订单 ,然后将其发送回客户

Right now I have my models setup that, (condensed here for space saving) 现在我有我的模型设置,(为了节省空间而在此处浓缩)

Clients has_many :orders

Order -> belongs_to :property, :clients
Order -> has_many :doors, :windows, :heating/cooling, through: :property

Property -> has_many :doors, :windows, :heating/cooling, :owners
Doors, Windows, Heating/Cooling, Owners -> belongs_to :property

So my problem is that I want to be able to know what doors, windows, and heating/cooling were pulled on which order. 所以我的问题是,我希望能够知道按什么顺序拉动了哪些门,窗户和暖气/制冷系统。 So I'd say that they should belong to order BUT if multiple orders come in on the same property, I don't want to have the same information for those models in my db multiple times for said property just because they come in on a different order, and likewise want to be able to look up the property itself and see all associated orders, doors, windows, heating/cooling. 因此,我想说的是,如果同一属性上有多个订单进入,则它们应属于订单BUT,我不想为我的数据库中的这些属性多次为这些模型提供相同的信息,只是因为它们进入了一个不同的顺序,同样希望能够查找物业本身,并查看所有相关的顺序,门,窗,供暖/制冷。

I hope my explanation was clear as I'm still fairly new to rails and how the associations work and their capabilities so any insight would be super helpful, thanks in advance. 我希望我的解释很清楚,因为我对Rails以及它如何工作以及它们的功能还很陌生,所以在此先感谢您的任何见解。

Seems like the quickest approach would be to add an order_ids column, serialized as an array, onto the doors/windows/heating/cooling tables. 似乎最快的方法似乎是将order_ids列(序列化为数组)添加到门/窗/加热/冷却表上。 You'd need application logic to maintain column state though. 但是,您需要应用程序逻辑来维护列状态。

Unrelated: are the doors/windows/heating/cooling models unique enough that doing something like an items table and have it be polymorphic wouldn't work? 不相关:门/窗/加热/冷却模型是否足够独特以至于不能执行像items表之类的并且具有多态性的模型?

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

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