Rails总是渴望加载与default_scope的关联不起作用

时间:2015-05-15 15:55:34

标签: ruby-on-rails activerecord eager-loading

我正在尝试使用default_scope加载关联课程,但它似乎不起作用,因为正在生成N + 1个查询。由于after_initialize挂钩,这些查询存在。

如何删除那些N + 1个查询?

class User < ActiveRecord::Base
  has_one  :curriculum,
            autosave: true,
            dependent: :destroy,
            inverse_of: :user

  default_scope { includes(:curriculum) }
  after_initialize :build_curriculum,     if: "curriculum.blank?"
end



12:52:53 web.1    |   SQL (28.3ms)  SELECT DISTINCT "users"."id", "users"."name" AS alias_0, "users"."name" AS alias_1 FROM "users" LEFT OUTER JOIN "curriculums" ON "curriculums"."user_id" = "users"."id" LEFT OUTER JOIN "attendances" ON "attendances"."user_id" = "users"."id" WHERE "attendances"."event_id" = 1 ORDER BY "users"."name" ASC LIMIT 10 OFFSET 0
12:52:53 web.1    |   SQL (4.1ms)  SELECT "users"."id" AS t0_r0, "users"."name" AS t0_r1, "users"."lastname" AS t0_r2, "users"."created_at" AS t0_r3, "users"."updated_at" AS t0_r4, "users"."email" AS t0_r5, "users"."encrypted_password" AS t0_r6, "users"."reset_password_token" AS t0_r7, "users"."reset_password_sent_at" AS t0_r8, "users"."remember_created_at" AS t0_r9, "users"."sign_in_count" AS t0_r10, "users"."current_sign_in_at" AS t0_r11, "users"."last_sign_in_at" AS t0_r12, "users"."current_sign_in_ip" AS t0_r13, "users"."last_sign_in_ip" AS t0_r14, "users"."auth_token" AS t0_r15, "users"."role" AS t0_r16, "users"."cpf" AS t0_r17, "users"."source" AS t0_r18, "users"."linkedin_campaign_shown_at" AS t0_r19, "curriculums"."id" AS t1_r0, "curriculums"."user_id" AS t1_r1, "curriculums"."university_id" AS t1_r2, "curriculums"."picture" AS t1_r3, "curriculums"."phone" AS t1_r4, "curriculums"."end_year" AS t1_r5, "curriculums"."created_at" AS t1_r6, "curriculums"."updated_at" AS t1_r7, "curriculums"."document" AS t1_r8, "curriculums"."tag_code_conflict" AS t1_r9, "attendances"."id" AS t2_r0, "attendances"."event_id" AS t2_r1, "attendances"."user_id" AS t2_r2, "attendances"."tag_code" AS t2_r3, "attendances"."created_at" AS t2_r4, "attendances"."updated_at" AS t2_r5, "attendances"."tag_code_conflict" AS t2_r6, "attendances"."follows_campaign_shown_at" AS t2_r7 FROM "users" LEFT OUTER JOIN "curriculums" ON "curriculums"."user_id" = "users"."id" LEFT OUTER JOIN "attendances" ON "attendances"."user_id" = "users"."id" WHERE "attendances"."event_id" = 1 AND "users"."id" IN (3095, 3491, 2671, 2693, 1346, 3897, 4312, 5004, 3015, 5541) ORDER BY "users"."name" ASC
12:52:53 web.1    |   Curriculum Load (0.3ms)  SELECT "curriculums".* FROM "curriculums" WHERE "curriculums"."user_id" = $1 ORDER BY "curriculums"."id" ASC LIMIT 1  [["user_id", 3095]]
12:52:53 web.1    |   Curriculum Load (5.4ms)  SELECT "curriculums".* FROM "curriculums" WHERE "curriculums"."user_id" = $1 ORDER BY "curriculums"."id" ASC LIMIT 1  [["user_id", 3491]]
12:52:53 web.1    |   Curriculum Load (0.3ms)  SELECT "curriculums".* FROM "curriculums" WHERE "curriculums"."user_id" = $1 ORDER BY "curriculums"."id" ASC LIMIT 1  [["user_id", 2671]]
12:52:53 web.1    |   Curriculum Load (0.3ms)  SELECT "curriculums".* FROM "curriculums" WHERE "curriculums"."user_id" = $1 ORDER BY "curriculums"."id" ASC LIMIT 1  [["user_id", 2693]]
12:52:53 web.1    |   Curriculum Load (0.3ms)  SELECT "curriculums".* FROM "curriculums" WHERE "curriculums"."user_id" = $1 ORDER BY "curriculums"."id" ASC LIMIT 1  [["user_id", 1346]]
12:52:53 web.1    |   Curriculum Load (0.3ms)  SELECT "curriculums".* FROM "curriculums" WHERE "curriculums"."user_id" = $1 ORDER BY "curriculums"."id" ASC LIMIT 1  [["user_id", 3897]]
12:52:53 web.1    |   Curriculum Load (0.3ms)  SELECT "curriculums".* FROM "curriculums" WHERE "curriculums"."user_id" = $1 ORDER BY "curriculums"."id" ASC LIMIT 1  [["user_id", 4312]]
12:52:53 web.1    |   Curriculum Load (0.3ms)  SELECT "curriculums".* FROM "curriculums" WHERE "curriculums"."user_id" = $1 ORDER BY "curriculums"."id" ASC LIMIT 1  [["user_id", 5004]]
12:52:53 web.1    |   Curriculum Load (1.5ms)  SELECT "curriculums".* FROM "curriculums" WHERE "curriculums"."user_id" = $1 ORDER BY "curriculums"."id" ASC LIMIT 1  [["user_id", 3015]]
12:52:53 web.1    |   Curriculum Load (0.4ms)  SELECT "curriculums".* FROM "curriculums" WHERE "curriculums"."user_id" = $1 ORDER BY "curriculums"."id" ASC LIMIT 1  [["user_id", 5541]]

0 个答案:

没有答案