Validate required for put_assoc

Viewed 1369

In my phoenix project, I have a posts and tags schema linked through a join table

schema "posts" do
    field :title, :string
    field :body, :string

    many_to_many :tags, App.Tag, join_through: App.PostsTags , on_replace: :delete
    timestamps()   
end?

How do i ensure that tags are present went using put_assoc?

def changeset(struct, params \\ %{}) do
    struct
    |> cast(params, [:title, :body])
    |> put_assoc(:tags, parse_tags(params), required: true)
    |> validate_required([:title, :body, :tags])
end

Both required: true on put_assoc and adding :tags to validate required do not work as I had imagined.

1 Answers
Related