嵌入式模型的ecto警告 [英] Ecto warning with embedded model

查看:76
本文介绍了嵌入式模型的ecto警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

施放时收到此警告

警告:不推荐使用cast/4的铸造嵌入,请使用 替换为cast_embed/3

warning: casting embeds with cast/4 is deprecated, please use cast_embed/3 instead

我有组织模型

defmodule Bonsai.Organization do
  use Bonsai.Web, :model
  alias Bonsai.OrganizationSettings

  schema "organizations" do
    field :name, :string
    field :currency, :string
    field :tenant, :string
    field :info, :map, default: %{}
    embeds_one :settings, OrganizationSettings, on_replace: :delete

    timestamps
  end

  @required_fields ~w(name currency tenant)
  @optional_fields ~w(info settings)

  @doc """
  """
  def changeset(model, params \\ %{}) do
    cast(model, params, @required_fields, @optional_fields)
    |> cast_embed(:settings)
    |> put_embed(:settings, OrganizationSettings.changeset(%OrganizationSettings{}, params[:settings] || %{}))
    |> change(%{info: params[:info] || %{}})
  end

end

还有我的嵌入式模型OrganizationSettings

And my embedded model OrganizationSettings

defmodule Bonsai.OrganizationSettings do
  use Ecto.Model
  #use Ecto.Changeset

  @primary_key {:id, :binary_id, autogenerate: true}
  #schema "" do
  embedded_schema do
    field :show_search_on_focus, :boolean, default: true
    field :theme, :string, default: "bonsai"
  end

  def changeset(model, params \\ %{}) do
    model
    |> cast(params, [:theme], [:show_search_on_focus])
    |> validate_inclusion(:theme, ["bonsai", "dark"])
  end

end

我尝试了很多方法,但是我做错了,请帮忙

I have tried many ways but I'm doing something wrong please help

推荐答案

请参见

See https://github.com/elixir-ecto/ecto/blob/cc92f05cb2f24c3206db9017e6c28ecf77ff100d/CHANGELOG.md - Revamped changesets. You're using deprecated cast/4 here:

cast(模型,参数,@ required_fields,@ optional_fields) 演员(模型,参数,[:主题],[:show_search_on_focus])

cast(model, params, @required_fields, @optional_fields) cast(model, params, [:theme], [:show_search_on_focus])

相反,请使用示例中显示的cast/3和validate_required/3.

Instead, use cast/3 and validate_required/3 as presented there in the example.

这篇关于嵌入式模型的ecto警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆