2016-06-13 71 views
1

我有一個鳳凰應用程序,我有民意調查,有很多候選人,有很多票。如何在鳳凰框架中嵌套預加載模型

在我的控制器

我加載投票是這樣的:

poll = Repo.get_by!(Poll, url: String.downcase(url)) 
    |> Repo.preload([:candidates]) 
# and set it for the view like this 
render(conn, "poll.html", poll: poll) 

在視圖中的考生在那裏。問題是,屬於特定候選者的投票不會在視圖中預加載。

我該如何做到這一點?嵌套預裝的語法是什麼?

回答

3

要做到嵌套的預加載,你可以通過嵌套嵌套列表作爲參數是這樣的:

poll = Repo.get_by!(Poll, url: String.downcase(url)) 
    |> Repo.preload([candidates: [:votes]]) 
+0

謝謝,就像風情萬種!請問你在哪裏找到這些信息? – RedRoosterMobile

+2

@RedRoosterMobile搜索此頁上的「嵌套關聯」:https://hexdocs.pm/ecto/Ecto.Query.html#preload/3 – Dogbert