GithubHelp home page GithubHelp logo

Comments (6)

secretfader avatar secretfader commented on August 16, 2024 1

@nav301186 In your User model, add:

schema "users" do
  has_many :images, LoginService.UserImage
end

This is a normal one-to-many relationship in Ecto. Nothing fancy. Then, in the Avatar model:

schema "user_images" do
  belongs_to :user, LoginService.User
  field :avatar, LoginService.Avatar.Type
end

Obviously, you'll need to create a table for user_images. This will allow you to attach many different UserImages to a given User, and manage the relationship just like you normally would in Ecto. If you're using Phoenix, you can add the relationship via a generator, like so: mix phoenix.gen.model UserImage user_images user_id:references:user avatar:string.

It might be possible to use embedded schemas (rather than a separate table), but I'm not sure Arc supports this method.

@stavro I assume we can close this now. I hope the above was correct and helpful.

from arc.

MikeAndrianov avatar MikeAndrianov commented on August 16, 2024 1

Same thing 😢

from arc.

stavro avatar stavro commented on August 16, 2024

In general, when you have a one-to-many (one user can have many images) type of relationship, the images should be extracted into their own model. In this way, you'll create a first class Image (or Attachment) model, which has a user_id and a file attribute. Each image record will have it's own scoped image.

And since these new records are meaningless without their saved attachment, I would also recommend using a UUID type of identifier for the file name.

Approximately how many images will you have scoped to each model? There are a few different ways of doing this, and I can possibly help put together a demo for it.

from arc.

naveen-negi avatar naveen-negi commented on August 16, 2024

Thanks, I need to use 4-5 images per user. Demo would be great !!!

from arc.

Morganjackson avatar Morganjackson commented on August 16, 2024

@nicholaswyoung Thanks for this example! I

I Was able to get a single image saving, but I cant figure out how to get it to work with <%= file_input df, :image, multiple: true %>

Do you know if I need to adjust my controller to handle multiple files?

This is my create action:

def create(conn, %{"post" => post_params}) do
  changeset = Post.changeset(%Post{}, post_params)

  case Repo.insert(changeset) do
    {:ok, _post} ->
      conn
      |> put_flash(:info, "Post created successfully.")
      |> redirect(to: admin_post_path(conn, :index))
    {:error, changeset} ->
      render(conn, "new.html", changeset: changeset)
  end
end

Which works fine without multiple: true but I get the following when attempting to upload multiple images:

Request: POST /admin/posts ** (exit) an exception was raised: ** (FunctionClauseError) no function clause matching in Arc.Actions.Store.store/2 lib/arc/actions/store.ex:8: Arc.Actions.Store.store(Ap.BlogPost, [%Plug.Upload{content_type: "image/png", filename: "demo-1920x1200.png", path: "/var/folders/3l/j0tny20n2qg0n5zd6rwx8whr0000gn/T//plug-1482/multipart-557305-982633-1"}, %Plug.Upload{content_type: "image/jpeg", filename: "photo-1461611034385-e082102d5c75.jpeg", path: "/var/folders/3l/j0tny20n2qg0n5zd6rwx8whr0000gn/T//plug-1482/multipart-557305-999697-1"}, %Plug.Upload{content_type: "image/jpeg", filename: "photo.JPG", path: "/var/folders/3l/j0tny20n2qg0n5zd6rwx8whr0000gn/T//plug-1482/multipart-557306-7062-1"}])

from arc.

sc0ttman avatar sc0ttman commented on August 16, 2024

@Morganjackson Were you ever able to get it working with file_input ... multiple: true?

from arc.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.