I have the following schema and associations:
Restaurant has_many Review
Review belong_to Restaurant
Review belongs_to User
User has_many Review
I can easily query and retrieve all the reviews of a specific restaurant by preloading the Reviews, but I can’t seem to be able to retrieve the specific user who submitted the review in one query. I’m currently doing this in my live view:
|> assign(:restaurant, Listing.get_restaurant!(id))
|> assign(:reviews, Listing.get_reviews_by_restaurant_id(id))
Which then allows me to display the info about the restaurant first. Then the second query gets me all the reviews for that specific restaurant.
def get_reviews_by_restaurant_id(restaurant_id) do
Repo.all(
from r in Review,
where: r.restaurant_id == ^restaurant_id,
join: rt in assoc(r, :restaurant),
join: u in assoc(r, :user),
preload: [[restaurant: rt], [user: u]],
order_by: [desc: :id]
)
end
Wondering is there a more idiomatic way to retrieve the restaurant and all its associated reviews + user info in one query instead of 2 round trips to the DB?