will_paginate weirdness and bugs
Posted: 2 years ago (2009-06-23 16:46:27 UTC ) / Updated: 2 years ago (2009-06-23 16:53:48 UTC )
I thought, "I've heard good things about will_paginate, I'll just drop it in and maybe it'll all work."
Well, mostly.
I'm using a custom :finder parameter, because I'm sometimes searching by tag. So I have a :tag option that I pass through to it. My finder looked like this:
class Post < ActiveRecord::Base
def self.tag_finder(options)
tag = options.delete :tag
if(tag)
self.tagged_with(tag, options)
else
self.all(options)
end
end
end
This doesn't work, because will_paginate passes the same options through to ActiveRecord's count function, and :tag isn't a valid option. There's a "pass options through to count" parameter for will_paginate, but that has several problems.
For starters, you can't use a custom counter function, so just not passing the tag through will still not give me an accurate count. It'll just call Post.count, which tells me only the count for all posts combined. So instead, I'm passing a total count through, and getting it with a database query. That's horrifically non-scalable, but it'll do until this blog is substantially larger than it is now.
Also, using a custom finder seems to screw up the :per_page parameter, so I have to set that manually again. That's not as bad as not having pagination, but it's a minor annoyance.
So, with will_paginate and my custom finder, the paging code looks like this:
options = { :order => 'posts.created_at DESC, posts.id DESC',
:page => @page, :readonly => true,
:tag => @tag, :finder => :tag_finder,
:total_entries => Post.tag_counter(@tag),
:per_page => PostsHelper::PostsPerPage }
@posts = Post.paginate(@posts)
Note that I also created a custom counter function. If will_paginate ever starts using it, maybe it'll be easy for me to just do that and get free scalability that way.
Still no view helpers -- I'm still using the manual, hacked-up pager at the bottom of the page. That'll be fixed next.
