根据用户是否看过它,以及用户是否看过它,然后按照其拥有的观看次数(按升序排列),我提交了我想要的订单。这样,用户将能够在下一页/刷新时看到新的提交,而不是从视线中完全删除已查看的提交。我目前有2个表:提交和视图。
submissions
-----------
user_id
story_id
body
views
-----------
user_id
submissions_id
我有一个按查看次数排序的查询:
@story.submissions.select("submissions.*, count(views.submission_id) as views_count").
joins("LEFT OUTER JOIN views on views.submission_id=submissions.id").
group("submissions.id").
order("views_count")
但我不确定如何通过视图表中是否有条目来订购。
修改 我的查询无效,即使它确实有效。
答案 0 :(得分:0)
你可以这样做:
@story.submissions.select("submissions.*, count(views.submission_id) as views_count, user_views.id as user_view_id").
joins("LEFT OUTER JOIN views on views.submission_id=submissions.id LEFT OUTER JOIN views as user_views on user_views.submission_id=submissions.id and user_views.user_id = #{current_user.id}").
group("submissions.id").
order("user_view_id ASC, views_count DESC")
如果用户没有看到帖子,那么user_view_id
将为null,否则为非null。