1
votes

I'm trying to create a join table, and I'm running into this error while trying to build a struct.

ERROR

protocol Enumerable not implemented for %Statcasters.UsersLeagues{meta: #Ecto.Schema.Metadata<:built, "users_leagues">, commissioner: nil, id: nil, inserted_at: nil, league: #Ecto.Association.NotLoaded, league_id: nil, updated_at: nil, user: #Ecto.Association.NotLoaded, user_id: nil}. This protocol is implemented for: DBConnection.PrepareStream, DBConnection.Stream, Date.Range, Ecto.Adapters.SQL.Stream, File.Stream, Function, GenEvent.Stream, HashDict, HashSet, IO.Stream, List, Map, MapSet, Postgrex.Stream, Range, Stream, Timex.Interval

It's failing on this line:

LEAGUE CONTROLLER:

  def new(conn, _params) do
    changeset = League.changeset(%League{users_leagues: %UsersLeagues{}})

    render(conn, "new.html", changeset: changeset)
  end

I have a has_many through relationship with leagues and users. The join table is the users_leages table and that is what I'm trying to create in the new action. But when I try to load the new page. it breaks with this error.

1
Try changing %UsersLeagues{} to [%UsersLeagues{}]. - Dogbert
That worked! But I don't understand why? - Bitwise

1 Answers

4
votes

The error means that Ecto tried to use the value of the field users_leagues as an Enumerable which %UsersLeagues{} isn't. Since the field is a has_many field, a record can have many of them and it needs to be a list (or more specifically an Enumerable) of %UsersLeagues{} structs.

You can fix this by wrapping %UsersLeagues{} in a list:

changeset = League.changeset(%League{users_leagues: [%UsersLeagues{}]})