Given a model with ActiveStorage
class User
has_one_attached :avatar
end
I can check whether a single user has an avatar
@user.avatar.attached?
But how can I return a collection of all users with (or all users without) an attachment?
I tried using joins
to return all Users with an attachment, but this does not seem to work on either the blob or attachment table, or perhaps I'm not getting the syntax correct.
I'm sure I am overlooking something obvious. Is it possible to do something along the lines of:
User.where(attached_avatar: nil)
And if so, where is this documented?
User.left_joins(:avatar_attachment).where('active_storage_attachments.id is NULL')
– Horgan