J.Krishna
J.Krishna

Reputation: 1010

cast/3 in Elixir Ecto changesets

I have the following model. Do I have to include all field names in the cast or is there any default way to map all the fields in the params to model.

defmodule Chemical.Info do
    use Chemical.Web, :model

    schema "infos" do
        field :title, :string
        field :shortdesc, :string
        field :longdesc, :string
        field :images, :string
        field :regions, :string
        field :startdate, :date
        field :enddate, :date
        field :status, :string
        field :createdby, :string
        field :approvedby, :string

        timestamps()
    end

    def changeset(model, params \\ :empty) do
        model
        |> cast(params, ["title", "shortdesc", "longdesc"])
    end
end

Do I have to specify all the field names in the cast method to be copied into the model?

Upvotes: 1

Views: 336

Answers (1)

NoDisplayName
NoDisplayName

Reputation: 15736

I think this may work but I've never done it tbh ->

Enum.map(@ecto_fields, &(elem(&1, 0)))

Upvotes: 1

Related Questions