brendon/acts_as_list

How can I use act_as_list with a has_many_attached association (ActiveStorage, Rails 7)

benjaminb10 opened this issue · 3 comments

Let say we have a Product model with a has_many_attached: images association.

I want to let user change the position to an image they've uploaded.

Is there a way to add act_as_list to the images relation?

Unfortunately you can't (easily) work with the Attachment model so if you want to augment your attachments you need to make an intermediate model under your control i.e. ProductImage. It will belong_to :product and has_one_attached :image. You can then acts_as_list scope: :product.

Hope that helps :D

I'm not a fan of extending a model that you don't really have future-proof control over (i.e. Rails may decide to change how the model works in unpredictable ways). Better to have an intermediate model that just has_one_attached :image so that you can have more control (as mentioned above).

I've done this in my own project many times and it works well.

Up to you :)