2011-07-25 11 views
0

will_paginateプラグインを使用して注文する際に問題があります。ここでは私のモデルは、私が最も最近の記事を持っているトピックを取得しようとしているwill_paginateを使用している場合、関連するRailsモデルの順序が正しくありません

フォーラム

class Forum < ActiveRecord::Base 
    has_many :topics, :dependent => :destroy 
    has_many :posts, :through => :topics 

トピック

class Topic < ActiveRecord::Base 
    belongs_to :forum, :counter_cache => true 
    has_many :posts, :dependent => :delete_all 

ポスト

class Post < ActiveRecord::Base 
    belongs_to :topic, :counter_cache => true 
    belongs_to :user 

です。以下は正常に動作します:

forum = Forum.find(3) 
forum.topics.all(:include => [:posts], :order => "posts.created_at DESC") 

しかし、(will_paginateプラグインを使用して)ページ設定を導入すると、順序が正しくありません。

forum = Forum.find(3) 
forum.topics.paginate(:include => [:posts], :order => "posts.created_at DESC", :page => page) 

will_paginateプラグインの使用は有害な秩序に影響を与えることができ、なぜ誰もが知っていますか?

私はRails 2.3.9を使用しており、1.6.2 will_paginateしています。 Railsの3で

+0

あなたは試したことがあります。include-INGのトピックページ付けで? – jaydel

+0

どのバージョンのレールですか? – DGM

+0

Railsを使用する2.3.9 – Alex

答えて

0

は、私はあなたがのpaginate呼び出し前に発注を移動することができると思います。

forum.topics.includes(:posts).order("posts.created_at DESC").paginate() 
+0

残念なことにRails 2.3.9を使用しています。これがRails 2でどのように達成されるのかご存じですか? – Alex

0

に角括弧を使用しないでください:が含まれます。
を追加します。 _ ページパラメータごと:

forum = Forum.find(3) 
forum.topics.paginate(
     :per_page => 5, 
     :page  => page, 
     :include => :posts, 
     :order => "posts.created_at DESC") 
+0

ありがとうございますが、私はこれらの変更が注文の問題を解決していないのではないかと心配しています – Alex

関連する問題