Some suggestions

Use a type for the relationship and use QueryProxy chaining to make code a bit cleaner

Don't have time to test it at the moment, but this should work and should be a bit cleaner