defmodule Ami.Image do import Ecto.Query use Ecto.Schema alias Ami.{ Repo } schema "images" do field(:image, :string) field(:title, :string) field(:imageable_id, :integer) field(:imageable_type, :string) end def preload_image(struct, imageable_type) do struct |> Repo.preload(logo: from(i in __MODULE__, where: i.imageable_type == ^imageable_type)) end def image_url({id, image}) do case image do nil -> "" _ -> "https://ami-aws-s3.s3.amazonaws.com/uploads/image/image/#{id}/#{image}" end end end