Answer the question
In order to leave comments, you need to log in
How to add attachments of different data types (Audio, Video, Documents)?
How to add many attachments of different data types (Audio, Video, Documents)?
Let's say there is an Article.
I want to attach 3 videos, 5 audios, 2 pdfs and 1 picture to the Article. I want to use these attachments in other articles separately from each other.
How can this be embedded using the carrierwave gem , for example?
There is a working code, but it doesn't quite work as it should:
class Post < ActiveRecord::Base<br>
belongs_to :user<br>
has_many :attachments<br>
end<br><br>
class Attachment < ActiveRecord::Base<br>
attr_accessible :post_id, :image, :remote_image_url, :document, :remote_document_url, :video, :remote_video_url<br>
belongs_to :post<br>
belongs_to :user<br><br>
mount_uploader :image, ImageUploader<br>
mount_uploader :document, DocumentUploader<br>
mount_uploader :video, VideoUploader<br>
end<br>
Answer the question
In order to leave comments, you need to log in
Here we need to solve two problems.
class PostAttachment < ActiveRecord::Base
belongs_to :post
belongs_to :attachment
end
class Post < ActiveRecord::Base
has_many :post_attachments
has_many :attachments, through: :post_attachments
end
class Attachment < ActiveRecord::Base
...
end
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question