开发者

Rails How to Use Include

I have the following:

@comments = @message.comments.roots.order("created_at DESC")

This gets all the the comments for a particular message.

Later in the page I run several:

comment.user.profile_pic.url(:small)

The problem with this is that the save user query is hitting the database over and over.

User Load (0.9ms)  SELECT "users".* FROM "users" WHERE ("users"."id" = 3) LIMIT 1

I'm curious to learn how I can use include 开发者_JAVA技巧to eager load the user info along with the comments above. I tried:

@comments = @message.comments.include(:users).roots.order("created_at DESC")

Suggestions? Thanks


Since a Comment has only one User, you might try .includes(:user)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜