Skip to content

Commit

Permalink
Fix N+1 queries for ActiveStorage attachments
Browse files Browse the repository at this point in the history
Closes #3282
  • Loading branch information
mshibuya committed Oct 22, 2021
1 parent 4404758 commit e4d5b2f
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/rails_admin/config/fields/types/active_storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ class ActiveStorage < RailsAdmin::Config::Fields::Types::FileUpload
end
end

register_instance_option :eager_load do
{"#{name}_attachment": :blob}
end

def resource_url(thumb = false)
return nil unless value
if thumb && value.variable?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ def resource_url(thumb = false)
register_instance_option :delete_method do
"remove_#{name}" if bindings[:object].respond_to?("remove_#{name}")
end

register_instance_option :eager_load do
{"#{name}_attachments": :blob}
end
end
end
end
Expand Down
6 changes: 6 additions & 0 deletions spec/rails_admin/config/fields/types/active_storage_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,10 @@
end
end
end

describe '#eager_load' do
it 'points to associations to be eager-loaded' do
expect(field.eager_load).to eq({active_storage_asset_attachment: :blob})
end
end
end if defined?(ActiveStorage)
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,10 @@
end
end
end

describe '#eager_load' do
it 'points to associations to be eager-loaded' do
expect(field.eager_load).to eq({active_storage_assets_attachments: :blob})
end
end
end if defined?(ActiveStorage)

0 comments on commit e4d5b2f

Please sign in to comment.