In my current project my database requires a few associations between tables that ultimately end up belonging to a record in a ‘customers’ table. I’m finding it a real struggle to maintain the series of nested pre-loads my current understanding says I need and in some cases, finding they are creating loops that can crash the server. The table structure is like this:
Customers
x Items
- provider
- type
- status
- customer
x action
x note
x Events
- source
- status
- grade
- specialty
- destination
- customer
x decisions
x items
x Decisions
x note
- customer
where -
means it belongs_to
and x
is a has_many
.
If I make a call for a customer
I have to preload all of the second level tables (Items, Events and Decisions) and all of their sub tables or I get errors about associations not being loaded. As you can see, sometimes this includes pre-loading the customer within one of those pre-load sub queries which just doesn’t feel right at all.
I can choose not to return this extra customer in the JSON by having a different template in the View but if I don’t preload it, I get an error.
This is also a problem because some of these second level tables might have relations to each other. For example, a Decision
has a has_many
relationship with Event
. This looks like it creates an even bigger pre-loading loop and causes my docker container to crash out.
I’m clearly doing something wrong and I’m not sure if it’s in my understanding of preloading (which is poor but the documentation I find very hard to understand), my underlying table strucure (which I understand would be out of the scope of this forum) or something else. It doesn’t look like you can choose NOT to preload an item so i’m a bit stuck.
Note: These are typically currently called without using join
syntax as I found it difficult to understand with so many nested preloads.