Trouble with cast_embed/3 error message

1,398 views
Skip to first unread message

Matt Widmann

unread,
Jan 13, 2016, 11:39:17 PM1/13/16
to elixir-ecto
I'm getting an exception I had a lot of trouble understanding what I did wrong...

** (RuntimeError) casting embeds with cast/4 is not supported, use cast_embed/3 instead
     stacktrace:
       (ecto) lib/ecto/changeset.ex:386: Ecto.Changeset.type!/2
       (ecto) lib/ecto/changeset.ex:365: Ecto.Changeset.process_param/6
       (elixir) lib/enum.ex:1151: Enum."-map_reduce/3-lists^mapfoldl/2-0-"/3
       (elixir) lib/enum.ex:1151: Enum."-map_reduce/3-lists^mapfoldl/2-0-"/3
       (ecto) lib/ecto/changeset.ex:344: Ecto.Changeset.cast/5
       (slack_coder) lib/models/user.ex:28: SlackCoder.Models.User.changeset/2
       test/models/user_test.exs:10

I had the embedded field in the required/optional fields in my cast call but didnt realize it (since it was `@required` & `@optional`). Perhaps the error message could be updated to say to remove the offending field from the required/optional fields in the cast/4 call?

Nhu

unread,
Jan 15, 2016, 12:58:39 AM1/15/16
to elixir-ecto
I'm not 100% if your problem is similar to mine without seeing the code. I fixed mine by following the example on Jose's blog post on saving associations. I removed the embedded/association field from @required_fields and @optional_fields, then in changeset, I added the cast_assoc, like this. Hopefully that will fix your problem too.


def changeset(todo_list, params \\ :empty) do
  todo_list
 
|> cast(params, @required_fields, @optional_fields)
 
|> cast_embed(:todo_items, required: true)
end

Matt Widmann

unread,
Jan 15, 2016, 4:46:48 PM1/15/16
to elixir-ecto
Yep thats exactly my problem (and I stated that under the stack trace). I was asking if anyone thought the error message should be different, perhaps mentioning that embedded fields do not belong in the `@required`/`@optional` fields. Also, mentioning which field has the problem would be helpful as well.
Reply all
Reply to author
Forward
0 new messages