Where should preload be done


I’m using Phoenix 1.3 with an umbrella app for the domain and another umbrella app web.

My get_, list_ function preload a lot of things(3, 4 ,5 tables) and it seems not right.

Where do you preload tables ? In context specific functions (get_top_with_song(id), get_top_with_song_and_suggestor(id)) ?
In the controller ? The view ?



I do it in context specific functions in almost the same manner you wrote it :slight_smile:

It seems strange for me to create functions following my web pages. If I need data from a preload in a specific page, maybe to write a new function.

I was thinking of creating “atomic” function preloading data. And calling them in my controller :confused:

If you need to preload different fields on your pages you could pass the fields to preload as options to your context function. Then you can keep the knowledge about which fields to preload in your controller, but your context can still handle some sane defaults to preload and you won’t need to create a multitude of functions.


Something like that ?

def get_song!(id), do: Repo.get!(Song, id)
def get_song!(id, preload_options), do: get_song!(id) |> Repo.preload(preload_options)