I'm trying to create a many to many relationship using the current user assignment in the connection. The problem is I don't have access to conn in the model. I've tried passing it with the changeset, but that didn't seem like the right way to do it.
What's the best way to handle this? I imagine I can import conn into models. Is there any reason I wouldn't want to do that?
Here is what my changeset looks like right now
def changeset(model, user, params \\ :invalid) do
model
|> cast(params, @required_fields)
|> put_assoc(:users, [user])
|> validate_required([:name])
|> unique_constraint(:name)
end
UPDATE
Ended up going with Steve's suggestion. Added a virtual field for the user id to the schema and this function to the changeset pipeline
def changeset(model, params \\ :invalid) do
model
|> cast(params, @required_fields)
|> associate_current_user(params)
|> validate_required([:name])
|> unique_constraint(:name)
end
def associate_current_user(model, params) do
case params do
%{"user_id" => id} ->
user = Repo.get!(User, id)
model
|> put_assoc(:users, [user])
_ ->
model
end
end