我在ActiveStorage上遇到问题,目前我的模型上有has_on_attached
和has_many_attached
关系
每次我调用模型时,它都会加载如下关系:
ActiveStorage::Attachment Load (4.2ms) SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = $1 AND "active_storage_attachments"."record_type" = $2 AND "active_storage_attachments"."name" = $3 LIMIT $4 [["record_id", 4934], ["record_type", "User"], ["name", "profile_picture"], ["LIMIT", 1]]
ActiveStorage::Attachment Exists (0.9ms) SELECT 1 AS one FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = $1 AND "active_storage_attachments"."record_type" = $2 AND "active_storage_attachments"."name" = $3 LIMIT $4 [["record_id", 4934], ["record_type", "User"], ["name", "home_pictures"], ["LIMIT", 1]]
如何默认情况下禁用此行为?
答案 0 :(得分:0)
您可以使用Model.with_attached_images.find(:id)
来避免N + 1
https://github.com/rails/rails/tree/master/activestorage#examples
我不确定您是否可以禁用急切加载附件,但是以上内容应该可以帮助您进行一些清理。