[英]Incorrect ordering of associated Rails model when using will_paginate
I'm having problems ordering with the will_paginate plugin. 我在订购will_paginate插件时遇到问题。 Here are my models
这是我的模特
Forum 论坛
class Forum < ActiveRecord::Base
has_many :topics, :dependent => :destroy
has_many :posts, :through => :topics
Topic 话题
class Topic < ActiveRecord::Base
belongs_to :forum, :counter_cache => true
has_many :posts, :dependent => :delete_all
Post 发布
class Post < ActiveRecord::Base
belongs_to :topic, :counter_cache => true
belongs_to :user
I'm trying to get the topics which have the most recent posts. 我正在尝试获取最新帖子的主题。 The following works correctly:
以下内容可以正常工作:
forum = Forum.find(3)
forum.topics.all(:include => [:posts], :order => "posts.created_at DESC")
But when introducing pagination (using the will_paginate plugin) the ordering is incorrect. 但是,在引入分页(使用will_paginate插件)时,顺序不正确。
forum = Forum.find(3)
forum.topics.paginate(:include => [:posts], :order => "posts.created_at DESC", :page => page)
Does anybody know why use of the will_paginate plugin could be detrimentally affecting ordering? 有人知道为什么使用will_paginate插件会有害地影响订购吗?
I'm using Rails 2.3.9 and will_paginate 1.6.2. 我正在使用Rails 2.3.9和will_paginate 1.6.2。
在Rails 3中,我认为您可以在分页调用之前将订单移至:
forum.topics.includes(:posts).order("posts.created_at DESC").paginate()
Do not use square brackets in :include . 不要在:include中使用方括号。
Also add :per _ page parameter: 还添加:per _ page参数:
forum = Forum.find(3)
forum.topics.paginate(
:per_page => 5,
:page => page,
:include => :posts,
:order => "posts.created_at DESC")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.