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?
Trending in Questions
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
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
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
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
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
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
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
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
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
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 2- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
codeanpeace
Looking at Ecto’s Embedded Schemas docs, the container changeset takes a plain map and not the embedded struct.
which would explain your error message:
So instead of:
%Msg{} |> Msg.changeset(%{msg: "Hello!", translations: %Translations{}})Give this a try:
%Msg{} |> Msg.changeset(%{msg: "Hello!", translations: %{}})Vovchikan
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:mapwith map inside