防止Rails管理员自定义字段的多个数据库请求

时间:2017-08-15 07:05:20

标签: ruby-on-rails performance rails-admin

我在rails admin中有自定义布尔字段,嵌套资源使用

class Sector
  has_many :holds, dependent: :destroy, inverse_of: :sector
  def holden
    holds.find_by_end_date(nil).present?
  end
end

当系统呈现Sectors列表时,我看到多个DB请求获取每个Sector的holden字段值。

  Hold Load (2.0ms)  SELECT  "holds".* FROM "holds" WHERE "holds"."sector_id" = $1 AND "holds"."end_date" IS NULL LIMIT $2  [["sector_id", 119], ["LIMIT", 1]]
  Hold Load (2.0ms)  SELECT  "holds".* FROM "holds" WHERE "holds"."sector_id" = $1 AND "holds"."end_date" IS NULL LIMIT $2  [["sector_id", 118], ["LIMIT", 1]]
  Hold Load (4.0ms)  SELECT  "holds".* FROM "holds" WHERE "holds"."sector_id" = $1 AND "holds"."end_date" IS NULL LIMIT $2  [["sector_id", 117], ["LIMIT", 1]]
  Hold Load (3.0ms)  SELECT  "holds".* FROM "holds" WHERE "holds"."sector_id" = $1 AND "holds"."end_date" IS NULL LIMIT $2  [["sector_id", 116], ["LIMIT", 1]]
  Hold Load (2.0ms)  SELECT  "holds".* FROM "holds" WHERE "holds"."sector_id" = $1 AND "holds"."end_date" IS NULL LIMIT $2  [["sector_id", 115], ["LIMIT", 1]]
  Hold Load (3.0ms)  SELECT  "holds".* FROM "holds" WHERE "holds"."sector_id" = $1 AND "holds"."end_date" IS NULL LIMIT $2  [["sector_id", 114], ["LIMIT", 1]]
  Hold Load (3.0ms)  SELECT  "holds".* FROM "holds" WHERE "holds"."sector_id" = $1 AND "holds"."end_date" IS NULL LIMIT $2  [["sector_id", 102], ["LIMIT", 1]]
  ...

如何提高此功能的效果?

0 个答案:

没有答案