Get only associated records in many_to_many

I have a roles and permissions set up. A user has many roles and each role has many permissions. I need to get all the user permissions. I can do this with joins, but I’m curious to know if its possible to get them using preload and assoc. E.g to get all the user associated roles, all I have to do is Ecto.assoc(user, :roles) which returns all his roles. How do I get his permissions without using joins?

Solved :sneezing_face: Ecto.assoc(user, [:roles, :permissions])

1 Like