预先加载的关联,但上限回报 [英] Eager loading association but limit return

查看:110
本文介绍了预先加载的关联,但上限回报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎是一个简单的查询,但我不能钉吧。

It seems like a simple query but I just can't nail it.

基本上它归结为具有职位类别多岁的老观念。预期协会到位,一个类别的has_many的帖子,而帖子属于一个类别。

Basically it boils down to the age old concept of a categories having posts. The expected associations are in place, a category has_many posts whilst a post belongs to a category.

我要检索的所有类别,有自己的岗位,但限制的职位数为10。

I want to retrieve all the categories with their posts but limit the number of posts to 10.

任何想法?

推荐答案

这是不是你可以用原始的SQL操作为限的是在总数据集大小,而不是别的。

This isn't something that you can do with raw SQL as LIMIT's are on the total dataset size, not anything else.

纯粹由SQL这样做的唯一方法是创建一个假身份证列的联接和筛选比出来时,这恐怕是实现明智的是令人难以置信的依赖正在使用的数据库服务器上。

The only way of doing this purely by SQL is to create a fake id column in your join and filter than when it comes out which is something that implementation wise is incredibly dependant on the database server you are using.

该方案是要么得到所有类别和职位,并削减记录下来,还是让所有类别和迭代获得10个职位为约尔格建议。

The alternatives as either get all categories and posts and cut the recordset down, or get all categories and iteratively get 10 posts as Joerg suggests.

这篇关于预先加载的关联,但上限回报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆