Acts As Taggable On Grows Up

Acts As Taggable On (original post here), the tagging plugin with custom tag contexts, has gathered up some great new features over the past weeks thanks to the efforts of the community as well as fellow Intrideans Pradeep Elankumaran and Brendan Lim. I just wanted to take this opportunity to go over some of what’s new and interesting in the world of acts_as_taggable_on.

Community Fixes

First, Peter Cooper was kind enough to submit a patch that allows acts_as_taggable_on to work with Rails 2.1’s named_scope when using find_options_for_tag_counts.

Secondly, the much requested support for Single Table Inheritance is finally in! It was just a matter of using a class inheritable attribute instead of a class instance variable, and big thanks to slainer68 for hunting that down and taking the time to submit a patch.

If there’s anything you’ve hacked on to Acts As Taggable On, I urge you to submit a patch to the Lighthouse Project. I try to get new patches integrated into the codebase as quickly as possible, so please do submit anything!

During the Community Code Drive at RailsConf two great features were added: taggers and related objects.

Taggers

Tags can now have ownership, allowing for such things as User-tracked tags and more. This was a requested feature and something that I’d been looking forward to myself. Here’s the usage:

class User < ActiveRecord::Base
  acts_as_tagger
end

class Photo < ActiveRecord::Base
  acts_as_taggable_on :locations
end

@some_user.tag(@some_photo, :with => "paris, normandy", :on => :locations)
@some_user.owned_taggings
@some_user.owned_tags
@some_photo.locations_from(@some_user)

Find Related

Another request (and another great idea) is the ability to find related objects by similar tags. This is now available through the @object.find_related_on_tags syntax:

@bobby = User.find_by_name("Bobby")
@bobby.skill_list # => ["jogging", "diving"]

@frankie = User.find_by_name("Frankie")
@frankie.skill_list # => ["hacking"]

@tom = User.find_by_name("Tom")
@tom.skill_list # => ["hacking", "jogging", "diving"]

@tom.find_related_on_skills # => [<User name="Bobby">,<User name="Frankie">]
@bobby.find_related_on_skills # => [<User name="Tom">] 
@frankie.find_related_on_skills # => [<User name="Tom">] 

Gemified!

Acts As Taggable On now works as a GemPlugin in Rails. This is a new way (as of Rails 2.1) of distributing plugins as gems and having them still automatically link up and do their magic. To use it as a gem, add it to your config/environment.rb like so:

config.gem "mbleigh-acts-as-taggable-on", :source => "http://gems.github.com", :lib => "acts-as-taggable-on"

Now you should be able to get the latest version of the plugin just by running rake gems:install. However, this hasn’t been working for me so the alternative is just to install the gem directly:

gem install mbleigh-acts-as-taggable-on --source http://gems.github.com/

Now when you run your Rails app, even though it’s not in vendor/plugins it should be running! To make sure, look for this line on startup:

** acts_as_taggable_on: initialized properly

There are still a couple of issues outstanding in Rails regarding GemPlugins (if you unpack it, it will not run the initialization properly for some reason), but I wanted to give everyone the latest and greatest way to install the plugin possible. It will still work fine using the conventional methods as well.

Community and Future

I’ve been really happy with the response and support of the community, and I would like to do everything possible to cultivate future participation. To that end, I have created an Acts As Community Project for acts_as_taggable_on that will hopefully provide some casual communication about the project. Feel free to post on the wall or in the forums, and look out for additions soon.

Finally, the area of the plugin that still needs some work is tag caching. This is not a particular area of my expertise, so I’m hoping that someone from the community will write up some specs that flesh out the caching functionality in new and interesting ways.

Thanks for all of the patches, and I hope you continue to enjoy using Acts As Taggable On!

UPDATE (6/10/08): The improvements keep on rolling! After writing the post, I went off on a tangent and decided to make the plugin work both traditionally and as a gem. See more details above in the “Gemified” section.

Share:

16 Responses to “Acts As Taggable On Grows Up”

  1. Jim Neath

    Looks great, guys. I've been looking for a better tagging solution for a while and acts_as_taggable_on seems to fit all the criteria. Keep up the good work.
  2. Heiopei

    Thanks a lot. I had been using has_many_polymorphs up to now but this is a much more elegant solution. The possibility to "own" tags is what I was missing in h_m_p Ze guys from intridea uber rocken! Greetings Benjamin
  3. joost

    I am using this plugin and so far very happy with it. Is it possible to use named_scope with the .find_tagged_with method? I can't seem to get that to work. I am assuming Peter Cooper's fix doesn't apply to the finder method. What I would like is: Content.published.find_tagged_with('homepage') where published is a named_scope on Content.
  4. John

    This is great. I was using acts_as_taggable gem but unfortunately it is no longer being maintained. Anyway, when using find_related_on_tags method I ran into the following problem in PostgrSQL 8.3 under Debian, ActiveRecord::StatementInvalid: PGError: ERROR: column "products.itemno" must appear in the GROUP BY clause or be used in an aggregate function : SELECT products.*, COUNT(tags.id) AS count FROM products, tags, taggings WHERE (products.id = taggings.taggable_id AND taggings.taggable_type = 'Product' AND taggings.tag_id = tags.id AND tags.name IN (E'Tag1',E'Tag2')) GROUP BY products.id ORDER BY count DESC I had similar problem with act_as_taggable gem. Somehow when the SQL statement when changed from "SELECT products.* ..." to "SELECT products.id ..." seems to work for me. It takes an extra step to first search for id then products through the id but that's the only way I got it to work for me. I don't know if the error is self induced or if it related to how Postgres handle SELECT statements. Anyone else ran into this problem when using Postgres with find_related_on? Thanks, John
  5. Brian Johnson

    I modified the previous version to support ordered tags. It was pretty simple to do, but I haven't looked at the updated code to see how it would fit in. It would be a nice feature to add. We have an application with various tagging contexts, but the tags need to be ranked in order of importance. I used acts_as_list and added a position field to taggings and set them whenever the list was updated. Also, I'm having a strange issue with the gem. The gem is installed, and I have the config.gem in my environment.rb, but I'm not seeing it initialize when my app starts up and I'm getting an error trying to use acts_as_taggable: undefined method `acts_as_taggable_on' for #<class:0x4067670> My understanding is with config.gem "require" is no longer required, so I can't figure out what I'm missing. In environment.rb: config.gem "will_paginate" config.gem "has_many_polymorphs" config.gem "mbleigh-acts-as-taggable-on", :source => "http://gems.github.com", :lib => "acts-as-taggable-on" Thanks
  6. Peter Jackson

    Not sure what isn't working for you with the config.gem statement. The issue with config.gem for me is that rails/init.rb is failing to run when I freeze the gem. If I leave the gem installed but not frozen, the initializer is invoked. If I have them gem installed and frozen, then the system gem's initializer is run, not the frozen initializer. There's an open Rails ticket at Lighthouse which describes the issue. Looks like the 2.1.1 release will fix it. http://rails.lighthouseapp.com/projects/8994/tickets/324-gem-dependencies-inconsistently-load-rails-init-rb
  7. Peter Jackson

    I was having trouble with space-delimited fields coming in from the "edit" form creating multi-word tags in the database. I worked around it like this.
  8. Peter Jackson

    Ha. Or you can do it the easy way:
    TagList.delimiter = " "
    My bad. :)
  9. Roy Quader

    I'm using acts_as_taggable_on on my site but I had so much trouble getting it to work with will_paginate that I ended up not paginating tag-search results. Does the newer version work better with will_paginate?
  10. Steve Martocci

    Anyone using nested named scopes successfully? User.tagged_with("Foo", :on => :skills).tagged_with("Bar", :on => :interests) That function is returning me an empty array. When each call is done on their own they both return the same two results. Steve
  11. Henry

    I am facing the same problem with nested named scopes, any luck?
  12. tashfeen

    i could not get the config.gem so installed the gem directly. however, how do i get my app to recognize the gem? i hougt you could just do a require "acts_as_taggable_on" in the enviroment but that does nto work.
  13. Michael Bleigh

    @tashfeen: you need the config.gem because that is how Rails knows to run the initialization script. Without a config.gem working, I'd suggest installing it as a traditional plugin.
  14. tashfeen

    thanks for the reply! so, i am workin on a windows machine and can not grab for some strange reason any plugins with git eventhough it is available from the command ine. it just wont do it. :( do you have repository at a non-git site?
  15. tashfeen

    so, i installed the will_paginate plugin as a gem and then just included require "will_painate" in the environmnet. i was hoping to get away with that. the weird thing is that i was still able to do the db migration. so, do i just need a require statement?
  16. Paul

    Great plugin/gem! I quickly realised however there is no support for querying tags across different models. I have at least 3 different models using AATO currently, and I have to manually combine results into an array, or write custom SQL (union statement) to display "everything" tagged. Any suggestions for achieving this using the current code? ALSO for Tashfeen (above) or anyone else trying to install on a Windows machine from Git Hub, remember to add a trailing slash to the URL like this: ruby script/plugin install git://github.com/mbleigh/acts-as-taggable-on.git/


Leave a Reply