Hi,
I have article schema
schema "articles" do
field :title, :string
field :url, :string
belongs_to :media, Media, on_replace: :nilify
timestamps()
end
And I want to add association to media that already exists and its loaded with API so In my changeset I use:
def changeset(%Article{} = article, attrs) do
article
|> cast(attrs, [:title, :url])
|> unique_constraint(:url)
|> validate_required([:title, :url])
|> put_assoc(:media, attrs["media"])
end
attrs["media"]
is map with id and all other fields but It creates a new association it dosnt associate it with existing one.
I got it to work with |> put_assoc(:media Gaby.Library.get_media!(attrs["media"]["id"]))
But I dont want to load something from database if I already have ID …