will_paginate with include re-ordering results
Rails 2.3.8 + will_paginate 2.3.14
I have both Product and ProductReview models.
Product
has_many :product_reviews, :dependent => :destroy
named_scope :most_recently_reviewed, :order => 'product_reviews.created_at desc', :include => [:product_reviews]
ProductReview
belongs_to :product, :counter_cache => true
Running the most basic query with and without pagination returns items in a completely different order.
Product.most_recently_reviewed.collect{|p| p.id }[0,9]
=> [开发者_开发技巧1660, 1658, 2374, 578, 1595, 135, 531, 550, 1511]
Product.most_recently_reviewed.paginate(:page => 1, :per_page => 40).collect{|p| p.id }[0,9]
=> [1660, 2374, 578, 1711, 1855, 1730, 1668, 1654, 2198]
Expanding per_page to a number greater than the number of products causes paginate to return the proper results:
Product.most_recently_reviewed.paginate(:page => 1, :per_page => 1000).collect{|p| p.id }[0,9]
=> [1660, 1658, 2374, 578, 1595, 135, 531, 550, 1511]
Any suggestions? Thanks.
Probably the limit from pagination is being applied to the results of the join, which are rows with multiplicity of the number of product reviews. I haven't found a better solution to this problem other than moving the scope criteria into the pagination call (you can then move the pagination call into the named scope). Named scopes just don't seem to work well with will_paginate.
精彩评论