Hey guys im new to phoenix and Elixir. Can the “ImageUploader.Type” datatype be declared in a way such that it can store multiple values so that it? and how do you declare it in your schema ? What i have so far is:
field :image, Reports.ImageUploader.Type
def changeset(issue__logger, attrs) do
|> cast_attachments(attrs, [:image])
|> cast_attachments(attrs, [:image])
end
then in my template the
currently with that piece of code i can only cast one image at a time even if i have selected multiple images. Where am i missing it?
I’m assuming you’re using waffle, in the future you should specify what libraries your question references.
You cannot do this as far as I know, what you’d want to do instead is have a table that holds your image, and then have a many_to_many or has_many: :through relationship from your schema to that new schema. You’d then create a new image schema for each image you’ve uploaded.
Using waffle. I store many images in one jsonb field using embeds_many
In the migration you will need add :medias, {:array, :map}, default: []
Then my I have my schema article who holds many medias.
defmodule Schema.Article do
schema "articles" do
embeds_many :medias, Media
end
def changeset(article, attrs) do
article
|> cast(attrs, (other fields ...))
|> cast_embed(:medias, with: &Schema.Media.changeset/2)
end
defmodule Schema.Media do
use Waffle.Ecto.Schema
alias Uploaders.Image
@primary_key {:id, :binary_id, autogenerate: true}
embedded_schema do
field :src, Image.Type
field :alt, :string
end
def changeset(media, attrs) do
media
|> cast(attrs, ~w(alt)a)
|> cast_attachments(attrs, ~w(src)a)
end
end