Using Ecto preload to load relationships in one query
Hello! Loving using Ash so far. I'm trying to make a simple Reddit clone, and I have a structure basically like this:
- Post
- belongs to an author (user)
- belongs to a community
When I display a post, I need to fetch the author and the community (so I can link to both). Currently I'm doing this by just building a
load
into my action:
However, this does not do a join, but instead does two separate queries (one for the author
, one for the community
):
How do I modify this to fetch everything in a single query? When I get into fetching nested comments in a bit, having to fetch the users and the counts in separate queries feels like it would get slow. Is there a way I can use Ecto's preload
option in Ash?1 Reply
Nothing in ash will actually fetch them in a single query. Joined queries producing multiple result sets don’t compose well with other features in Ash and also generally don’t scale as well typically.
If you really want to, you can write manual actions, or use the
modify_query
option on read actions to alter the ecto query.