【发布时间】:2015-11-17 12:09:03
【问题描述】:
我有下面的 Ecto 型号。渲染时,我希望 JSON 将属性名称“layers”替换为“tilemap_layers”。
我的应用程序中有许多类型的“层”,所以当我创建我的数据库模式时,我需要创建一个唯一命名的模式。但是,此 JSON 将由第三方客户端使用,必须将其命名为“层”。
推荐的方法是什么?
模型在这里:
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
【问题讨论】:
标签: json elixir phoenix-framework ecto