[英]Rails3 - FIND(:all) with a Join, how to include a record from the JOIN?
[英]How do i not include latest record but all record from an object in rails?
例如,如果我想使用@ posts.all拉所有@posts,但是我不想包括其中的最后或最新记录?
这就是我想要做的
@posts = Post.all(没有创建的最新记录。)
基本上是所有记录,但不是最后一个记录。
我认为尝试生成不包含最后一个元素的SQL查询是不值得的。 特别是,子查询可能比将所有记录加载到Array
并排除最后一个要慢:
@posts = Post.all[0..-2]
注释中的其他示例如下所示:
@contact_prices = @contact.retail_prices.all.order("created_at DESC").load[0..-2]
另一个选择(取决于关系的顺序)可能是使用offset
:
@contact_prices = @contact.retail_prices.order("created_at DESC").offset(1)
这是我想做的最直接的方法:
Post.limit(Post.count - 1)
如果您希望查询允许分页或其他LIMIT查询,则可以尝试类似
Post.where("id < ?", Post.last.id)
很多答案可以解决问题,但会抛出其他选择:
@posts = Post.where("id != ?", Post.last.id)
一线AR:
Post.where.not(id: Post.last&.id)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.