Skip to content

Latest commit

 

History

History
22 lines (14 loc) · 484 Bytes

2019-05-13_reduce_active_storage_queries.md

File metadata and controls

22 lines (14 loc) · 484 Bytes

Reduce Active Storage Queries (N+1)

05/13/2019

Consider we are using ActiveStorage in a Rails project and we have a model like so:

class Document < ApplicationRecord
  has_one_attached :file
end

If our controller query for Document(s) does not preload the file, we will have an N+ 1 query on our hands.

Luckily, ActiveStorage includes a method to reduce N+1's. We can use it like so:

Document.with_attached_file

Tags: Rails, ActiveStorage, Ruby