0

我似乎找不到正确的方法来设置在用户收件箱中显示的最大消息数,而不会破坏分页。我正在努力做到这一点,因此只有最后 100 条收件箱消息从最新到最旧显示。

消息控制器.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 %> 
4

3 回答 3

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

希望这会奏效

于 2013-07-17T14:01:43.530 回答
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

希望它会有所帮助。

于 2013-07-17T14:14:58.353 回答
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
于 2013-07-17T14:41:48.023 回答