Forum: Ruby on Rails Solution: Getting will_paginate and acts_as_taggable to play

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
181976602397443ce119909b9d82cae2?d=identicon&s=25 blinking bear (Guest)
on 2007-05-23 08:25
(Received via mailing list)
I'm using both will_paginate <> and
acts_as_taggable plugins on the app <> I'm
working on. The tagged results need to be paged just like other lists.
Here's my hack to get the two to work together:

RAILS_ROOT/lib/paginated_tags.rb in lib:

module ActiveRecord
  module Acts #:nodoc:
    module Taggable #:nodoc:
      module SingletonMethods

        def find_tagged_with(list,*args)
          #build the limit sql
          options = args.last.is_a?(Hash) ? args.pop.symbolize_keys : {}
          limit,offset = options.delete(:limit), options.delete(:offset)
unless options.empty?
          scope =  (limit && offset) ? "LIMIT #{offset}, #{limit}" : ""

            "SELECT #{table_name}.* FROM #{table_name}, tags, taggings "
            "WHERE #{table_name}.#{primary_key} = taggings.taggable_id "
            "AND taggings.taggable_type = ? " +
            "AND taggings.tag_id = AND IN (?)
            acts_as_taggable_options[:taggable_type], list

        #will_paginate will call find_all_tagged_with
        alias find_all_tagged_with find_tagged_with

I can now call a method like this:

@stories = Story.paginate_tagged_with("goo", :total_entries => 2,  :page

You have to manually pass in the total_entries, but hacking the code
total_entries is set in the will_paginate finder method could get around

- Nathan Verni
This topic is locked and can not be replied to.