Rails,ActiveRecord,& Kaminari为分页

时间:2017-09-01 17:11:28

标签: sql ruby-on-rails database activerecord

我有 www.messenger.com克隆。用户与其他用户进行了多次对话。对话有很多消息。

索引操作会按照我需要的正确顺序对邮件进行排序(最近一次在底部。)但是,它最初并没有给我最后一页。

### Sorted correct order/direction, but not last page. I need the last page of the pagination to be the 'first' page.
def index
  @conversations = current_user.sorted_conversations.page(params[:page])
  @conversation = @conversations.first
  @messages = @conversations.first.messages.order('created_at ASC').page(params[:page])
  respond_to do |format|
    format.html
    format.js
  end
end

想象一下,大多数聊天应用都会向上滚动以检索较旧的消息。我觉得这不是重复的,因为其他类似的问题涉及体操,我的老板/导师经常告诉我,如果我做了很多,那么我可能做错了。

我觉得通过数据库查询而不是使用其他帮助程序,这是一种更简单的方法。我也尝试使用.reverse解决它,但随后ActiveRecord抱怨

  

未定义的方法total_pages'为#

0 个答案:

没有答案