New to Elixir…this is my first project.
I have a Event ecto model (events) that has many_to_many users, so a events_users table.
This is what I have attempted to far
def create(conn, %{"event" => event_params}) do
changeset = Event.create_changeset(%Event{owner_id: LivePaper.Authentication.current_user(conn).id}, event_params)
case Repo.insert(changeset) do
{:ok, event} ->
Ecto.Changeset.cast_assoc(event, :users, [LivePaper.Authentication.current_user(conn)])
conn
|> put_flash(:info, "Event created")
|> redirect(to: events_path(conn, :show, event.id))
{:error} ->
render conn, "new.html", changeset: changeset
end
end
With this model
defmodule LivePaper.Event do
use LivePaper.Web, :model
schema "events" do
field :name, :string
field :link, :string
field :owner_id, :integer
many_to_many :users, LivePaper.User, join_through: "events_users"
timestamps()
end
@required_fields ~w(name)
@optional_fields ~w()
@doc """
Builds a changeset based on the `struct` and `params`.
"""
def changeset(model, params \\ %{}) do
model
|> cast(params, @required_fields, @optional_fields)
end
def create_changeset(model, params \\ %{}) do
model
|> cast(params, @required_fields, @optional_fields)
|> put_change(:link, create_link(params["name"]))
|> unique_constraint(:link)
end
defp create_link(name) do
name
end
end
I know I am probably doing a lot of other things wrong, if you want to chime in, that’s fine. But, the event gets created and then I tried to add the user to that event and get this error no function clause matching in Ecto.Changeset.cast_relation/4
The event gets created just fine, but that line Ecto.Changeset.cast_assoc(event, :users, [LivePaper.Authentication.current_user(conn)])
is failing. Maybe that’s not the right way to do it, I don’t know. Ive spent hours googling trying to figure it out. Any help or assistance is appreciated.