#175 ✓resolved

Wrong count with acts_as_taggable

Reported by goodwill | January 6th, 2008 @ 09:20 AM

When I tried to use paginate with acts_as_taggable (on steroids), I have modified the code to allow me to put :match_all into the parameter list. I think its actually not related to the code I have modified, but the page result is always 2 despite I just have 2 record (I set the page size to 10 using :per_page).

Comments and changes to this ticket

  • goodwill

    goodwill January 6th, 2008 @ 09:30 AM

    I have played with that a while and realize the count is wrong, thats why it doesn't work. How come the records are fetched correctly but the count is wrong?

    My records are as follows:

    post1 [tags: en, news]

    post2 [tags: en, news]

    Thats all I got, and the end count is 12- I found out by changing per_page to 1 and realize there are 12 pages generated.

  • Mislav

    Mislav January 7th, 2008 @ 01:29 PM

    • State changed from “new” to “open”
    • Title changed from “will_paginate contains extra blank page at the end” to “Wrong count with acts_as_taggable”
    • Assigned user changed from “Chris Wanstrath” to “Mislav”

    Would you be kind enough to share the code you're using, too? ;-P

    I need the call to paginate finder and your modified code for the extra parameter. Also, do the following:

    1. rake log:clear
    2. script/console
    3. call the paginating finder on Post (paste it from the controller)
    4. exit
    5. go to development log, find generated SQL and paste it to me

    For pasting, use pastie.caboo.se or attach files to this ticket.

  • joeconnor79 (at yahoo)

    joeconnor79 (at yahoo) January 9th, 2008 @ 03:01 PM

    I also have this problem. The last 2 SQL queries from my db are:

    SELECT DISTINCT users.* FROM `users` INNER JOIN taggings users_taggings ON users_taggings.taggable_id = users.id AND users_taggings.taggable_type = 'User' INNER JOIN tags users_tags ON users_tags.id = users_taggings.tag_id WHERE ((users_tags.name LIKE '%ms%')) ORDER BY users.created_at DESC LIMIT 0, 20

    SELECT count(*) AS count_all FROM `users`

    So it looks like the count is not applying the same conditions as the search, which is why we are getting the wrong result for total_entries and this means that too many pagination links are generated.

    Hope that helps!

  • Mislav

    Mislav January 9th, 2008 @ 03:15 PM

    Joe: please tell me what exact plugin for tags do you use and how are you

    calling the paginating finder in your controller. Thanks!

    On Jan 9, 2008 3:01 PM, Lighthouse wrote:

  • joeconnor79 (at yahoo)

    joeconnor79 (at yahoo) January 9th, 2008 @ 03:21 PM

    Using acts_as_taggable_on_steroids (r351)

    Code in the controller is

    @users = User.paginate_tagged_with(params[:tags], :page => params[:page], :order => get_sort_options(params[:sort]))

    One tiny change in aatos, i added % around the search query so it matches partial search terms at line 141. Shouldn't affect anything else though...

  • Mislav

    Mislav January 9th, 2008 @ 03:50 PM

    OK thanks. This isn't a bug, it just doesn't work right now. I'll try

    to get the support up for acts_as_taggable and similar this evening -

    I've been planing to do so for a long time.

  • goodwill

    goodwill January 9th, 2008 @ 04:14 PM

    Exactly thats the problem.

    I have did a hack to make it work somehow.

    First define an alias to find_tagged_with:

    module ActiveRecord

    module Acts #:nodoc:

    module Taggable #:nodoc:

    module SingletonMethods

    alias find_all_tagged_with find_tagged_with





    Then I have created this extension:

    module ActiveRecordExtension

    1. use sql options to generate a count statement and execute

    module ClassMethods

    def count_by_options(options)

    sql_statement= send("construct_finder_sql", options)

    count_by_sql("SELECT COUNT(*) FROM (#{sql_statement}) RESULT")




    Using constuct_finder_sql make sure I generate the same sql as the fetcher going to use. Then I just did a simple warp with Select Count(*).

    Finally I use this code to make sure the pagination is correct:

    def list

    tag_string="#{@locale}, news"

    total_entries= Post.count_by_options(






    @news= Post.paginate_tagged_with(tag_string,




    :order=>'timestamp DESC',


    render :template=>'pagestore/common/news/list'



    Now the count is correct. I think these might help Mislav to fix the problem :)

  • Mislav

    Mislav January 31st, 2008 @ 11:17 PM

    Thanks. I'll try to develop something that will make this easier for the user.

  • thorsten (at 80beans)

    thorsten (at 80beans) April 14th, 2008 @ 04:41 PM


    i thought i put this here, since the error is most likely the same:

    when using paginate_with_ferret the page_count at least for the first

    page is wrong.

    in the log i find this:

    SELECT count(*) AS count_all FROM `descriptions

    which seems to be used for the result, but of course includes all

    records in the table, not only those returned by ferret

  • Mark Reginald James

    Mark Reginald James November 24th, 2010 @ 07:20 AM

    • Tag set to count, sql, will_paginate
    • Milestone order changed from “0” to “0”

    This comment contains a way to make will_paginate work properly with the acts_as_taggable_on_steroids plugin:

    Here are the changes I applied so that paginate_tagged_with returns the correct total_entries count, and also accepts the :match_all option (tag AND rather than tag OR). Both changes are in finder.rb:

    1. in the paginate method, comment out "args << find_options", and replace the finder send argument with "*(args.dup << find_options)". This ensures that the args parameter to wp_count doesn't include the options hash.

    2. In the wp_count method, change the custom scope invocation from "send(scoper, &counter)" to "send(scoper, *(args << count_options)) { all.count }". This allows the scoper to process its own options. (This needs to be made more efficient by setting the appropriate :select option inside the scope.)

    In the acts_as_taggable_on_steroids code, the scope method in question then needs to be added to acts_as_taggable.rb:

    def with_tagged_with(*args)
      with_scope(:find => find_options_for_find_tagged_with(*args)) { yield }

    It's nice that will_paginate was flexible enough to accommodate this.

  • Mislav

    Mislav September 17th, 2011 @ 05:06 PM

    • State changed from “open” to “resolved”

    (from [1c3e836e597b4c936bc23843d09915f92663a665]) Active Record 3.0: preserve pagination after scoped call

    AR 3.1 is not affected.

    Fixes #175

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

Everyone's favorite Ruby library for pagination of practically anything!

Referenced by