Rails Rails API 多次 Rendered

HyeKyoZ-github · 2021年07月15日 · 最后由 HyeKyoZ-github 回复于 2021年07月15日 · 453 次阅读

使用 ActiveStorage 组件,为了避免 N+1 查询,使用了 has_one_attached: :field 对应的 with_attached_field 方法,之后就出现了多次 Rendered。 这里 去掉 .with_attached_attachment .with_attached_icon 这两个方法之后就正常了。

Started GET "/api/v1/posts?writable_id=2&writable_type=Navigation&query=%E6%96%87%E7%AB%A0" for ::1 at 2021-07-15 16:51:34 +0800
   (69.6ms)  SELECT `schema_migrations`.`version` FROM `schema_migrations` ORDER BY `schema_migrations`.`version` ASC
Processing by PostsController#index as */*
  Parameters: {"writable_id"=>"2", "writable_type"=>"Navigation", "query"=>"文章"}
  Navigation Load (69.0ms)  SELECT `navigations`.* FROM `navigations` WHERE `navigations`.`id` = 2 LIMIT 1
  ↳ app/controllers/posts_controller.rb:10:in `index'
  Rendering posts/index.json.jbuilder
   (68.9ms)  SELECT COUNT(*) FROM `posts` INNER JOIN `writings` ON `posts`.`id` = `writings`.`post_id` WHERE `writings`.`writable_id` = 2 AND `writings`.`writable_type` = 'Navigation' AND (title like '%文章%' or content like '%文章%')
  ↳ app/views/posts/index.json.jbuilder:1
  Post Load (137.4ms)  SELECT `posts`.* FROM `posts` INNER JOIN `writings` ON `posts`.`id` = `writings`.`post_id` WHERE `writings`.`writable_id` = 2 AND `writings`.`writable_type` = 'Navigation' AND (title like '%文章%' or content like '%文章%') ORDER BY `posts`.`id` DESC LIMIT 25 OFFSET 0
  ↳ app/views/posts/index.json.jbuilder:2
  Writing Load (68.0ms)  SELECT `writings`.* FROM `writings` WHERE `writings`.`writable_type` = 'Navigation' AND `writings`.`post_id` IN (47, 46, 45, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22)
  ↳ app/views/posts/index.json.jbuilder:2
  Navigation Load (78.1ms)  SELECT `navigations`.* FROM `navigations` WHERE `navigations`.`id` IN (1, 2)
  ↳ app/views/posts/index.json.jbuilder:2
  Writing Load (75.3ms)  SELECT `writings`.* FROM `writings` WHERE `writings`.`writable_type` = 'Theme' AND `writings`.`post_id` IN (47, 46, 45, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22)
  ↳ app/views/posts/index.json.jbuilder:2
  Theme Load (65.9ms)  SELECT `themes`.* FROM `themes` WHERE `themes`.`id` = 12
  ↳ app/views/posts/index.json.jbuilder:2
  ActiveStorage::Attachment Load (73.8ms)  SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_type` = 'Post' AND `active_storage_attachments`.`name` = 'attachment' AND `active_storage_attachments`.`record_id` IN (47, 46, 45, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22)
  ↳ app/views/posts/index.json.jbuilder:2
  ActiveStorage::Attachment Load (84.3ms)  SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_type` = 'Post' AND `active_storage_attachments`.`name` = 'icon' AND `active_storage_attachments`.`record_id` IN (47, 46, 45, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22)
  ↳ app/views/posts/index.json.jbuilder:2
  Rendered posts/_post.json.jbuilder (Duration: 1.4ms | Allocations: 322)
  Rendered posts/_post.json.jbuilder (Duration: 0.2ms | Allocations: 73)
  Rendered posts/_post.json.jbuilder (Duration: 0.2ms | Allocations: 73)
  Rendered posts/_post.json.jbuilder (Duration: 0.2ms | Allocations: 74)
  Rendered posts/_post.json.jbuilder (Duration: 0.2ms | Allocations: 74)
  Rendered posts/_post.json.jbuilder (Duration: 0.2ms | Allocations: 74)
  Rendered posts/_post.json.jbuilder (Duration: 0.2ms | Allocations: 74)
  Rendered posts/_post.json.jbuilder (Duration: 0.3ms | Allocations: 74)
  Rendered posts/_post.json.jbuilder (Duration: 0.2ms | Allocations: 74)
  Rendered posts/_post.json.jbuilder (Duration: 0.2ms | Allocations: 74)
  Rendered posts/_post.json.jbuilder (Duration: 0.1ms | Allocations: 74)
  Rendered posts/_post.json.jbuilder (Duration: 0.1ms | Allocations: 74)
  Rendered posts/_post.json.jbuilder (Duration: 0.1ms | Allocations: 74)
  Rendered posts/_post.json.jbuilder (Duration: 0.1ms | Allocations: 74)
  Rendered posts/_post.json.jbuilder (Duration: 0.2ms | Allocations: 74)
  Rendered posts/_post.json.jbuilder (Duration: 0.1ms | Allocations: 74)
  Rendered posts/_post.json.jbuilder (Duration: 0.1ms | Allocations: 74)
  Rendered posts/_post.json.jbuilder (Duration: 0.1ms | Allocations: 74)
  Rendered posts/_post.json.jbuilder (Duration: 0.1ms | Allocations: 74)
  Rendered posts/_post.json.jbuilder (Duration: 0.1ms | Allocations: 74)
  Rendered posts/_post.json.jbuilder (Duration: 0.1ms | Allocations: 74)
  Rendered posts/_post.json.jbuilder (Duration: 0.1ms | Allocations: 74)
  Rendered posts/_post.json.jbuilder (Duration: 0.1ms | Allocations: 74)
  Rendered posts/_post.json.jbuilder (Duration: 0.1ms | Allocations: 74)
  Rendered posts/_post.json.jbuilder (Duration: 0.1ms | Allocations: 74)
  Rendered posts/index.json.jbuilder (Duration: 1056.0ms | Allocations: 38657)
Completed 200 OK in 1524ms (Views: 131.5ms | ActiveRecord: 1338.4ms | Allocations: 59390)

下面是相关代码

# PostsController#index
def index
  scope = Post.all
  if params[:writable_id].present? and params[:writable_type].present?
    writable = case params[:writable_type]
               when Navigation.name
                 Navigation.find(params[:writable_id])
               when Theme.name
                 Theme.find(params[:writable_id])
               else
                 raise ArgumentError, "writable_type: #{params[:writable_type]} is invalid."
               end
    scope = writable.posts
  end
  scope = scope.where(kind: params[:kind]) if params[:kind].present?
  scope = scope.where("title like ?", "%#{params[:query]}%") if params[:query].present?
  @posts = scope.with_attached_attachment
                .with_attached_icon
                .preload(:navigations, :themes)
                .page(params[:page]).per(params[:limit])
                .order(id: :desc)
end
class Post < ApplicationRecord
  enum kind: { notice: 0, article: 1 }
  enum source: { original: 0, link: 1 }
  enum jump_type: { jump_link: 0, mini_program: 1 }

  has_one_attached :icon
  has_one_attached :attachment

  has_many :writings
  has_many :navigations, through: :writings, source: :writable, source_type: 'Navigation'
  has_many :themes, through: :writings, source: :writable, source_type: 'Theme'
end

下面是去掉 .with_attached_attachment .with_attached_icon 这两个方法之后 log

Started GET "/api/v1/posts?writable_id=2&writable_type=Navigation&query=%E6%96%87%E7%AB%A0" for ::1 at 2021-07-15 17:20:31 +0800
   (64.5ms)  SELECT `schema_migrations`.`version` FROM `schema_migrations` ORDER BY `schema_migrations`.`version` ASC
Processing by PostsController#index as */*
  Parameters: {"writable_id"=>"2", "writable_type"=>"Navigation", "query"=>"文章"}
  Navigation Load (72.1ms)  SELECT `navigations`.* FROM `navigations` WHERE `navigations`.`id` = 2 LIMIT 1
  ↳ app/controllers/posts_controller.rb:10:in `index'
  Rendering posts/index.json.jbuilder
   (60.8ms)  SELECT COUNT(*) FROM `posts` INNER JOIN `writings` ON `posts`.`id` = `writings`.`post_id` WHERE `writings`.`writable_id` = 2 AND `writings`.`writable_type` = 'Navigation' AND (title like '%文章%')
  ↳ app/views/posts/index.json.jbuilder:1
  Post Load (229.0ms)  SELECT `posts`.* FROM `posts` INNER JOIN `writings` ON `posts`.`id` = `writings`.`post_id` WHERE `writings`.`writable_id` = 2 AND `writings`.`writable_type` = 'Navigation' AND (title like '%文章%') ORDER BY `posts`.`id` DESC LIMIT 25 OFFSET 0
  ↳ app/views/posts/index.json.jbuilder:2
  Writing Load (77.0ms)  SELECT `writings`.* FROM `writings` WHERE `writings`.`writable_type` = 'Navigation' AND `writings`.`post_id` IN (47, 46, 45, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22)
  ↳ app/views/posts/index.json.jbuilder:2
  Navigation Load (75.7ms)  SELECT `navigations`.* FROM `navigations` WHERE `navigations`.`id` IN (1, 2)
  ↳ app/views/posts/index.json.jbuilder:2
  Writing Load (72.5ms)  SELECT `writings`.* FROM `writings` WHERE `writings`.`writable_type` = 'Theme' AND `writings`.`post_id` IN (47, 46, 45, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22)
  ↳ app/views/posts/index.json.jbuilder:2
  Theme Load (94.7ms)  SELECT `themes`.* FROM `themes` WHERE `themes`.`id` = 12
  ↳ app/views/posts/index.json.jbuilder:2
  ActiveStorage::Attachment Load (82.1ms)  SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 47 AND `active_storage_attachments`.`record_type` = 'Post' AND `active_storage_attachments`.`name` = 'icon' LIMIT 1
  ↳ app/views/posts/_post.json.jbuilder:5
  ActiveStorage::Attachment Load (68.2ms)  SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 47 AND `active_storage_attachments`.`record_type` = 'Post' AND `active_storage_attachments`.`name` = 'attachment' LIMIT 1
  ↳ app/views/posts/_post.json.jbuilder:14
  Rendered posts/_post.json.jbuilder (Duration: 265.2ms | Allocations: 4326)
  ActiveStorage::Attachment Load (67.3ms)  SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 46 AND `active_storage_attachments`.`record_type` = 'Post' AND `active_storage_attachments`.`name` = 'icon' LIMIT 1
  ↳ app/views/posts/_post.json.jbuilder:5
  ActiveStorage::Attachment Load (76.4ms)  SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 46 AND `active_storage_attachments`.`record_type` = 'Post' AND `active_storage_attachments`.`name` = 'attachment' LIMIT 1
  ↳ app/views/posts/_post.json.jbuilder:14
  Rendered posts/_post.json.jbuilder (Duration: 149.8ms | Allocations: 1787)
  ActiveStorage::Attachment Load (71.2ms)  SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 45 AND `active_storage_attachments`.`record_type` = 'Post' AND `active_storage_attachments`.`name` = 'icon' LIMIT 1
  ↳ app/views/posts/_post.json.jbuilder:5
  ActiveStorage::Attachment Load (93.4ms)  SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 45 AND `active_storage_attachments`.`record_type` = 'Post' AND `active_storage_attachments`.`name` = 'attachment' LIMIT 1
  ↳ app/views/posts/_post.json.jbuilder:14
  Rendered posts/_post.json.jbuilder (Duration: 171.1ms | Allocations: 1771)
  ActiveStorage::Attachment Load (78.6ms)  SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 43 AND `active_storage_attachments`.`record_type` = 'Post' AND `active_storage_attachments`.`name` = 'icon' LIMIT 1
  ↳ app/views/posts/_post.json.jbuilder:5
  ActiveStorage::Attachment Load (64.2ms)  SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 43 AND `active_storage_attachments`.`record_type` = 'Post' AND `active_storage_attachments`.`name` = 'attachment' LIMIT 1
  ↳ app/views/posts/_post.json.jbuilder:14
  Rendered posts/_post.json.jbuilder (Duration: 146.7ms | Allocations: 1772)
  ActiveStorage::Attachment Load (69.9ms)  SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 42 AND `active_storage_attachments`.`record_type` = 'Post' AND `active_storage_attachments`.`name` = 'icon' LIMIT 1
  ↳ app/views/posts/_post.json.jbuilder:5
  ActiveStorage::Attachment Load (64.9ms)  SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 42 AND `active_storage_attachments`.`record_type` = 'Post' AND `active_storage_attachments`.`name` = 'attachment' LIMIT 1
  ↳ app/views/posts/_post.json.jbuilder:14
  Rendered posts/_post.json.jbuilder (Duration: 139.1ms | Allocations: 1772)
  ActiveStorage::Attachment Load (70.5ms)  SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 41 AND `active_storage_attachments`.`record_type` = 'Post' AND `active_storage_attachments`.`name` = 'icon' LIMIT 1
  ↳ app/views/posts/_post.json.jbuilder:5
  ActiveStorage::Attachment Load (86.1ms)  SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 41 AND `active_storage_attachments`.`record_type` = 'Post' AND `active_storage_attachments`.`name` = 'attachment' LIMIT 1
  ↳ app/views/posts/_post.json.jbuilder:14
  Rendered posts/_post.json.jbuilder (Duration: 161.7ms | Allocations: 1778)
  ActiveStorage::Attachment Load (70.0ms)  SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 40 AND `active_storage_attachments`.`record_type` = 'Post' AND `active_storage_attachments`.`name` = 'icon' LIMIT 1
  ↳ app/views/posts/_post.json.jbuilder:5
  ActiveStorage::Attachment Load (77.7ms)  SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 40 AND `active_storage_attachments`.`record_type` = 'Post' AND `active_storage_attachments`.`name` = 'attachment' LIMIT 1
  ↳ app/views/posts/_post.json.jbuilder:14
  Rendered posts/_post.json.jbuilder (Duration: 153.8ms | Allocations: 1772)
  ActiveStorage::Attachment Load (75.5ms)  SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 39 AND `active_storage_attachments`.`record_type` = 'Post' AND `active_storage_attachments`.`name` = 'icon' LIMIT 1
  ↳ app/views/posts/_post.json.jbuilder:5
  ActiveStorage::Attachment Load (85.3ms)  SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 39 AND `active_storage_attachments`.`record_type` = 'Post' AND `active_storage_attachments`.`name` = 'attachment' LIMIT 1
  ↳ app/views/posts/_post.json.jbuilder:14
  Rendered posts/_post.json.jbuilder (Duration: 166.5ms | Allocations: 1772)
  ActiveStorage::Attachment Load (84.6ms)  SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 38 AND `active_storage_attachments`.`record_type` = 'Post' AND `active_storage_attachments`.`name` = 'icon' LIMIT 1
  ↳ app/views/posts/_post.json.jbuilder:5
  ActiveStorage::Attachment Load (104.6ms)  SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 38 AND `active_storage_attachments`.`record_type` = 'Post' AND `active_storage_attachments`.`name` = 'attachment' LIMIT 1
  ↳ app/views/posts/_post.json.jbuilder:14
  Rendered posts/_post.json.jbuilder (Duration: 195.1ms | Allocations: 1772)
  ActiveStorage::Attachment Load (91.9ms)  SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 37 AND `active_storage_attachments`.`record_type` = 'Post' AND `active_storage_attachments`.`name` = 'icon' LIMIT 1
  ↳ app/views/posts/_post.json.jbuilder:5
  ActiveStorage::Attachment Load (73.4ms)  SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 37 AND `active_storage_attachments`.`record_type` = 'Post' AND `active_storage_attachments`.`name` = 'attachment' LIMIT 1
  ↳ app/views/posts/_post.json.jbuilder:14
  Rendered posts/_post.json.jbuilder (Duration: 170.7ms | Allocations: 1772)
  ActiveStorage::Attachment Load (67.8ms)  SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 36 AND `active_storage_attachments`.`record_type` = 'Post' AND `active_storage_attachments`.`name` = 'icon' LIMIT 1
  ↳ app/views/posts/_post.json.jbuilder:5
  ActiveStorage::Attachment Load (78.3ms)  SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 36 AND `active_storage_attachments`.`record_type` = 'Post' AND `active_storage_attachments`.`name` = 'attachment' LIMIT 1
  ↳ app/views/posts/_post.json.jbuilder:14
  Rendered posts/_post.json.jbuilder (Duration: 153.8ms | Allocations: 1772)
  ActiveStorage::Attachment Load (68.2ms)  SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 35 AND `active_storage_attachments`.`record_type` = 'Post' AND `active_storage_attachments`.`name` = 'icon' LIMIT 1
  ↳ app/views/posts/_post.json.jbuilder:5
  ActiveStorage::Attachment Load (63.4ms)  SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 35 AND `active_storage_attachments`.`record_type` = 'Post' AND `active_storage_attachments`.`name` = 'attachment' LIMIT 1
  ↳ app/views/posts/_post.json.jbuilder:14
  Rendered posts/_post.json.jbuilder (Duration: 137.9ms | Allocations: 1773)
  ActiveStorage::Attachment Load (85.7ms)  SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 34 AND `active_storage_attachments`.`record_type` = 'Post' AND `active_storage_attachments`.`name` = 'icon' LIMIT 1
  ↳ app/views/posts/_post.json.jbuilder:5
  ActiveStorage::Attachment Load (62.6ms)  SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 34 AND `active_storage_attachments`.`record_type` = 'Post' AND `active_storage_attachments`.`name` = 'attachment' LIMIT 1
  ↳ app/views/posts/_post.json.jbuilder:14
  Rendered posts/_post.json.jbuilder (Duration: 152.7ms | Allocations: 1772)
  ActiveStorage::Attachment Load (65.1ms)  SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 33 AND `active_storage_attachments`.`record_type` = 'Post' AND `active_storage_attachments`.`name` = 'icon' LIMIT 1
  ↳ app/views/posts/_post.json.jbuilder:5
  ActiveStorage::Attachment Load (70.3ms)  SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 33 AND `active_storage_attachments`.`record_type` = 'Post' AND `active_storage_attachments`.`name` = 'attachment' LIMIT 1
  ↳ app/views/posts/_post.json.jbuilder:14
  Rendered posts/_post.json.jbuilder (Duration: 140.4ms | Allocations: 1772)
  ActiveStorage::Attachment Load (68.9ms)  SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 32 AND `active_storage_attachments`.`record_type` = 'Post' AND `active_storage_attachments`.`name` = 'icon' LIMIT 1
  ↳ app/views/posts/_post.json.jbuilder:5
  ActiveStorage::Attachment Load (75.7ms)  SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 32 AND `active_storage_attachments`.`record_type` = 'Post' AND `active_storage_attachments`.`name` = 'attachment' LIMIT 1
  ↳ app/views/posts/_post.json.jbuilder:14
  Rendered posts/_post.json.jbuilder (Duration: 151.2ms | Allocations: 1772)
  ActiveStorage::Attachment Load (70.3ms)  SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 31 AND `active_storage_attachments`.`record_type` = 'Post' AND `active_storage_attachments`.`name` = 'icon' LIMIT 1
  ↳ app/views/posts/_post.json.jbuilder:5
  ActiveStorage::Attachment Load (78.2ms)  SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 31 AND `active_storage_attachments`.`record_type` = 'Post' AND `active_storage_attachments`.`name` = 'attachment' LIMIT 1
  ↳ app/views/posts/_post.json.jbuilder:14
  Rendered posts/_post.json.jbuilder (Duration: 153.3ms | Allocations: 1772)
  ActiveStorage::Attachment Load (74.5ms)  SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 30 AND `active_storage_attachments`.`record_type` = 'Post' AND `active_storage_attachments`.`name` = 'icon' LIMIT 1
  ↳ app/views/posts/_post.json.jbuilder:5
  ActiveStorage::Attachment Load (74.9ms)  SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 30 AND `active_storage_attachments`.`record_type` = 'Post' AND `active_storage_attachments`.`name` = 'attachment' LIMIT 1
  ↳ app/views/posts/_post.json.jbuilder:14
  Rendered posts/_post.json.jbuilder (Duration: 155.8ms | Allocations: 1772)
  ActiveStorage::Attachment Load (81.9ms)  SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 29 AND `active_storage_attachments`.`record_type` = 'Post' AND `active_storage_attachments`.`name` = 'icon' LIMIT 1
  ↳ app/views/posts/_post.json.jbuilder:5
  ActiveStorage::Attachment Load (84.5ms)  SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 29 AND `active_storage_attachments`.`record_type` = 'Post' AND `active_storage_attachments`.`name` = 'attachment' LIMIT 1
  ↳ app/views/posts/_post.json.jbuilder:14
  Rendered posts/_post.json.jbuilder (Duration: 172.7ms | Allocations: 1776)
  ActiveStorage::Attachment Load (73.3ms)  SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 28 AND `active_storage_attachments`.`record_type` = 'Post' AND `active_storage_attachments`.`name` = 'icon' LIMIT 1
  ↳ app/views/posts/_post.json.jbuilder:5
  ActiveStorage::Attachment Load (71.7ms)  SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 28 AND `active_storage_attachments`.`record_type` = 'Post' AND `active_storage_attachments`.`name` = 'attachment' LIMIT 1
  ↳ app/views/posts/_post.json.jbuilder:14
  Rendered posts/_post.json.jbuilder (Duration: 150.7ms | Allocations: 1772)
  ActiveStorage::Attachment Load (79.9ms)  SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 27 AND `active_storage_attachments`.`record_type` = 'Post' AND `active_storage_attachments`.`name` = 'icon' LIMIT 1
  ↳ app/views/posts/_post.json.jbuilder:5
  ActiveStorage::Attachment Load (75.2ms)  SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 27 AND `active_storage_attachments`.`record_type` = 'Post' AND `active_storage_attachments`.`name` = 'attachment' LIMIT 1
  ↳ app/views/posts/_post.json.jbuilder:14
  Rendered posts/_post.json.jbuilder (Duration: 160.4ms | Allocations: 1772)
  ActiveStorage::Attachment Load (80.3ms)  SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 26 AND `active_storage_attachments`.`record_type` = 'Post' AND `active_storage_attachments`.`name` = 'icon' LIMIT 1
  ↳ app/views/posts/_post.json.jbuilder:5
  ActiveStorage::Attachment Load (74.2ms)  SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 26 AND `active_storage_attachments`.`record_type` = 'Post' AND `active_storage_attachments`.`name` = 'attachment' LIMIT 1
  ↳ app/views/posts/_post.json.jbuilder:14
  Rendered posts/_post.json.jbuilder (Duration: 159.8ms | Allocations: 1772)
  ActiveStorage::Attachment Load (108.3ms)  SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 25 AND `active_storage_attachments`.`record_type` = 'Post' AND `active_storage_attachments`.`name` = 'icon' LIMIT 1
  ↳ app/views/posts/_post.json.jbuilder:5
  ActiveStorage::Attachment Load (85.2ms)  SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 25 AND `active_storage_attachments`.`record_type` = 'Post' AND `active_storage_attachments`.`name` = 'attachment' LIMIT 1
  ↳ app/views/posts/_post.json.jbuilder:14
  Rendered posts/_post.json.jbuilder (Duration: 199.2ms | Allocations: 1772)
  ActiveStorage::Attachment Load (70.8ms)  SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 24 AND `active_storage_attachments`.`record_type` = 'Post' AND `active_storage_attachments`.`name` = 'icon' LIMIT 1
  ↳ app/views/posts/_post.json.jbuilder:5
  ActiveStorage::Attachment Load (63.1ms)  SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 24 AND `active_storage_attachments`.`record_type` = 'Post' AND `active_storage_attachments`.`name` = 'attachment' LIMIT 1
  ↳ app/views/posts/_post.json.jbuilder:14
  Rendered posts/_post.json.jbuilder (Duration: 139.1ms | Allocations: 1772)
  ActiveStorage::Attachment Load (78.2ms)  SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 23 AND `active_storage_attachments`.`record_type` = 'Post' AND `active_storage_attachments`.`name` = 'icon' LIMIT 1
  ↳ app/views/posts/_post.json.jbuilder:5
  ActiveStorage::Attachment Load (79.5ms)  SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 23 AND `active_storage_attachments`.`record_type` = 'Post' AND `active_storage_attachments`.`name` = 'attachment' LIMIT 1
  ↳ app/views/posts/_post.json.jbuilder:14
  Rendered posts/_post.json.jbuilder (Duration: 162.6ms | Allocations: 1772)
  ActiveStorage::Attachment Load (67.3ms)  SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 22 AND `active_storage_attachments`.`record_type` = 'Post' AND `active_storage_attachments`.`name` = 'icon' LIMIT 1
  ↳ app/views/posts/_post.json.jbuilder:5
  ActiveStorage::Attachment Load (83.7ms)  SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 22 AND `active_storage_attachments`.`record_type` = 'Post' AND `active_storage_attachments`.`name` = 'attachment' LIMIT 1
  ↳ app/views/posts/_post.json.jbuilder:14
  Rendered posts/_post.json.jbuilder (Duration: 156.9ms | Allocations: 1773)
  Rendered posts/index.json.jbuilder (Duration: 4976.1ms | Allocations: 79057)
Completed 200 OK in 5492ms (Views: 224.6ms | ActiveRecord: 5210.9ms | Allocations: 99695)

没看出异常,默认分页 25 个 post,渲染 25 次 post 的片段 json 应该是正常的。

LiinNs 回复

关键是去掉.with_attached_attachment .with_attached_icon这两个方法之后就只渲染一次了

HyeKyoZ-github 关闭了讨论。 07月15日 17:23
HyeKyoZ-github 重新开启了讨论。 07月15日 17:24
HyeKyoZ-github 重新开启了讨论。 07月15日 17:24
LiinNs 回复

知道了,谢谢

HyeKyoZ-github 关闭了讨论。 07月15日 17:25
需要 登录 后方可回复, 如果你还没有账号请 注册新账号