# Acts\_as\_taggable query

**URL:** <https://www.ruby-forum.com/t/acts-as-taggable-query/51214>\
**Category:** Rails\
**Created:** [December 28, 2005, 5:47am UTC](https://www.ruby-forum.com/t/acts-as-taggable-query/51214 "2005-12-28T05:47:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vivek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/77aa72/32.png) [@vivek](https://www.ruby-forum.com/u/vivek)\
**Post date:** [December 28, 2005, 5:47am UTC](https://www.ruby-forum.com/t/acts-as-taggable-query/51214/1 "2005-12-28T05:47:34Z")

</div>

Hi ,  
I couldnt find any mailing list for this nice plugin so I hope this is  
not  
off topic.I just have a basic question here.  
I already had created a table and a model for the tags and items and the  
join table and now want to use this plugin but I just cant seem to  
figure  
out how to make it use my table instead of the default.

class Tag \< ActiveRecord::Base # already exists and has the required  
fields  
like name, id & an items (counter)  
end

class Item \< ActiveRecord::Base  
acts\_as\_taggable :join\_class\_name ‘Itemtag’ # should I use join\_table  
here?  
end

class Itemtag \<ActiveRecord::Base  
set\_table\_name ‘itemtags’ # note that this is different from the  
default  
conention of tags\_items  
end

With the above settings I thought that the plugin would use the table I  
have  
created but when I try to add a tag using the Taggable::tag instance  
method  
I get an error becuase the plugins still tries to use tags\_items table  
which doesnt exist. Is there anything else I need to do ?

Second, I had a little optimization in mind for doing the tag count so I  
added a field in the Tag table(which is named ‘tags’ as per the  
convention )  
called items .I increment this whenever an item is tagged with a  
particular  
tag,so that we can avoid an sql query for the count and jut return the  
value  
of this field in the record. I want to add some code in the taggable.rb  
to  
take care of this. The counter field has the same name as the table name  
of  
the taggable object (items)

def tag(tags, options = {})  
…

tag\_record = tag\_model.find\_by\_name(name) || tag\_model.new(:name =\>  
name)  
##here is my code  
tag\_record.table\_name +=1

Is this correct?  
Vivek

---

<div class="post-metadata">

**Author:** ![vivek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/77aa72/32.png) [@vivek](https://www.ruby-forum.com/u/vivek)\
**Post date:** [December 28, 2005, 3:01pm UTC](https://www.ruby-forum.com/t/acts-as-taggable-query/51214/2 "2005-12-28T15:01:28Z")

</div>

anybody ??

---

<div class="post-metadata">

**Author:** ![vivek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/77aa72/32.png) [@vivek](https://www.ruby-forum.com/u/vivek)\
**Post date:** [December 29, 2005, 5:32am UTC](https://www.ruby-forum.com/t/acts-as-taggable-query/51214/3 "2005-12-29T05:32:20Z")

</div>

On 12/27/05, Vivek K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I already had created a table and a model for the tags and items and the  
> join table and now want to use this plugin but I just cant seem to figure  
> out how to make it use my table instead of the default.

Look at the rdocs for acts\_as\_taggable, you can use the  
:join\_class\_name option to specify a model to use instead of the  
default tags\_.

> tag\_record = tag\_model.find\_by\_name(name) || tag\_model.new(:name =\> name)
> 
> ##here is my code  
> tag\_record.table\_name +=1

No. tag\_record refers to a record from the tags table, thus  
tag\_record.table\_name evaluates to tags.tags. You want to increment  
the join\_table.table\_name (or rather, what this evals to) method on  
the tag\_record table.

I’m not sure what the exact syntax is for this, but I recommend  
against it. It’ll require you wrap some business logic around it to  
keep your database accurate (introducing potential for problems) .  
For example, I believe there are several other places you’d have to  
modify the taggable library to decrement/change/reset this counter  
when tags are removed/cleared.

Further, it won’t significantly reduce the load on the db given all  
the queries Rails is already making. It’s probably better to cache  
data structures, pages, or page fragments (depending on your  
situation). If you’re doing something like displaying a list of gags  
with counts (e.g. cats (5)), this is a great candidate for page  
caching, for example. You can update every time something is tagged,  
or do it periodically if it isn’t critical that counts are always  
exact.

Hope that helps,

## pt.

Parker T.

> **[ParkErt.com is for sale | HugeDomains](https://www.hugedomains.com/domain_profile.cfm?d=parkert.com)**
>
> Join thousands of people who own a premium domain. Affordable financing available.

510.541.0125

---

<div class="post-metadata">

**Author:** ![vivek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/77aa72/32.png) [@vivek](https://www.ruby-forum.com/u/vivek)\
**Post date:** [December 29, 2005, 7:18am UTC](https://www.ruby-forum.com/t/acts-as-taggable-query/51214/4 "2005-12-29T07:18:47Z")

</div>

On 12/29/05, Parker T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> default tags\_.  
> I took care of this later by passing the join\_table parameter in  
> addition to  
> the join\_class\_name (the rdocs doesnt mention the join\_table as a param  
> but  
> I just figured it out by seeing the code.Anyway it works for me now!

No. tag\_record refers to a record from the tags table, thus

> tag\_record.table\_name evaluates to tags.tags. You want to increment  
> the join\_table.table\_name (or rather, what this evals to) method on  
> the tag\_record table.

The syntax is tricky …but I think this method executes in an instance  
of  
the tagged model (Item ) so I tried  
tag\_record[#{table\_name}] += 1.  
As you say,I guess the business logic may get a bit hairy so I am going  
to  
try the caching approach .But I would like to figure out the syntax for  
this.

Further, it won’t significantly reduce the load on the db given all

> the queries Rails is already making. It’s probably better to cache  
> data structures, pages, or page fragments (depending on your  
> situation). If you’re doing something like displaying a list of gags  
> with counts (e.g. cats (5)), this is a great candidate for page  
> caching, for example.

Actually this is what I am trying to do .I planned to show some html  
each  
time a user clicks a tag.This has some html and shows how many items  
have  
been tagged with this tag. I think I will now have to make this a  
seperate  
:partial in order to cache it. Still need to read up on the caching  
portion  
in Rails.  
Thanks
