attachinary
attachinary copied to clipboard
Image Positioning
If I'm using ruby has_attachments :photos, maximum: 10
Is it possible to assign a position to these photos after they are uploaded?
I would like to be able to manage the order the photos are displayed when calling:
<% @user.photos.each do |photo| %>
<%= cl_image_tag(photo.path, { size: '125x125', crop: :fit }) %>
<% end %>
Use a singular model. A User
has_many :photos
and a Photo
has_attachment :file
. Then you can add whatever metadata on Photo
you want.
Just make sure you do efficient database calls so you aren't doing N+1 and you should be fine.
fyi our fork at https://github.com/reverbdev/ directly supports position
In this commit we made it a permanent part of the plugin. We believe photo ordering is a fundamental part of a photo plugin and don't want to have an additional join for something that can be done in a column:
https://github.com/reverbdev/attachinary/commit/72b44b68aa8770c80d815a4e29edb60531bf4522
@maletor How do I prevent N+1s?
I tried Posting.includes(:user, :photos)
, but get the following error:
Association named 'photos' was not found on Posting; perhaps you misspelled it?
Is the association named something else under the hood?
More:
>> Posting.first.photos
=> #<ActiveRecord::Associations::CollectionProxy []>
>> Posting.includes(:photos).first.photos
!! #<ActiveRecord::AssociationNotFoundError: Association named 'photos' was not found on Posting; perhaps you misspelled it?>