Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ActiveStorage::Blob.unattached scope #31970

Merged

Conversation

fatkodima
Copy link
Member

One blob can be attached to multiple records
(with has_(one/many)_attached :thingy, dependent: false) so it is needed to manually do garbage collecting of orphaned blobs when
all related records are destroyed. Also there is a possibility of orphaned records, when for example, user
abandones to submit a form with attachments.

So it seems useful and convenient for me to have unattached scope on
Blob to simplify said manual garbage collecting. So, GC of orphaned records within some rake task can be done:

ActiveStorage::Blob.unattached.find_each(&:purge_later)

r? @georgeclaghorn @dhh

@@ -27,6 +27,8 @@ class ActiveStorage::Blob < ActiveRecord::Base

has_many :attachments

scope :unattached, -> { left_joins(:attachments).where(ActiveStorage::Attachment.table_name => { blob_id: nil }) }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we need an index in blob_id now? The index we have has :record_type, :record_id and :name before the blob_id so the left-most rule will not apply anymore.

Copy link
Member Author

@fatkodima fatkodima Feb 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh cool! I always forget that

@rafaelfranca rafaelfranca merged commit b9ed1fa into rails:master Feb 13, 2018
rafaelfranca added a commit that referenced this pull request Feb 13, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants