If you already have access to
my_model, then I’d probably check for the existence of the foreign key first, i.e.,
my_model.model1_id before I actually try to query for each model—that could save you 3 database calls
Otherwise, you could try left outer joins (using Ecto.Query.join/5) on Model1, Model2, Model3 and Model4, add a “where model_1.id IS NOT NULL” type clause and then add a LIMIT 1. That’s very likely overkill, and might not even be the most performant option, as left outer joins aren’t always the fastest route. You could build structs from your query (using Ecto.Query.API.struct/2) and then something like your
get_current_fr_model might be a reasonable solution. All of that would make it doable in a single database call.
As far as the logic of get_current_fr_model goes, I think I personally would prefer throwing the models into a list and using Enum.find/3 over a bunch of