Last active
March 11, 2021 04:07
-
-
Save Bighamster/70eae6cafb1b47f54d5d26127edd2e0d to your computer and use it in GitHub Desktop.
Using a join model to annotate Active Storage blobs with custom data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Project.first.images.attach(io: File.open(filename), filename: filename, content_type: 'image/jpg') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Image < ApplicationRecord | |
belongs_to :project | |
has_one_attached :file | |
delegate_missing_to :file | |
scope :positioned, -> { order(position: :asc) } | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<% @projects.with_attached_images.each do |project| %> | |
<%# ... %> | |
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Project < ApplicationRecord | |
has_many :images, dependent: :destroy | |
scope :with_attached_images, -> { includes(:images).merge(Image.with_attached_file) } | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<% @project.images.positioned.with_attached_file.each do |image| %> | |
<%= image_tag image %> | |
<% end %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment