Sigil a word array to atoms, doesn't seem to work on Elixir 1.10

This is working code from an old codebase. I recently upgraded to version 1.10 of Elixir, and it’s no longer working, seeing this error:

defmodule MyApp.Accounts.AccountRole do
  use MyApp.Schema

  schema "account_roles" do
    belongs_to(:account, Account)
    belongs_to(:role, Role)
  end

  # No longer works. See error below
  #@required_attributes ~w(role_id, account_id)a

  # This works as expected.
  @required_attributes [:role_id, :account_id]

  def changeset(schema, params \\ %{}) do
    schema
    |> cast(params, @required_attributes)
  end

  def create_changeset(schema, params \\ %{}) do
    schema
    |> changeset(params)
    |> validate_required(@required_attributes)
    |> cast_assoc(:role)
    |> cast_assoc(:account)
  end
end

Here’s the error:

iex(13)> MyApp.Accounts.AccountRole.changeset(%MyApp.Accounts.AccountRole{}, %{account_id: "10d2bcf8-123123123"})                                                 
** (ArgumentError) unknown field `:"role_id,"` given to cast. Either the field does not exist or it is a :through association (which are read-only). The known fields are: :account, :account_id, :id, :role, :role_id
    (ecto 3.3.1) lib/ecto/changeset.ex:549: Ecto.Changeset.type!/2
    (ecto 3.3.1) lib/ecto/changeset.ex:518: Ecto.Changeset.process_param/7
    (elixir 1.10.0-rc.0) lib/enum.ex:2102: Enum."-reduce/3-lists^foldl/2-0-"/3
    (ecto 3.3.1) lib/ecto/changeset.ex:503: Ecto.Changeset.cast/6

Is this a bug or something I’m missing on my end?

1 Like

remove the comma?

@required_attributes ~w(role_id, account_id)a

this is elixir 1.8.2, not sure when/if it broke

iex(16)> ~w(role_id, account_id)a

[:"role_id,", :account_id]
iex(17)> ~w(role_id account_id)a 
[:role_id, :account_id]
3 Likes

It didn’t break, that’s the way the word splitter has always worked, it splits on whitespace, not comma’s. As comma’s are not whitespace they’ve always been included in the word.

2 Likes

Earlier there was warning, I do not know if this is still a case.