How to autoload a relationship?
The setup:
lib/app/shop/resources/product.ex
I want to query for the product "Banana":
Question: How can I autoload the
category
when I query for a product
so that I don't have to do two SQL requests? App.Shop.Product.by_name("Banana")
should get me the product
and the category
.3 Replies
You can create a read function with a preparation that always adds the load
Or you can do it when you call the interface:
You can’t do it with only one sql query at the moment though
At some point we will add that optimization to Ash to use joins for single cardinality relationships, but it won’t be something you can turn on or off.
or you can do it for all reads by using a preparations block in your resource
many ways to skin cats in Ash 🙂