elixir - Poison.Encoder 如何预加载关联?

标签 elixir phoenix-framework ecto

我有下面的 Ecto 模型。当我尝试渲染时出现错误。如何修改 @derive 以使其预加载?还是我必须写出实现?处理这个问题的推荐方法是什么?

** (RuntimeError) cannot encode association :tilemap_layers from MyProject.Tilemap to JSON because the association was not loaded. Please make sure you have preloaded the association or remove it from the data to be encoded

模型在这里:

defmodule MyProject.Tilemap do
  use MyProject.Web, :model

  @derive {Poison.Encoder, only: [
    :name,
    :tile_width,
    :tile_height,
    :width,
    :height,
    :orientation,
    :tilemap_layers,
    :tilesets
  ]}

  schema "tilemaps" do

    field :name, :string
    field :tile_width, :integer
    field :tile_height, :integer
    field :width, :integer
    field :height, :integer
    field :orientation, :string

    has_many :tilemap_layers, MyProject.TilemapLayer
    has_many :tilesets, MyProject.Tileset

    timestamps
  end

  @required_fields ~w(tile_width tile_height width height)
  @optional_fields ~w()

  @doc """
  Creates a changeset based on the `model` and `params`.

  If no params are provided, an invalid changeset is returned
  with no validation performed.
  """
  def changeset(model, params \\ :empty) do
    model
    |> cast(params, @required_fields, @optional_fields)
  end
end

最佳答案

简短的回答是你不应该这样做。预加载数据不是 View 层的责任。

您应该在获取资源(通常是 Controller 或从 Controller 调用的函数)时执行预加载。

例如使用 Ecto.Repo.preload/3 :

def index(_conn, _params)
  timemaps = Tilemap |> Repo.all() |> Repo.preload(:timemap_layers)
  render("index.json", tilemaps: tilemaps)
end

您还可以使用 Ecto.Query.preload/3 在查询中执行预加载:

query = from t in Tilemap,
  preload: [:tilemap_layers]
Repo.all(query)

关于elixir - Poison.Encoder 如何预加载关联?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33756142/

相关文章:

postgresql - 在 Phoenix 中使用 PostgreSQL array_to_string

phoenix-framework - 脚手架应用程序时的多对多关系

elixir - 管道图正在尝试调用原始函数

elixir - 如果第一个条件失败,如何为变量分配默认值?

elixir - 在不同的端口上运行 iex mix phoenix.server 不起作用

elixir - 将 phoenix 框架中日期选择器的默认值设置为当前日期

elixir - 仅当 MIX_ENV 最初未设置为 "test"时才加载标记为测试的混合依赖项

erlang - Elixir 中为什么有两种函数?

elixir - 如何通过终端将记录添加到 Ecto 存储库? (即)

migration - 外迁移 : index on a renamed table