brendon / acts_as_list

An ActiveRecord plugin for managing lists.
http://brendon.github.io/acts_as_list/
MIT License
2.04k stars 355 forks source link

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

Closed benjaminb10 closed 1 year ago

benjaminb10 commented 1 year ago

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?

brendon commented 1 year ago

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

benoror commented 4 months ago

Thoughts on these solutions?:

brendon commented 4 months ago

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 :)