Active Record 连接中的限制和偏移
我想对以下 Active Record 查询应用限制和偏移:
results = ForumThread.joins(:posts).where(:posts => {:some_integer => 123})
注意模型中的 ForumThread :has_many posts 关联。
我尝试在 where 子句中包含 @options
哈希值。我尝试将 .limit(5)
附加到查询末尾。然而这些都不起作用。阅读 Active Record 查询界面指南 也没有帮助。 如何对我的查询应用限制和偏移?
如果替代查找方法更合适,我愿意修改查询。
I'd like to apply a limit and offset to the following Active Record query:
results = ForumThread.joins(:posts).where(:posts => {:some_integer => 123})
Note ForumThread :has_many posts association in the model.
I tried including an @options
hash in the where clause. I tried appending .limit(5)
to the end of the query. However neither of these work. Reading Active Record Query Interface guide didn't help either. How can I apply limit and offset to my query?
I'm open to modifying the query if an alternative finder method is more appropriate.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于我稍后调用了
results.count
,因此查询包含COUNT(*)
。显然,SELECT COUNT(*)
忽略了限制子句,因此将LIMIT(5)
附加到查询中没有效果。The query contains
COUNT(*)
since I was callingresults.count
later. ApparentlySELECT COUNT(*)
ignores limit clauses hence appendingLIMIT(5)
to the query had no effect.