修改获取所有消息的排序算法,使用数据库的order

This commit is contained in:
ouyangxuhua 2015-10-09 10:57:53 +08:00
parent fce872a6a5
commit c4c005a39b
1 changed files with 1 additions and 8 deletions

View File

@ -112,17 +112,10 @@ class UsersController < ApplicationController
case params[:type]
when nil
@message_alls = []
system_message_types = MessageAll.where("message_type =?", "SystemMessage").order("created_at desc")
messages = MessageAll.where("user_id =? and message_type !=?" ,@user, "SystemMessage").order("created_at desc")
messages = MessageAll.where("(user_id =? and message_type !=?) or message_type =?" ,@user, "SystemMessage", "SystemMessage").order("created_at desc")
messages.each do |message_all|
@message_alls << message_all.message
end
# 系统消息需要给每个人发送,之所以这样做,可以避免遍历系统所有用户
system_message_types.each do |system_message_type|
@message_alls << system_message_type.message
end
# 取出所有符合条件的按照创建时间排序
@message_alls = @message_alls.sort {|a,b| b.created_at <=> a.created_at}
when 'unviewed'
@message_alls = []
messages = MessageAll.where("user_id =?", @user).order("created_at desc")