限制收件箱中显示的总消息数

时间:2013-07-17 13:55:14

标签: ruby-on-rails ruby

我似乎找不到正确的方法来设置在用户收件箱中显示的最大邮件数量,而不会破坏分页。我正在努力使它只显示从最新到最旧的最后100个收件箱消息。

messages_controller.rb

class MessagesController < ApplicationController

def index
@messages = current_user.received_messages.paginate(:page => params[:page], :per_page =>    15,  :order => 'created_at DESC', )
end

使用will_paginate gem

<%= will_paginate @messages %> 

3 个答案:

答案 0 :(得分:1)

def index
@messages = current_user.received_messages.paginate(:page => params[:page], :per_page =>    15).order('created_at DESC').limit(100)
end

或尝试使用

def index
    @records = current_user.received_messages.order('created_at DESC').limit(100)
@messages = @records.paginate(:page => params[:page], :per_page =>    15)
    end

希望这会起作用

答案 1 :(得分:0)

但是首先实现active_records条件是一个好习惯,而不是分页。

def index
 @messages = current_user.received_messages.order(:created_at).reverse_order.limit(100).paginate(:page => params[:page], :per_page => 15)
end

希望它会有所帮助。

答案 2 :(得分:0)

之前我遇到过这个问题,我的修复是:

require 'will_paginate/array' # To paginate an array instead of ActiveRecord
class MessagesController < ApplicationController

def index
@messages = current_user.received_messages.limit(100).all.paginate(:page => params[:page], :per_page => 15,  :order => 'created_at DESC') # I transform the resultset to an array using .all before the paginate
end