如何在凤凰长生不老药中深入显示参考文献

时间:2020-08-29 02:42:27

标签: elixir phoenix-framework

我想在Pheonix Elixirr的深处显示两个表的引用,似乎离实际获取仅一步之遥。

有三个相互关联的表:

主表是“客户”。客户有要求。请求具有请求类型,这是一个查找表。

在“客户模板”中想显示客户提出的请求清单。在显示模板中,存在如下循环:

  <ul>
     <%= for request <- @client.requests do %>

       <%= request.request_date %> - 
       <line in question> -
       <%= request.notes %>&nbsp;

       <%= link "Show", to: Routes.client_request_path(@conn, :show, @client, request) %>&nbsp;
       <%= link "Edit", to: Routes.client_request_path(@conn, :edit, @client, request) %>
     <% end %>
  </ul>

上面没有问题的行也可以正常工作。我想添加从下拉列表中选择的请求类型,以便从Request表中进行引用。

如果我尝试添加: <%= request.requesttype.requestname => 它不起作用。

在“请求显示”页面中 <%= @requesttype.requestname => 确实有效

在相关行中 <%= request.requesttype_id %> 确实显示正确的ID。

错误是key :requesttype not found in: #Ecto.Association.NotLoaded<association :requesttypes is not loaded>.

get_client函数如下:

def get_client!(id, tenant) do 
Client 
|> Repo.get!(id, prefix: Triplex.to_prefix(tenant)) 
|> Repo.preload([:genders, :requests]) 
end 

如何从请求表中添加关联?

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

如错误消息所提示,嵌套struct BasicInfo_UI: View { @State var username: String = "" @State var isPrivate: Bool = true @State var notificationsEnabled: Bool = false @State private var previewIndex = 0 var previewOptions = ["Always", "When Unlocked", "Never"] var body: some View { Form { Section(header: Text("PROFILE")) { TextField("Username", text: $username) Toggle(isOn: $isPrivate) { Text("Private Account") } } Section(header: Text("NOTIFICATIONS")) { Toggle(isOn: $notificationsEnabled) { Text("Enabled") } Picker(selection: $previewIndex, label: Text("Show Previews")) { ForEach(0 ..< previewOptions.count) { Text(self.previewOptions[$0]) } } } Section(header: Text("ABOUT")) { HStack { Text("Version") Spacer() Text("2.2.1") } } Section { Button(action: { print("Perform an action here...") }) { Text("Reset All Settings") } } } } } struct BasicInfo_UI_Previews: PreviewProvider { static var previews: some View { BasicInfo_UI() } } 不会被加载,因为您只是预加载了requesttypes,而不是相关联的client.requests。您需要明确说明您要预加载的内容,这实际上是一件好事,因为您可以控制要查询的内容。

使用requesttypes应该可以解决您的问题,它像one of these doc examples中那样使用关键字列表来预加载嵌套的关联。

请注意,这将引发4个单独的查询(根据您的使用情况可能很好还是不合适),如果要使用联接来避免这种情况,则可能应该编写查询(请参见this great explanation)。

我鼓励您在REPL(Repo.preload([genders: [], requests: :requesttypes]))中处理您的查询,它非常透明,您会看到:

  • 正在返回的数据(已加载或未加载的数据)
  • 调试日志中的基础查询

在Elixir学校,有一个great section也可能很有趣。

相关问题