Acts_as_taggable query


#1

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


#2

anybody ??


#3

On 12/27/05, Vivek K. 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.
http://www.parkert.com/
510.541.0125


#4

On 12/29/05, Parker T. 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