# Need help using will\_paginate with tagging

**URL:** <https://www.ruby-forum.com/t/need-help-using-will-paginate-with-tagging/132694>\
**Category:** Rails\
**Created:** [March 23, 2008, 4:34pm UTC](https://www.ruby-forum.com/t/need-help-using-will-paginate-with-tagging/132694 "2008-03-23T16:34:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [March 23, 2008, 4:34pm UTC](https://www.ruby-forum.com/t/need-help-using-will-paginate-with-tagging/132694/1 "2008-03-23T16:34:50Z")

</div>

I have a simple tagging system:

places(id, title)  
taggings(id, place\_id, tag\_id)  
tags(id, title)

I can do the following sql to find places that are tagged as ‘cheap’  
AND tagged as ‘sushi’:

select \* from places inner join taggings on taggings.place\_id =  
places.id and taggings.tag\_id in (select id from tags where title =  
‘cheap’ or title = ‘sushi’) group by places.id having count(\*) = 2

I’m trying to do the same or similar query using will\_paginate - can  
someone help? This sort of works:

Place.paginate(:per\_page =\> 10, :page =\> 1, :include =\>  
[:taggings, :tags], :conditions =\> “taggings.tag\_id = tags.id and  
taggings.tag\_id in (select id from tags where title = ‘cheap’ or title  
= ‘sushi’)”, :group =\> “places.id having count(\*) = 2”)

The problem with this query is that if a certain tag does not exist I  
get a Mysql error. For example if someone searches with:

Place.paginate(:per\_page =\> 10, :page =\> 1, :include =\>  
[:taggings, :tags], :conditions =\> “taggings.tag\_id = tags.id and  
taggings.tag\_id in (select id from tags where title = ‘cheap’ or title  
= ‘asdfsadf’)”, :group =\> “places.id having count(\*) = 2”)

It gives, ActiveRecord::StatementInvalid: Mysql::Error: You have an  
error in your SQL syntax; check the manual that corresponds to your  
MySQL server version for the right syntax to use near 'having count(\*)  
= 2

---

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [March 24, 2008, 3:35am UTC](https://www.ruby-forum.com/t/need-help-using-will-paginate-with-tagging/132694/2 "2008-03-24T03:35:28Z")

</div>

Vince,

I think you want to do a join like this

Articles.find(:all, :conditions =\> …,  
:joins =\> “LEFT JOIN authors ON articles.author\_id=authors.id”)

thank you can you can use those table(s) in your :conditions

Arshak

vince wrote:

> I have a simple tagging system:
> 
> places(id, title)  
> taggings(id, place\_id, tag\_id)  
> tags(id, title)
> 
> I can do the following sql to find places that are tagged as ‘cheap’  
> AND tagged as ‘sushi’:
> 
> select \* from places inner join taggings on taggings.place\_id =  
> places.id and taggings.tag\_id in (select id from tags where title =  
> ‘cheap’ or title = ‘sushi’) group by places.id having count(\*) = 2
> 
> I’m trying to do the same or similar query using will\_paginate - can  
> someone help? This sort of works:
> 
> Place.paginate(:per\_page =\> 10, :page =\> 1, :include =\>  
> [:taggings, :tags], :conditions =\> “taggings.tag\_id = tags.id and  
> taggings.tag\_id in (select id from tags where title = ‘cheap’ or title  
> = ‘sushi’)”, :group =\> “places.id having count(\*) = 2”)
> 
> The problem with this query is that if a certain tag does not exist I  
> get a Mysql error. For example if someone searches with:
> 
> Place.paginate(:per\_page =\> 10, :page =\> 1, :include =\>  
> [:taggings, :tags], :conditions =\> “taggings.tag\_id = tags.id and  
> taggings.tag\_id in (select id from tags where title = ‘cheap’ or title  
> = ‘asdfsadf’)”, :group =\> “places.id having count(\*) = 2”)
> 
> It gives, ActiveRecord::StatementInvalid: Mysql::Error: You have an  
> error in your SQL syntax; check the manual that corresponds to your  
> MySQL server version for the right syntax to use near 'having count(\*)  
> = 2
