Comments (6)
@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.
Same thing 😢
from arc.
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.
Thanks, I need to use 4-5 images per user. Demo would be great !!!
from arc.
@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.
@Morganjackson Were you ever able to get it working with file_input ... multiple: true
?
from arc.
Related Issues (20)
- Warnings during compilation on ELixir 1.8.0-otp-21.2.2
- Expires header is N/A when uploading to S3. HOT 1
- Arc: Uploading Protected Files HOT 3
- Test configuration, how to prevent HTTP requests?
- `arc` crashes when trying to delete a file with a `:skip`ped transform HOT 1
- Arc sometimes does not save files
- Validation when uploading from a remote URL
- Attachment not uploaded to S3 on create, only on update HOT 3
- Converting PDF to PNG - resulting images stuck in temporary file (multi-page pdf) HOT 4
- url/1 returns non existing files HOT 2
- Seperate store_filename from url_filename in definition HOT 2
- minio storage support HOT 1
- Object deletion by scope HOT 2
- MyApp.Avatar.url({filename, scope}) returns path with leading slash
- Change tmp %Arc.File{} path.
- Versions on the fly
- Does it work with Digital Ocean storage instead of AWS? HOT 1
- Local upload - (UndefinedFunctionError) function nil.id/0 is undefined HOT 2
- Getting Timeout error - Azure container upload
- ARC.File unpack/repack /APEX ENGINE
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from arc.