Forum: Ruby on Rails acts_as_taggable query

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
6828ffc79486cd2442714bf32286a910?d=identicon&s=25 Vivek Krishna (Guest)
on 2005-12-28 05:47
(Received via mailing list)
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
6828ffc79486cd2442714bf32286a910?d=identicon&s=25 Vivek Krishna (Guest)
on 2005-12-28 15:01
(Received via mailing list)
anybody ??
19c9af375aca3001e3978cb16684803c?d=identicon&s=25 Parker Thompson (Guest)
on 2005-12-29 05:32
(Received via mailing list)
On 12/27/05, Vivek Krishna <krishna.vivek@gmail.com> 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_<taggable>.

>
>  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 Thompson
http://www.parkert.com/
510.541.0125
6828ffc79486cd2442714bf32286a910?d=identicon&s=25 Vivek Krishna (Guest)
on 2005-12-29 07:18
(Received via mailing list)
On 12/29/05, Parker Thompson <parkert@gmail.com> wrote:
> default tags_<taggable>.
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
This topic is locked and can not be replied to.