2017-07-15 67 views
0

所以,我有這種情況Rails的..把元,兒童和其他家長

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

includes有一些有用的事情可以做,比如

加載嵌套關係可以利用哈希:

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 } 
+0

任何疑問這似乎是最好的解決辦法現在..我會嘗試這個 – Jonathan