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]
]
任何疑問這似乎是最好的解決辦法現在..我會嘗試這個 – Jonathan