Fork me on GitHub
Hoopla! - now with extra whiz-bang home

Clearly I’m loving scope out. It’s dramatically reduced the amount and complexity of code for my current app. Another plugin I’ve come to love is will_paginate because of the very Ruby-like way it allows paginating without any configuration.

Unfortunately these two plugins don’t quite work together well. Like Rails itself will_paginate does a fine job of finding the records but is less reliable for counting them.

Example:

    class Person < ActiveRecord::Base
      scope_out :women, :conditions => ["people.sex = ?", 'F']
    end
    35.times do
      Person.create(:sex => 'F')
    end
    14.times do
      Person.create(:sex => 'M')
    end
    @people = Person.paginate_women(:all, :page => 2)
    @people.size # => 4
    @people.total_entries # => 49
  

Whoops. That makes for some confusing pagination.

There’s a few possibilities for fixing this. My favorite is to hope that Mislav can apply this patch. But in the meantime here’s a quick way around it:

    module WillPaginate
      module Finder
        module ClassMethods
          def wp_count_with_scope!(options, args, finder)
            if respond_to?(scoper = finder.sub(/^find/, 'with'))
            	send(scoper) { wp_count_without_scope!(options, args, finder) }
            else
              wp_count_without_scope!(options, args, finder)
            end
          end
          alias_method_chain :wp_count!, :scope
        end
      end
    end
  
blog comments powered by Disqus