Wrong count with acts_as_taggable
Reported by goodwill | January 6th, 2008 @ 12: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 January 6th, 2008 @ 12: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 January 7th, 2008 @ 04:29 AM
- → Title changed from will_paginate contains extra blank page at the end to Wrong count with acts_as_taggable
- → State changed from new to open
- → 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 meFor pasting, use pastie.caboo.se or attach files to this ticket.
-

joeconnor79 (at yahoo) January 9th, 2008 @ 06:01 AM
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 January 9th, 2008 @ 06:15 AM
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) January 9th, 2008 @ 06:21 AM
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 January 9th, 2008 @ 06:50 AM
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 January 9th, 2008 @ 07:14 AM
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
end
end
end
end
Then I have created this extension:
module ActiveRecordExtension
- 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")
end
end
end
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(
Post.find_options_for_find_tagged_with(
tag_string,
:match_all=>true
)
)
@news= Post.paginate_tagged_with(tag_string,
:match_all=>true,
:page=>params[:page],
:per_page=>2,
:order=>'timestamp DESC',
:total_entries=>total_entries)
render :template=>'pagestore/common/news/list'
end
end
Now the count is correct. I think these might help Mislav to fix the problem :)
-
Mislav January 31st, 2008 @ 02:17 PM
Thanks. I'll try to develop something that will make this easier for the user.
-

thorsten (at 80beans) April 14th, 2008 @ 07:41 AM
Hi,
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
Please Login or create a free account to add a new comment.
You can update this ticket by sending an email to from your email client. (help)
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
