在ecto中预加载嵌套关联

时间:2016-11-07 10:29:17

标签: elixir ecto

我有4个模型,它们被关联为

表格

has_many :form_fields, FormField, on_delete: :delete_all
has_many :fields, through: [:form_fields, :field]
has_many :conditions, Condition, on_delete: :delete_all

字段

has_many :form_fields, FormField, on_delete: :delete_all
has_many :forms, through: [:form_fields, :form]
has_many :conditions, Condition, on_delete: :delete_all

FormField

belongs_to :form, Form
belongs_to :field, Field

条件

belongs_to :field, Field
belongs_to :form, Form

我想编写一个查询,以获取idfieldsconditions一致的表单(Form& {{1在每个字段中

例如:要获得Field为1的表单,

id

我的问题被标记为another question的可能重复,这非常直接。但我的情况与此略有不同。请看看这两个问题并帮助我克服这一步。

1 个答案:

答案 0 :(得分:0)

对不起伙计们。以下查询有效

form_conditions =
  Condition
  |> where(form_id: ^id)
form =
  Form
  |> where(id: ^id)
  |> preload(fields: [conditions: ^form_conditions])
  |> Repo.one!