通过关联created_at,或者通过自己的created_at如果没有关联订单 [英] Order by association created_at, or by own created_at if no association

查看:203
本文介绍了通过关联created_at,或者通过自己的created_at如果没有关联订单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的模型组的has_many的帖子,帖子have_many评论。

My group model has_many posts, posts have_many comments.

当我告诉一组帖子,我想在最近的评论中创建的,如果有任何一个职位,或职位本身的created_at订购。

When I show the posts on a group, I want to order by the created at of the most recent comment, if there are any on a post, or by the created_at of the post itself.

所以,旧的文章会跳回到列表的顶部,如果它有一个新的注释。

So an old post would jump back to the top of the list if it has a new comment.

推荐答案

我会坚持一点点不同的方法:你的评论belongs_to的:POST belongs_to的接受:触摸选项。将其设置为真正和你的帖子的的updated_at 将自动更新的注释的修改。这样你就可以自由地展示自己的帖子被下令的updated_at

I would stick to a little different approach: your Comment belongs_to :post and belongs_to accepts :touch option. Set it to true and your Post's updated_at will be automatically updated on a Comment's modification. This way you can freely show your posts ordered by their updated_at.

这篇关于通过关联created_at,或者通过自己的created_at如果没有关联订单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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