我阅读了关于如何查询具有ActiveStorage附件的记录的有用答案?
我试图查询所有用户谁至少有一个图像(容易适应相关的问题,见下文),和其第一张图像为.variable?
目前我所知道的
返回所有拥有至少一张图片的用户
User.
left_joins(:images_attachments).
group(:id).
having("COUNT(active_storage_attachments) > 0")
但我不知道如何返回只是用户的第一个图像是.variable?
(即用户的图像不会错误,当它的大小改变)
由于variable?
是在ActiveStorage::Blob::Representable
模块中定义的,您应该加载每个ActiveStorage::Blob
对象并检查该方法在调用它时返回的值。但我认为这可能是最后的选择。
知道variable?
方法所做的只是检查blobcontent_type
是否是ActiveStorage.variable_content_types
中预定义的blob之一,您可以尝试这样做,但使用SQL:
User.left_joins(:main_image_attachment)
.joins('INNER JOIN active_storage_blobs ON active_storage_blobs.id = active_storage_attachments.blob_id')
.group(:id)
.having('COUNT(active_storage_attachments.id) > 0')
.where(active_storage_blobs: { content_type: ActiveStorage.variable_content_types })
所以你做几乎相同的,但只是把active_storage_blobs
表到内存中访问content_type
列。
我试图查询所有用户谁至少有一个图像(容易适应相关的问题,见下文),谁的第一个图像是。variable?
你想要Users who have at least one image
,所以INNER JOIN
应该可以。
如果你想要whose first image
的字面意思,这可以通过join subquery
来实现。
结合@SebastianPalma的答案,我们可以这样做:
# Query for attachments with just first image of users
subquery = ActiveStorage::Attachment.where(record_type: 'User')
.order(:record_id, created_at: :asc)
.select('DISTINCT ON (record_id) *')
.to_sql
User.joins("INNER JOIN (#{subquery}) AS active_storage_attachments ON active_storage_attachments.id = users.id")
.joins('INNER JOIN active_storage_blobs ON active_storage_blobs.id = active_storage_attachments.blob_id')
.where(active_storage_blobs: { content_type: ActiveStorage.variable_content_types })