Vovchikan
(Ecto.CastError) expected params to be a :map, got: `%Hi.Msg.Translations{}`
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?
Marked As Solved
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: %{}})
Popular in Questions
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Hi,
I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
Hello, I have map which I want to convert it to string like this:
the map:
%{last_name: "tavakkoli", name: "shahryar"}
the string I ne...
New
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
Hey all,
I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
Other popular topics
What is the proper way to load a module from a file in to IEX?
In the python world, doing something like this pretty standard:
from ....
New
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
New
Manning 2016 Halloween weekend sale via Deal of the Day
Friday, October 28 - Half off all MEAPs - code WM102816LT
Saturday, October 29 ...
New
What is the idiomatic way of matching for not nil in Elixir?
E.g.,
First way:
defp halt_if_not_signed_in(conn, signed_in_account) when...
New
I have followed this StackOverflow post to install the specific version of Erlang.
And When I am running mix ecto.setup then getting fol...
New
As a follow up to my earlier question:
I have the code compiling and running but not getting a successful login from the rest server. ...
New
Hi,
I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
Hey,
Just curious what are the main benefits of Elixir compared to Clojure?
When is Elixir more useful than Clojure and vice versa?
Th...
New
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors:
[WARN] - (starship::utils): Executing command ...
New








