Hi there, If you''re building a web application that uses tags, you might want to take a look here: http://dema.ruby.com.br/articles/2005/08/27/easy-tagging-with-rails (sorry about the previous typo on the subject line) Cheers, Dema -- http://dema.ruby.com.br - Rails from a .NET perspective
Hi, This was just in time delivery :) Was about to implement some stuff myself when you popped this one in. I have some issues though: I have set up a tables for the tags. Since I want to crosslink with only one other table (items) I need a table named tags_items, afaik. The docs are sparse in terms of how the db-schema is to be. This is how my related tables are set up: tags: id (int, pk, autogenerated) name (varchar 30) tags_items: id (int, pk, autogenerated) tag_id (with relation to tag.id) item_id (with relation to item.id) items: id (int, pk, autogenerated) ... I have also added a Tag model which looks like this: class Tag < ActiveRecord::Base has_and_belongs_to_many :tags_items end Things *almost* work perfectly. I did have one small issue that I fixed by adding a method named taglist to the taggable.rb file. It basically works like a helper to do simple output of the tag_names to the forms. (First time working on this level in ruby/rails. Cool stuff!) The real problem and reason for writing is that I am sometimes getting duplicate key errors: SQL (0.000000) #23000Duplicate entry ''1'' for key 1: INSERT INTO tags_items (`item_id`, `tag_id`, `id`) VALUES (''3'', ''1'', 1) It seems from this that the tags_items.id has been set to 1. I don''t know why it does so though. These are the observations done in regards to the above behavior: * If you edit an already existing item with tags, and specifies a tag that this item already has on it''s list, then it doesn''t have any problems. * If you add a tag that has never been used anywhere before, it also works. * If you add a tag that another item has already used, then this problem is observed. Shouldn''t the id have been false - or nil? Since the tags_items.id is a sequential autogenerated id (MySQL)? It''s a new record, so I can''t understand why it wants to set it? How do I fix this? Is there anything wrong in my setup, or is this related to the base code for acts_as_taggable? Or maybe something else stupid I am doing... :) Thanks anyway for a great tutorial in rails-hacking, and for a great extension! ------------------------------------------------------------------------ /*Ronny Hanssen*/ Demetrius Nunes wrote:> Hi there, > > If you''re building a web application that uses tags, you might want to > take a look here: > http://dema.ruby.com.br/articles/2005/08/27/easy-tagging-with-rails > > (sorry about the previous typo on the subject line) > > Cheers, > Dema >
Ronny, You don''t need to promote tags_items to a full model. It''s just a join table, unless you wanted user-specific tags or needed to occasionally update information in the item tag (e.g. a del.icio.us user-specified extended description). So I would have class Tag < ActiveRecord::Base has_and_belongs_to_many :items end and there''s a reciprocal habtm in the Item model. Can''t say this is exactly what Dema''s code expects since I''m currently using my own code (for per-user and general tags). You might want to look at Bob Aman''s nice TagTools project (http://rubyforge.org/projects/tagtools/), although that might be more than you need. Regards, Bill On 8/29/05, Ronny Hanssen <ronnyh-7tg5dEkr+6sdnm+yROfE0A@public.gmane.org> wrote:> Hi, > > This was just in time delivery :) Was about to implement some stuff > myself when you popped this one in. > > I have some issues though: > > I have set up a tables for the tags. Since I want to crosslink with only > one other table (items) I need a table named tags_items, afaik. The docs > are sparse in terms of how the db-schema is to be. This is how my > related tables are set up: > > tags: > id (int, pk, autogenerated) > name (varchar 30) > > tags_items: > id (int, pk, autogenerated) > tag_id (with relation to tag.id) > item_id (with relation to item.id) > > items: > id (int, pk, autogenerated) > ... > > I have also added a Tag model which looks like this: > > class Tag < ActiveRecord::Base > has_and_belongs_to_many :tags_items > end > > Things *almost* work perfectly. I did have one small issue that I fixed > by adding a method named taglist to the taggable.rb file. It basically > works like a helper to do simple output of the tag_names to the forms. > (First time working on this level in ruby/rails. Cool stuff!) > > The real problem and reason for writing is that I am sometimes getting > duplicate key errors: > SQL (0.000000) #23000Duplicate entry ''1'' for key 1: INSERT INTO > tags_items (`item_id`, `tag_id`, `id`) VALUES (''3'', ''1'', 1) > > It seems from this that the tags_items.id has been set to 1. I don''t > know why it does so though. > > These are the observations done in regards to the above behavior: > > * If you edit an already existing item with tags, and specifies a tag > that this item already has on it''s list, then it doesn''t have any problems. > * If you add a tag that has never been used anywhere before, it also works. > * If you add a tag that another item has already used, then this > problem is observed. > > Shouldn''t the id have been false - or nil? Since the tags_items.id is a > sequential autogenerated id (MySQL)? It''s a new record, so I can''t > understand why it wants to set it? > > How do I fix this? Is there anything wrong in my setup, or is this > related to the base code for acts_as_taggable? Or maybe something else > stupid I am doing... :) > > Thanks anyway for a great tutorial in rails-hacking, and for a great > extension! > > ------------------------------------------------------------------------ > /*Ronny Hanssen*/ > > > Demetrius Nunes wrote: > > Hi there, > > > > If you''re building a web application that uses tags, you might want to > > take a look here: > > http://dema.ruby.com.br/articles/2005/08/27/easy-tagging-with-rails > > > > (sorry about the previous typo on the subject line) > > > > Cheers, > > Dema > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Thanks. I am still new to this, but the essence here is to drop the id column of the join table. Is that it? Thanks for the tip about tagtools. I''ve checked it out, but I really don''t have the need to map users to the tags. Also, I can''t see that it has so many other features. At least not yet, but I couldn''t find any other documentation stating the future plans either. I may just be blind of course :) ------------------------------------------------------------------------ /*Ronny Hanssen*/ Bill Katz wrote:> Ronny, > > You don''t need to promote tags_items to a full model. It''s just a join > table, unless you wanted user-specific tags or needed to occasionally > update information in the item tag (e.g. a del.icio.us user-specified > extended description). > > So I would have > > class Tag < ActiveRecord::Base > has_and_belongs_to_many :items > end > > and there''s a reciprocal habtm in the Item model. Can''t say this is > exactly what Dema''s code expects since I''m currently using my own code > (for per-user and general tags). You might want to look at Bob Aman''s > nice TagTools project (http://rubyforge.org/projects/tagtools/), > although that might be more than you need. > > Regards, > Bill > > On 8/29/05, Ronny Hanssen <ronnyh-7tg5dEkr+6sdnm+yROfE0A@public.gmane.org> wrote: > >>Hi, >> >>This was just in time delivery :) Was about to implement some stuff >>myself when you popped this one in. >> >>I have some issues though: >> >>I have set up a tables for the tags. Since I want to crosslink with only >>one other table (items) I need a table named tags_items, afaik. The docs >>are sparse in terms of how the db-schema is to be. This is how my >>related tables are set up: >> >>tags: >> id (int, pk, autogenerated) >> name (varchar 30) >> >>tags_items: >> id (int, pk, autogenerated) >> tag_id (with relation to tag.id) >> item_id (with relation to item.id) >> >>items: >> id (int, pk, autogenerated) >> ... >> >>I have also added a Tag model which looks like this: >> >>class Tag < ActiveRecord::Base >> has_and_belongs_to_many :tags_items >>end >> >>Things *almost* work perfectly. I did have one small issue that I fixed >>by adding a method named taglist to the taggable.rb file. It basically >>works like a helper to do simple output of the tag_names to the forms. >>(First time working on this level in ruby/rails. Cool stuff!) >> >>The real problem and reason for writing is that I am sometimes getting >>duplicate key errors: >>SQL (0.000000) #23000Duplicate entry ''1'' for key 1: INSERT INTO >>tags_items (`item_id`, `tag_id`, `id`) VALUES (''3'', ''1'', 1) >> >>It seems from this that the tags_items.id has been set to 1. I don''t >>know why it does so though. >> >>These are the observations done in regards to the above behavior: >> >> * If you edit an already existing item with tags, and specifies a tag >>that this item already has on it''s list, then it doesn''t have any problems. >> * If you add a tag that has never been used anywhere before, it also works. >> * If you add a tag that another item has already used, then this >>problem is observed. >> >>Shouldn''t the id have been false - or nil? Since the tags_items.id is a >>sequential autogenerated id (MySQL)? It''s a new record, so I can''t >>understand why it wants to set it? >> >>How do I fix this? Is there anything wrong in my setup, or is this >>related to the base code for acts_as_taggable? Or maybe something else >>stupid I am doing... :) >> >>Thanks anyway for a great tutorial in rails-hacking, and for a great >>extension! >> >>------------------------------------------------------------------------ >>/*Ronny Hanssen*/ >> >> >>Demetrius Nunes wrote: >> >>>Hi there, >>> >>>If you''re building a web application that uses tags, you might want to >>>take a look here: >>>http://dema.ruby.com.br/articles/2005/08/27/easy-tagging-with-rails >>> >>>(sorry about the previous typo on the subject line) >>> >>>Cheers, >>>Dema >>> >> >>_______________________________________________ >>Rails mailing list >>Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>http://lists.rubyonrails.org/mailman/listinfo/rails >> > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Hm. I think I missed your real suggestion to change the relation for Tag. So the habtm reference in tag should go to items instead of tags_items.>From the sparse docs that was included I guessed that I was to usetags_items. It seems itself is setting up habtm: tags_items in the acts_as_taggable class. I was uncertain if I was to create the Tag class at all, but got an error without it. So I set it up. But I don''t know for sure what the habtm is to reference though. So basically: * Is the tags table supposed to have a id column? If not, what about pk? A combo of the two fks? * What habtm reference is supposed to set in the Tag model? ------------------------------------------------------------------------ /*Ronny Hanssen*/ Bill Katz wrote:> Ronny, > > You don''t need to promote tags_items to a full model. It''s just a join > table, unless you wanted user-specific tags or needed to occasionally > update information in the item tag (e.g. a del.icio.us user-specified > extended description). > > So I would have > > class Tag < ActiveRecord::Base > has_and_belongs_to_many :items > end > > and there''s a reciprocal habtm in the Item model. Can''t say this is > exactly what Dema''s code expects since I''m currently using my own code > (for per-user and general tags). You might want to look at Bob Aman''s > nice TagTools project (http://rubyforge.org/projects/tagtools/), > although that might be more than you need. > > Regards, > Bill > > On 8/29/05, Ronny Hanssen <ronnyh-7tg5dEkr+6sdnm+yROfE0A@public.gmane.org> wrote: > >>Hi, >> >>This was just in time delivery :) Was about to implement some stuff >>myself when you popped this one in. >> >>I have some issues though: >> >>I have set up a tables for the tags. Since I want to crosslink with only >>one other table (items) I need a table named tags_items, afaik. The docs >>are sparse in terms of how the db-schema is to be. This is how my >>related tables are set up: >> >>tags: >> id (int, pk, autogenerated) >> name (varchar 30) >> >>tags_items: >> id (int, pk, autogenerated) >> tag_id (with relation to tag.id) >> item_id (with relation to item.id) >> >>items: >> id (int, pk, autogenerated) >> ... >> >>I have also added a Tag model which looks like this: >> >>class Tag < ActiveRecord::Base >> has_and_belongs_to_many :tags_items >>end >> >>Things *almost* work perfectly. I did have one small issue that I fixed >>by adding a method named taglist to the taggable.rb file. It basically >>works like a helper to do simple output of the tag_names to the forms. >>(First time working on this level in ruby/rails. Cool stuff!) >> >>The real problem and reason for writing is that I am sometimes getting >>duplicate key errors: >>SQL (0.000000) #23000Duplicate entry ''1'' for key 1: INSERT INTO >>tags_items (`item_id`, `tag_id`, `id`) VALUES (''3'', ''1'', 1) >> >>It seems from this that the tags_items.id has been set to 1. I don''t >>know why it does so though. >> >>These are the observations done in regards to the above behavior: >> >> * If you edit an already existing item with tags, and specifies a tag >>that this item already has on it''s list, then it doesn''t have any problems. >> * If you add a tag that has never been used anywhere before, it also works. >> * If you add a tag that another item has already used, then this >>problem is observed. >> >>Shouldn''t the id have been false - or nil? Since the tags_items.id is a >>sequential autogenerated id (MySQL)? It''s a new record, so I can''t >>understand why it wants to set it? >> >>How do I fix this? Is there anything wrong in my setup, or is this >>related to the base code for acts_as_taggable? Or maybe something else >>stupid I am doing... :) >> >>Thanks anyway for a great tutorial in rails-hacking, and for a great >>extension! >> >>------------------------------------------------------------------------ >>/*Ronny Hanssen*/ >> >> >>Demetrius Nunes wrote: >> >>>Hi there, >>> >>>If you''re building a web application that uses tags, you might want to >>>take a look here: >>>http://dema.ruby.com.br/articles/2005/08/27/easy-tagging-with-rails >>> >>>(sorry about the previous typo on the subject line) >>> >>>Cheers, >>>Dema >>> >> >>_______________________________________________ >>Rails mailing list >>Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>http://lists.rubyonrails.org/mailman/listinfo/rails >> > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Demetrius Nunes
2005-Sep-01 01:38 UTC
Re: [ANN] acts_as_taggable : easy tagging with Rails
Ronny Hanssen wrote:> This was just in time delivery :) Was about to implement some stuff > myself when you popped this one in. > > I have some issues though: > > I have set up a tables for the tags. Since I want to crosslink with only > one other table (items) I need a table named tags_items, afaik. The docs > are sparse in terms of how the db-schema is to be. This is how my > related tables are set up: > > tags: > id (int, pk, autogenerated) > name (varchar 30) > > tags_items: > id (int, pk, autogenerated) > tag_id (with relation to tag.id) > item_id (with relation to item.id) > > items: > id (int, pk, autogenerated) > ... > The real problem and reason for writing is that I am sometimes getting > duplicate key errors: > SQL (0.000000) #23000Duplicate entry ''1'' for key 1: INSERT INTO > tags_items (`item_id`, `tag_id`, `id`) VALUES (''3'', ''1'', 1)Hi Ronny, just drop the ''id'' table from the ''tags_items'' table and you should be go to go. Also, expect a few more feature from the acts_as_taggable soon, as I will add *finders* and other interesting stuff. Cheers, Dema -- http://dema.ruby.com.br - Rails from a .NET perspective
To answer myself: Ronny Hanssen wrote:> Hm. I think I missed your real suggestion to change the relation for > Tag. So the habtm reference in tag should go to items instead of tags_items. > >>From the sparse docs that was included I guessed that I was to use > tags_items. It seems itself is setting up habtm: tags_items in the > acts_as_taggable class. > > I was uncertain if I was to create the Tag class at all, but got an > error without it. So I set it up. But I don''t know for sure what the > habtm is to reference though. > > So basically: > * Is the tags table supposed to have a id column? If not, what about > pk? A combo of the two fks?Seems the id column is to go. And I think that is normal for Rails'' join tables, right? I am just new to this yet... :)> * What habtm reference is supposed to set in the Tag model?This one I am not sure of. I guess it depends on how one want to use the tags collection. the habtm reference is basically only adding methods to the class. I haven''t figured it out yet, but I think I''ll try the tags_items solution first. ------------------------------------------------------------------------ /*Ronny Hanssen*/