Rails 3 ActiveRecord 查询接口,按每个父项最近创建的子项对父项进行排序
大家好,我有一个模型线程,有很多帖子。
我想知道使用 Rails3 如何按照最近创建的帖子的线程降序对线程进行排序。
在段/伪代码中,这就是我想要
为每个线程执行的操作,找到最近创建的帖子 以某种方式将该帖子日期与其相关帖子结合起来 按此关联的发布日期按升序对我的线程进行排序
Hey all, I have a model Thread that has_many Posts.
I was wondering with rails3 how i would go about Sorting the thread in descending order by which thread has the most recently created post.
In segments / pseudocode this is what i want to do
for each thread find the most recently created post
somehow combine that post date with its associated post
order my threads by this associated post date in ascending order
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这里最好和最性能的解决方案是使用
touch
选项:因此您将能够找到最后一个活动线程,就像这样简单:
查询怎么样...您可以尝试通过子对象找到它:
I think the best and most perfomance solution here is to use
touch
option:So you will able to find last active Threads as simple as:
What about query... You can try to find it via child objects: