Vovchikan

Vovchikan

I’m trying to use library trans.

This my Schema module:

defmodule Hi.Msg do
  use Ecto.Schema
  use Trans, translates: [:msg],
    default_locale: Application.compile_env(:hello, :locale)

  import Ecto.Changeset

  schema "msgs" do
    field :msg, :string

    embeds_one :translations, Translations, on_replace: :update, primary_key: false do
      embeds_one :en, Hi.Msg.Translation
      embeds_one :ru, Hi.Msg.Translation
      embeds_one :uz, Hi.Msg.Translation
    end
  end

  def changeset(msg, params \\ %{}) do
    msg
    |> cast(params, [:msg])
    |> cast_embed(:translations, with: &translations_changeset/2)
    |> validate_required([:msg])
  end

  defp translations_changeset(translations, params) do
    translations
    |> cast(params, [])
    |> cast_embed(:en)
    |> cast_embed(:ru)
    |> cast_embed(:uz)
  end

  def translate(msgid) do
    import Ecto.Query
    msg =
      Hi.Msg
      |> Repo.get_by!(msg: msgid)

    Trans.Translator.translate(msg, :msg,
      Application.get_env(:hello, :locale))
  end
end

defmodule Hi.Msg.Translation do
  use Ecto.Schema
  import Ecto.Changeset

  @primary_key false
  embedded_schema do
    field :msg, :string
  end

  def changeset(fields, params) do
    fields
    |> cast(params, [:msg])
    |> validate_required([:msg])
  end
end

This is my migration:

defmodule Msg.Repo.Migrations.CreateMsg do
  use Ecto.Migration

  def change do
    create table(:msgs) do
      add :msg, :string
      add :translations, :map
    end

    create unique_index(:msgs, :msg)
  end
end

When i’m trying to create through changeset() new %Hi.Msg{} with %Hi.Msg.Translations{} inside it, this error occurs:

iex(1)> alias Msg.Repo
iex(2)> alias Hi.Msg
iex(3)> alias Hi.Msg.{Translation, Translations}

iex(6)> %Msg{} |> Msg.changeset(%{msg: "Hello!", translations: %Translations{}})
 
** (Ecto.CastError) expected params to be a :map, got: `%Hi.Msg.Translations{en: nil, ru: nil, uz: nil}`
    (ecto 3.9.4) lib/ecto/changeset.ex:498: Ecto.Changeset.cast/4
    (hello 0.1.0) lib/hi/msg.ex:27: Hi.Msg.translations_changeset/2
    (ecto 3.9.4) lib/ecto/changeset/relation.ex:137: Ecto.Changeset.Relation.do_cast/6
    (ecto 3.9.4) lib/ecto/changeset/relation.ex:335: Ecto.Changeset.Relation.single_change/5
    (ecto 3.9.4) lib/ecto/changeset/relation.ex:121: Ecto.Changeset.Relation.cast/5
    (ecto 3.9.4) lib/ecto/changeset.ex:832: Ecto.Changeset.cast_relation/4
    (hello 0.1.0) lib/hi/msg.ex:21: Hi.Msg.changeset/2
iex(6)> 

Without Translations it works fine:

iex(5)> %Msg{} |> Msg.changeset(%{msg: "Hello!"})
#Ecto.Changeset<
  action: nil,
  changes: %{msg: "Hello!"},
  errors: [],
  data: #Hi.Msg<>,
  valid?: true
>

Schema module was copied from hex page of library trans.

I’m never used before embedded schemas, can u help me to understand this problem?

Showing Posts 1 to 2

codeanpeace

codeanpeace

Looking at Ecto’s Embedded Schemas docs, the container changeset takes a plain map and not the embedded struct.

# where profile is an embedded schema within user
changeset = User.changeset(%User{}, %{profile: %{online: true, visibility: :public}})

which would explain your error message:

expected params to be a :map, got: %Hi.Msg.Translations{...}

So instead of: %Msg{} |> Msg.changeset(%{msg: "Hello!", translations: %Translations{}})
Give this a try: %Msg{} |> Msg.changeset(%{msg: "Hello!", translations: %{}})

Vovchikan

Vovchikan OP

Ok, I think I figured it out. In first argument of changeset() i’m using my struct with struct inside, but in second argument instead :struct, i need to pass :map with map inside

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews