Tomas Valent


10 Experience
0 Lessons Completed
0 Questions Solved


Add an Array/JSON Array type column to the model which declares the has_many_attached and store ids of ActiveStorage::Attachments in it. Then you can do :

# app/models/entry.rb
class Entry < ApplicationRecord
  has_many_attached :pictures

  def ordered_pictures
    pictures.sort_by{ |pic| ordered_picture_ids.index( || (*100) }

  def ordered_picture_ids=(ids)
    super( # convert any ids passed to this method to integer
                           # this is just for security reasons,
                           # you don't need to do this for the feature to work

more detailed example at:

Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more.

© 2024 GoRails, LLC. All rights reserved.