开发者

Rails eager loading

开发者 https://www.devze.com 2022-12-31 04:46 出处:网络
HI, I have a Test model, which has_many questions, and Question, which has_many answers... When I make a query for a Test with :include => [:questions, {:questions => :answers}] ActiveRecord makes two

HI, I have a Test model, which has_many questions, and Question, which has_many answers... When I make a query for a Test with :include => [:questions, {:questions => :answers}] ActiveRecord makes two more queries to fetch the questions and then to fetch the answers - it doesn`t join them!!! When I do the query with :joins ActiveRecord makes the query, but later when I need the Test.questions or Test.questions.answers ActiveRecord makes again those开发者_Go百科 2 extra queries!!! And later when I enumerate the questions or answers in the log I see other queries for each object, but it has Cache tag...

Is this normal?


I agree with shteef, what happens when you do the following:

:include => { :questions => :answers }
0

精彩评论

暂无评论...
验证码 换一张
取 消