简体   繁体   中英

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.

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.

Seems like the quickest approach would be to add an order_ids column, serialized as an array, onto the doors/windows/heating/cooling tables. 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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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