In my rails app I have few related models for example:
在我的rails应用程序中,我有几个相关的模型,例如:
Event
has_many :comments
has_many :attendents
has_many :requests
What I need is to order by 'created_at' but not only main model (Event) but also related models, so I will display on top of the list event with most recent activity i.e.: comments, attendents, requests
我需要的是通过'created_at'排序,但不仅仅是主模型(事件),还有相关的模型,因此我将在列表事件的顶部显示最近的活动,即:评论,参与者,请求
So if Event date is newer than any comment, request or attendent date this event will be on top. But if there is an event with newer comment this one will be on top etc.
因此,如果活动日期比任何评论,请求或约会日期更新,则此活动将在最前面。但如果有一个更新评论的事件,这个将在顶部等。
How should I implement such ordering?
我该如何实施这样的订购?
EDIT db is mysql
编辑db是mysql
Thanks
3 个解决方案
#1
I would place a column on the event for last_activity, and maintain it by touching from the associated models.
我会在last_activity的事件上放置一个列,并通过触摸相关模型来维护它。
The alternative is to order by using a join or subquery against the other tables, which is going to require a database query that will be much less efficient than simply ordering by last_activity descending.
另一种方法是通过对其他表使用连接或子查询来进行排序,这将要求数据库查询的效率远低于仅通过last_activity降序排序。
#2
Event
.select("events.*")
.joins("LEFT OUTER JOIN comments ON comments.event_id = events.id")
.joins("LEFT OUTER JOIN attendents ON attendents.event_id = events.id" )
.joins("LEFT OUTER JOIN requests ON requests.event_id = events.id")
.group("events.id")
.order("GREATEST(events.created_at,
MAX(comments.created_at),
MAX(attendents.created_at),
MAX(requests.created_at)) DESC")
#3
If I understand your question correctly, something like this
如果我正确理解你的问题,就像这样
Firstly add this to your Models in question:
首先将此添加到您的模型中:
default_scope order('created_at DESC')
Then I would do some grouping:
然后我会做一些分组:
@comments = Comment.all
@comments_group = @comments.group_by { |c| c.event}
And in your view you'll be able to loop through each block:
在您的视图中,您将能够遍历每个块:
@comments_group.each do |event, comment|
- event.name
comment.each do|c|
- c.body
I did not test this but it should give you an idea.
我没有测试这个,但它应该给你一个想法。
#1
I would place a column on the event for last_activity, and maintain it by touching from the associated models.
我会在last_activity的事件上放置一个列,并通过触摸相关模型来维护它。
The alternative is to order by using a join or subquery against the other tables, which is going to require a database query that will be much less efficient than simply ordering by last_activity descending.
另一种方法是通过对其他表使用连接或子查询来进行排序,这将要求数据库查询的效率远低于仅通过last_activity降序排序。
#2
Event
.select("events.*")
.joins("LEFT OUTER JOIN comments ON comments.event_id = events.id")
.joins("LEFT OUTER JOIN attendents ON attendents.event_id = events.id" )
.joins("LEFT OUTER JOIN requests ON requests.event_id = events.id")
.group("events.id")
.order("GREATEST(events.created_at,
MAX(comments.created_at),
MAX(attendents.created_at),
MAX(requests.created_at)) DESC")
#3
If I understand your question correctly, something like this
如果我正确理解你的问题,就像这样
Firstly add this to your Models in question:
首先将此添加到您的模型中:
default_scope order('created_at DESC')
Then I would do some grouping:
然后我会做一些分组:
@comments = Comment.all
@comments_group = @comments.group_by { |c| c.event}
And in your view you'll be able to loop through each block:
在您的视图中,您将能够遍历每个块:
@comments_group.each do |event, comment|
- event.name
comment.each do|c|
- c.body
I did not test this but it should give you an idea.
我没有测试这个,但它应该给你一个想法。