Rails rails 怎么写这种子查询???

lin_style · June 28, 2012 · Last by leomao10 replied at June 28, 2012 · 3892 hits

select * from (select * from users where id!=1 limit 5) as a order by id limit 6 offset 0

如下这种语句,需求就是分页的时候,我想指定 limit 的数据,用的是 kaminari 的插件

User.where(xxxx).limit(5).map{ |u| u.orders.limit(6) }.flat.uniq!

@lin_style, 没看懂你的 sql,你要怎么指定 limit 的数据?

不过如果说子查询,你可以用 squeel(https://github.com/ernie/squeel), 它可以直接在 where 里面写子查询

或者把子查询拆成两个 sql,通常我的第一个 sql 都只会返回 id 数组,这样可以保证 rails 不会创建大量的 object

You need to Sign in before reply, if you don't have an account, please Sign up first.