Rails ..带元素,孩子和其他父母

时间:2017-07-15 22:07:35

标签: ruby-on-rails activerecord

所以,我有这种情况

class AnamnesisModel < ApplicationRecord
  has_many :anamnesis_questions
  accepts_nested_attributes_for :anamnesis_questions
end

class AnamnesisQuestion < ApplicationRecord
  belongs_to :anamnesis_model
  belongs_to :question
end

class Question < ApplicationRecord
end

而且,我想找到一个特定的AnamnesisModel,就像这样

AnamnesisModel.find(1)

然后,带上所有对象,例如AnamnesisModel,AnamnesisQuestion和Question,只执行一个(或两个)sql。

我一直在尝试像preload,eager_load,include这样的东西,但是,最后..带来问题,它为每个AnamnesisQuestions执行一个sql。

Question Load (0.0ms)  SELECT  "questions".* FROM "questions" WHERE "questions"."id" = ? LIMIT ?  [["id", 66], ["LIMIT", 1]
]
  Question Load (0.0ms)  SELECT  "questions".* FROM "questions" WHERE "questions"."id" = ? LIMIT ?  [["id", 67], ["LIMIT", 1]
]
  Question Load (0.0ms)  SELECT  "questions".* FROM "questions" WHERE "questions"."id" = ? LIMIT ?  [["id", 68], ["LIMIT", 1]
]
  Question Load (0.0ms)  SELECT  "questions".* FROM "questions" WHERE "questions"."id" = ? LIMIT ?  [["id", 69], ["LIMIT", 1]
]
  Question Load (0.0ms)  SELECT  "questions".* FROM "questions" WHERE "questions"."id" = ? LIMIT ?  [["id", 70], ["LIMIT", 1]
]
  Question Load (0.0ms)  SELECT  "questions".* FROM "questions" WHERE "questions"."id" = ? LIMIT ?  [["id", 71], ["LIMIT", 1]
]
  Question Load (0.0ms)  SELECT  "questions".* FROM "questions" WHERE "questions"."id" = ? LIMIT ?  [["id", 72], ["LIMIT", 1]
]
  Question Load (0.0ms)  SELECT  "questions".* FROM "questions" WHERE "questions"."id" = ? LIMIT ?  [["id", 73], ["LIMIT", 1]
]
  Question Load (0.0ms)  SELECT  "questions".* FROM "questions" WHERE "questions"."id" = ? LIMIT ?  [["id", 74], ["LIMIT", 1]
]
  Question Load (0.0ms)  SELECT  "questions".* FROM "questions" WHERE "questions"."id" = ? LIMIT ?  [["id", 75], ["LIMIT", 1]
]
  Question Load (0.0ms)  SELECT  "questions".* FROM "questions" WHERE "questions"."id" = ? LIMIT ?  [["id", 76], ["LIMIT", 1]
]
  Question Load (1.0ms)  SELECT  "questions".* FROM "questions" WHERE "questions"."id" = ? LIMIT ?  [["id", 77], ["LIMIT", 1]
]
  Question Load (0.0ms)  SELECT  "questions".* FROM "questions" WHERE "questions"."id" = ? LIMIT ?  [["id", 78], ["LIMIT", 1]
]
  Question Load (0.0ms)  SELECT  "questions".* FROM "questions" WHERE "questions"."id" = ? LIMIT ?  [["id", 79], ["LIMIT", 1]
]
  Question Load (0.0ms)  SELECT  "questions".* FROM "questions" WHERE "questions"."id" = ? LIMIT ?  [["id", 80], ["LIMIT", 1]
]
  Question Load (0.0ms)  SELECT  "questions".* FROM "questions" WHERE "questions"."id" = ? LIMIT ?  [["id", 81], ["LIMIT", 1]
]
  Question Load (1.0ms)  SELECT  "questions".* FROM "questions" WHERE "questions"."id" = ? LIMIT ?  [["id", 82], ["LIMIT", 1]
]
  Question Load (0.0ms)  SELECT  "questions".* FROM "questions" WHERE "questions"."id" = ? LIMIT ?  [["id", 83], ["LIMIT", 1]
]
  Question Load (0.0ms)  SELECT  "questions".* FROM "questions" WHERE "questions"."id" = ? LIMIT ?  [["id", 84], ["LIMIT", 1]
]

1 个答案:

答案 0 :(得分:1)

includes有许多有用的功能,例如

  

使用Hash:

可以加载嵌套关系
users = User.includes(:address, friends: [:address, :followers])

所以,我们可以这样做:

model = Anamnesis.includes(anamnesis_questions: :question).find(1)
#  Anamnesis Load (0.1ms)  SELECT  "anamneses".* FROM "anamneses" WHERE "anamneses"."id" = ? LIMIT ?  [["id", 1], ["LIMIT", 1]]
#  AnamnesisQuestion Load (0.2ms)  SELECT "anamnesis_questions".* FROM "anamnesis_questions" WHERE "anamnesis_questions"."anamnesis_id" = 1
#  Question Load (0.2ms)  SELECT "questions".* FROM "questions" WHERE "questions"."id" IN (2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21)
# => #<Anamnesis id: 1, created_at: "2017-07-15 22:17:33", updated_at: "2017-07-15 22:17:33">

然后您不需要进一步查询

# No SQL queries generated
model.anamnesis_questions.each { |aq| puts aq.question.id }