<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Slash Dot Dash: acts_as_taggable per user tagging</title>
    <link>http://www.slashdotdash.net/articles/2006/04/29/acts_as_taggable-per-user-tagging</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Rolling on Rails</description>
    <item>
      <title>acts_as_taggable per user tagging</title>
      <description>&lt;p&gt;David Heinemeier Hansson created the &lt;code&gt;acts_as_taggable&lt;/code&gt; Ruby on Rails plugin to allow tagging of any Active Record object. This is great for tagging objects application-wide, but I required user specific tags for my Rails app. Hopefully the following outlines the exact changes required to add the same functionality to your own Rails app &amp;#8211; post a comment if you have any problems.&lt;/p&gt;


	&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: There is also an &lt;code&gt;acts_as_taggable&lt;/code&gt; gem which is different than the plugin. The &lt;code&gt;acts_as_taggable&lt;/code&gt; plugin is only available for Rails 1.1.&lt;/p&gt;


	&lt;h3&gt;Install &lt;code&gt;acts_as_taggable&lt;/code&gt; plugin&lt;/h3&gt;


&lt;pre&gt;
script/plugin install acts_as_taggable
&lt;/pre&gt;

	&lt;h3&gt;Create database tables&lt;/h3&gt;


	&lt;p&gt;The following are mysql specific create table definitions:&lt;/p&gt;


&lt;pre&gt;
-- tags
DROP TABLE IF EXISTS tags;
CREATE TABLE IF NOT EXISTS tags (
  id int(11) unsigned NOT NULL auto_increment,
  name varchar(255) NOT NULL,
  created_at datetime NOT NULL,
  updated_at datetime NOT NULL,
  PRIMARY KEY (id)
) TYPE=InnoDB;

-- taggings
DROP TABLE IF EXISTS taggings;
CREATE TABLE taggings (
  id int(11) unsigned NOT NULL auto_increment,
  tag_id int(11) unsigned NOT NULL,
  taggable_id int(11) unsigned NOT NULL,
  taggable_type varchar(255),
  user_id int(11) unsigned NOT NULL,
  PRIMARY KEY (id)
) ENGINE=InnoDB;
&lt;/pre&gt;

	&lt;p&gt;The above taggings table differs from the normal &lt;code&gt;acts_as_taggable&lt;/code&gt; plugin requirement by adding a &lt;code&gt;user_id&lt;/code&gt; column to associate a tagging with an individual user. I am assuming that your Rails application already has a users table (I am using the login_engine to provide user authentication).&lt;/p&gt;


	&lt;h3&gt;Update Rails objects&lt;/h3&gt;


	&lt;p&gt;Add &lt;code&gt;acts_as_taggable&lt;/code&gt; to the class you want to be able to tag&lt;/p&gt;


&lt;pre&gt;
class ItemToTag &amp;lt; ActiveRecord::Base
  acts_as_taggable
end
&lt;/pre&gt;

&lt;pre&gt;
class User &amp;lt; ActiveRecord::Base  
  has_many :taggings
  has_many :tags, :through =&amp;gt; :taggings, :select =&amp;gt; "DISTINCT tags.*" 
end
&lt;/pre&gt;

	&lt;h3&gt;Modify &lt;code&gt;acts_as_taggable&lt;/code&gt; Plugin&lt;/h3&gt;


	&lt;p&gt;To add per-user taggings we need to modify the &lt;code&gt;acts_as_taggable&lt;/code&gt; plugin itself. First we add &lt;code&gt;belongs_to :user&lt;/code&gt; to associate a tagging with a user.&lt;/p&gt;


&lt;pre&gt;
class Tagging &amp;lt; ActiveRecord::Base
  belongs_to :tag
  belongs_to :taggable, :polymorphic =&amp;gt; true

  belongs_to :user  

  def self.tagged_class(taggable)
    ActiveRecord::Base.send(:class_name_of_active_record_descendant, taggable.class).to_s
  end

  def self.find_taggable(tagged_class, tagged_id)
    tagged_class.constantize.find(tagged_id)
  end
end
&lt;/pre&gt;

	&lt;p&gt;Next replace the existing &lt;code&gt;on&lt;/code&gt; method with the following in the Tag class (tag.rb file).&lt;/p&gt;


&lt;pre&gt;
def on(taggable, user)
  tagging = taggings.create :taggable =&amp;gt; taggable, :user =&amp;gt; user
end
&lt;/pre&gt;

	&lt;p&gt;Update acts_as_taggable.rb class by replacing each of the following methods with those given below:&lt;/p&gt;


&lt;pre&gt;
def find_tagged_with(list, user)
  find_by_sql([
    "SELECT #{table_name}.* FROM #{table_name}, tags, taggings " +
    "WHERE #{table_name}.#{primary_key} = taggings.taggable_id " +
    "AND taggings.user_id = ? " +
    "AND taggings.taggable_type = ? " +
    "AND taggings.tag_id = tags.id AND tags.name IN (?)",
    user.id, acts_as_taggable_options[:taggable_type], list
  ])
end
&lt;/pre&gt;

&lt;pre&gt;
def tag_with(list, user)
  Tag.transaction do
   Tagging.destroy_all(" taggable_id = #{self.id} and taggable_type = '#{self.class}' and user_id = #{user.id}") 

    Tag.parse(list).each do |name|
      if acts_as_taggable_options[:from]
        send(acts_as_taggable_options[:from]).tags.find_or_create_by_name(name).on(self, user)
      else
        Tag.find_or_create_by_name(name).on(self, user)
      end
    end
  end
end
&lt;/pre&gt;

&lt;pre&gt;
def tag_list
  tags.collect { |tag| tag.name.include?(" ") ? "'#{tag.name}'" : tag.name }.join(" ")
end
&lt;/pre&gt;

	&lt;h3&gt;Usage&lt;/h3&gt;


&lt;pre&gt;
item_to_tag.tag_with('ruby rails', current_user)
item_to_tag.save
item_to_tag.tag_list  # =&amp;gt; 'ruby rails'
current_user.tags     # =&amp;gt; 'ruby rails'
ItemToTag.find_tagged_with('ruby', current_user)
&lt;/pre&gt;

	&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: The tag_with is destructive, it deletes all previous tags.&lt;/p&gt;


	&lt;h4&gt;Update&lt;/h4&gt;


	&lt;p&gt;Previously the &lt;code&gt;tags&lt;/code&gt; collection for the User object would return &lt;span class="caps"&gt;ALL&lt;/span&gt; taggings (with duplicates if multiple objects had been tagged with the same tag). Thanks to &lt;a href="http://www.ianli.com/"&gt;Ian Li&lt;/a&gt; who kindly suggested the use of &lt;code&gt;:select =&amp;gt; "DISTINCT tags.*"&lt;/code&gt; for the tags &lt;code&gt;has_many&lt;/code&gt; relationship in the User class, duplicates are now filtered at the database.&lt;/p&gt;


	&lt;h4&gt;Update II&lt;/h4&gt;


	&lt;p&gt;I&amp;#8217;ve replaced the &lt;code&gt;taggings.destroy_all&lt;/code&gt; with the following in the &lt;code&gt;tag_with&lt;/code&gt; method. This is in response to feedback from Bryan and Anna regarding the destructive nature of the previous &lt;code&gt;tag_with&lt;/code&gt; method. Thanks for the comments.&lt;/p&gt;


&lt;pre&gt;
Tagging.destroy_all(" taggable_id = #{self.id} and taggable_type = '#{self.class}' and user_id = #{user.id}")
&lt;/pre&gt;

	&lt;p&gt;Since writing this post back in April 2006 I&amp;#8217;ve started a new project with slightly different tagging requirements, I&amp;#8217;ll create a new article when I&amp;#8217;m done and update the content here.&lt;/p&gt;


	&lt;h3&gt;References&lt;/h3&gt;


	&lt;p&gt;Pretty much all of this information came from the Ruby on Rails wiki and Rails Weenie so props to them.&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;&lt;a href="http://wiki.rubyonrails.com/rails/pages/ActsAsTaggablePluginHowto"&gt;ActsAsTaggablePluginHowto&lt;/a&gt;&lt;/li&gt;
		&lt;li&gt;&lt;a href="http://rails.techno-weenie.net/question/2006/3/6/how_to_add_user_to_acts_as_taggable"&gt;How to add user to acts_as_taggable&lt;/a&gt;&lt;/li&gt;
	&lt;/ul&gt;</description>
      <pubDate>Sat, 29 Apr 2006 14:58:00 +0100</pubDate>
      <guid isPermaLink="false">urn:uuid:7a15d47c30331ece218c33b727bc198a</guid>
      <author>ben@slashdotdash.net (Ben)</author>
      <link>http://www.slashdotdash.net/articles/2006/04/29/acts_as_taggable-per-user-tagging</link>
      <category>Ruby on Rails</category>
      <trackback:ping>http://www.slashdotdash.net/articles/trackback/12</trackback:ping>
    </item>
    <item>
      <title>"acts_as_taggable per user tagging" by Micah</title>
      <description>&lt;p&gt;Update: My blog has moved to a more permanent location.  The patch I mentioned can now be found here: &lt;a href="http://blog.aisleten.com/2007/02/25/acts_as_taggable-per-user-from-slashdotslash-plus-a-few-fixes/" rel="nofollow"&gt;http://blog.aisleten.com/2007/02/25/acts_as_taggable-per-user-from-slashdotslash-plus-a-few-fixes/&lt;/a&gt;&lt;/p&gt;</description>
      <pubDate>Sun, 22 Apr 2007 15:11:43 +0100</pubDate>
      <guid isPermaLink="false">urn:uuid:68764a3c-bf23-4422-8165-887144a8904c</guid>
      <link>http://www.slashdotdash.net/articles/2006/04/29/acts_as_taggable-per-user-tagging#comment-80</link>
    </item>
    <item>
      <title>"acts_as_taggable per user tagging" by anrake</title>
      <description>&lt;p&gt;How do I find all the instances of the model tagged by the current user?&lt;/p&gt;</description>
      <pubDate>Sat, 14 Apr 2007 23:47:38 +0100</pubDate>
      <guid isPermaLink="false">urn:uuid:354faaa9-535e-40b8-b3ba-bfc1540ecec2</guid>
      <link>http://www.slashdotdash.net/articles/2006/04/29/acts_as_taggable-per-user-tagging#comment-78</link>
    </item>
    <item>
      <title>"acts_as_taggable per user tagging" by Sean</title>
      <description>&lt;p&gt;I concur with RS.  I had the same problem and it works now with that fix.&lt;/p&gt;</description>
      <pubDate>Sun, 08 Apr 2007 20:13:01 +0100</pubDate>
      <guid isPermaLink="false">urn:uuid:09559aa9-36a1-47f8-ad04-0f96f9c79808</guid>
      <link>http://www.slashdotdash.net/articles/2006/04/29/acts_as_taggable-per-user-tagging#comment-76</link>
    </item>
    <item>
      <title>"acts_as_taggable per user tagging" by RS</title>
      <description>&lt;p&gt;I was getting this error:&lt;/p&gt;


	&lt;p&gt;ActiveRecord::AssociationTypeMismatch (User expected, got User):
 /vendor/plugins/acts_as_taggable/lib/tag.rb:31:in `on&amp;#8217;&lt;/p&gt;


	&lt;p&gt;Changed this line in tag.rb:&lt;/p&gt;


	&lt;p&gt;def on(taggable, user)
   tagging = taggings.create :taggable =&amp;gt; taggable, :user =&amp;gt; user
end&lt;/p&gt;


	&lt;p&gt;to:&lt;/p&gt;


	&lt;p&gt;def on(taggable, user)
   tagging = taggings.create :taggable =&amp;gt; taggable, :user_id =&amp;gt; user.id
end&lt;/p&gt;</description>
      <pubDate>Wed, 28 Mar 2007 19:20:17 +0100</pubDate>
      <guid isPermaLink="false">urn:uuid:a6a5d02e-13e0-459f-a7f3-c5052c867b24</guid>
      <link>http://www.slashdotdash.net/articles/2006/04/29/acts_as_taggable-per-user-tagging#comment-69</link>
    </item>
    <item>
      <title>"acts_as_taggable per user tagging" by Micah</title>
      <description>&lt;p&gt;I went ahead and summed up my changes to the plugin and created an SVN patch.  The patch will take you from the original (by DHH) to acts_as_taggable with user, plus some bug fixes as noted in these comments.&lt;/p&gt;


	&lt;p&gt;You can get it on my blog: &lt;a href="http://blog.obsidianportal.com/?p=6" rel="nofollow"&gt;Midnight Oil&lt;/a&gt;&lt;/p&gt;


	&lt;p&gt;NO WARRANTY!&lt;/p&gt;


	&lt;p&gt;Note, that&amp;#8217;s sort of a working title (and location) for my blog, so if it has gone 404 by the time you read this&amp;#8230;sorry :(&lt;/p&gt;</description>
      <pubDate>Mon, 26 Feb 2007 02:50:40 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:16a0e688-a2d5-4c69-8f70-8e87c668f81e</guid>
      <link>http://www.slashdotdash.net/articles/2006/04/29/acts_as_taggable-per-user-tagging#comment-56</link>
    </item>
    <item>
      <title>"acts_as_taggable per user tagging" by Micah</title>
      <description>&lt;p&gt;I fixed the problem.  All I had to change was:&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
Tagging.destroy_all(" taggable_id = #{self.id} and taggable_type = '#{self.class}' and user_id = #{user.id}") 
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;should be:&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
Tagging.destroy_all(" taggable_id = #{self.id} and taggable_type = '#{acts_as_taggable_options[:taggable_type]}' and user_id = #{user.id}") 
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;The acts_as_taggable plugin automatically uses the base class of a model as the taggable_type.  This makes sense because all the objects in the heirarchy will be in the same table, so their IDs will all be different.&lt;/p&gt;


	&lt;p&gt;(Sorry for double-post.  I tried to clean up the formatting a little)&lt;/p&gt;</description>
      <pubDate>Mon, 26 Feb 2007 01:22:16 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:8d2f5cb2-3354-4d11-9465-45fa68fc9f47</guid>
      <link>http://www.slashdotdash.net/articles/2006/04/29/acts_as_taggable-per-user-tagging#comment-55</link>
    </item>
    <item>
      <title>"acts_as_taggable per user tagging" by Micah</title>
      <description>&lt;p&gt;I fixed the problem.  All I had to change was:&lt;/p&gt;


&lt;code&gt;
Tagging.destroy_all(" taggable_id = #{self.id} and taggable_type = '#{self.class}' and user_id = #{user.id}") 
&lt;/code&gt;
should be:
&lt;code&gt;
Tagging.destroy_all(" taggable_id = #{self.id} and taggable_type = '#{acts_as_taggable_options[:taggable_type]}' and user_id = #{user.id}") 
&lt;/code&gt;

	&lt;p&gt;The acts_as_taggable plugin automatically uses the base class of a model as the taggable_type.  This makes sense because all the objects in the heirarchy will be in the same table, so their IDs will all be different.&lt;/p&gt;</description>
      <pubDate>Mon, 26 Feb 2007 01:21:09 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:ecd8f40a-0e3b-402b-a7b0-079c41352874</guid>
      <link>http://www.slashdotdash.net/articles/2006/04/29/acts_as_taggable-per-user-tagging#comment-54</link>
    </item>
    <item>
      <title>"acts_as_taggable per user tagging" by Micah</title>
      <description>&lt;p&gt;Note: I already tried moving &amp;#8220;acts_as_taggable&amp;#8221; down to B and removing it from A, but this changed nothing.&lt;/p&gt;</description>
      <pubDate>Sun, 25 Feb 2007 21:13:40 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:60992b30-1497-41a3-8aca-54bfa1b0086d</guid>
      <link>http://www.slashdotdash.net/articles/2006/04/29/acts_as_taggable-per-user-tagging#comment-53</link>
    </item>
    <item>
      <title>"acts_as_taggable per user tagging" by Micah</title>
      <description>&lt;p&gt;I am having problems with polymorphic models.  I have a class structure with A as the superclass and B as the subclass.  I add &amp;#8220;acts_as_taggable&amp;#8221; to A.  Everything works fine, but the Taggings table entries list all taggable_type as &amp;#8220;A&amp;#8221;, even if it is a &amp;#8220;B&amp;#8221; instance.&lt;/p&gt;


	&lt;p&gt;So, this is a problem when it goes to delete all the tags (ie. when someone adds a new set of tags).  The original tags are never found, since there is a type mismatch (ie. all tags are typed for the superclass, not the actual instance class).&lt;/p&gt;


	&lt;p&gt;Anyone else run into this?  Any solutions?&lt;/p&gt;</description>
      <pubDate>Sun, 25 Feb 2007 21:12:51 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:62010774-8fef-4c7e-bdf1-63ebc3313587</guid>
      <link>http://www.slashdotdash.net/articles/2006/04/29/acts_as_taggable-per-user-tagging#comment-52</link>
    </item>
    <item>
      <title>"acts_as_taggable per user tagging" by Micah</title>
      <description>&lt;p&gt;I just wanted to say thanks&amp;#8230;before I grabbed all this code and ran off laughing with it!&lt;/p&gt;


	&lt;p&gt;Anyways, this is exactly what I was looking for.  Someone should create acts_as_user_taggable or something from all this.&lt;/p&gt;


	&lt;p&gt;I&amp;#8217;d do it, but my svn repo is private.&lt;/p&gt;</description>
      <pubDate>Wed, 21 Feb 2007 02:44:45 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:98ffbf32-e1c1-47cb-a996-f439fdcaff79</guid>
      <link>http://www.slashdotdash.net/articles/2006/04/29/acts_as_taggable-per-user-tagging#comment-48</link>
    </item>
    <item>
      <title>"acts_as_taggable per user tagging" by Micah</title>
      <description>&lt;p&gt;I just wanted to say thanks&amp;#8230;before I grabbed all this code and ran off laughing with it!&lt;/p&gt;


	&lt;p&gt;Anyways, this is exactly what I was looking for.  Someone should create acts_as_user_taggable or something from all this.&lt;/p&gt;


	&lt;p&gt;I&amp;#8217;d do it, but my svn repo is private.&lt;/p&gt;</description>
      <pubDate>Wed, 21 Feb 2007 02:44:45 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:7e43256b-184c-488e-b3ab-90324a0a08fd</guid>
      <link>http://www.slashdotdash.net/articles/2006/04/29/acts_as_taggable-per-user-tagging#comment-49</link>
    </item>
    <item>
      <title>"acts_as_taggable per user tagging" by matt</title>
      <description>&lt;p&gt;If you are only tagging a single model, you can add this method to &lt;code&gt;acts_as_taggable.rb&lt;/code&gt; and skip all of the other modifications -&lt;/p&gt;


&lt;pre&gt;
def find_by_user_tagged_with(user, list)
  find_by_sql([
    "SELECT #{table_name}.* FROM #{table_name}, tags, taggings " +
    "WHERE #{table_name}.#{primary_key} = taggings.taggable_id " +
    "AND #{table_name}.user_id = ? " +
    "AND taggings.taggable_type = ? " +
    "AND taggings.tag_id = tags.id AND tags.name IN (?)",
    user.id, acts_as_taggable_options[:taggable_type], list
  ])
end
&lt;/pre&gt;

	&lt;p&gt;It assumes that you are assigning a &lt;code&gt;user_id&lt;/code&gt; to the model instances that you are tagging. I haven&amp;#8217;t tested it thoroughly, but it seems to work. It would probably be better to just extend the existing &lt;code&gt;find_tagged_with&lt;/code&gt; method along the lines of &lt;a href="http://www.railsweenie.com/forums/2/topics/699.&lt;/p" rel="nofollow"&gt;http://www.railsweenie.com/forums/2/topics/699.&lt;/a&gt;&lt;/p&gt;&gt;</description>
      <pubDate>Sun, 18 Feb 2007 03:38:02 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:92af07e3-19fd-4199-a4c6-4cccd9477bf9</guid>
      <link>http://www.slashdotdash.net/articles/2006/04/29/acts_as_taggable-per-user-tagging#comment-47</link>
    </item>
    <item>
      <title>"acts_as_taggable per user tagging" by matt</title>
      <description>&lt;p&gt;If you are only tagging a single model, you can add this method to &lt;code&gt;acts_as_taggable.rb&lt;/code&gt; and skip all of the other modifications -&lt;/p&gt;


&lt;pre&gt;
def find_by_user_tagged_with(user, list)
  find_by_sql([
    "SELECT #{table_name}.* FROM #{table_name}, tags, taggings " +
    "WHERE #{table_name}.#{primary_key} = taggings.taggable_id " +
    "AND #{table_name}.user_id = ? " +
    "AND taggings.taggable_type = ? " +
    "AND taggings.tag_id = tags.id AND tags.name IN (?)",
    user.id, acts_as_taggable_options[:taggable_type], list
  ])
end
&lt;/pre&gt;

	&lt;p&gt;It assumes that you are assigning a &lt;code&gt;user_id&lt;/code&gt; to the model instances that you are tagging. I haven&amp;#8217;t tested it thoroughly, but it seems to work. It would probably be better to just extend the existing &lt;code&gt;find_tagged_with&lt;/code&gt; method along the lines of &lt;a href="http://www.railsweenie.com/forums/2/topics/699.&lt;/p" rel="nofollow"&gt;http://www.railsweenie.com/forums/2/topics/699.&lt;/a&gt;&lt;/p&gt;&gt;</description>
      <pubDate>Sun, 18 Feb 2007 03:37:50 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:c0e5e99f-6db0-46ec-b4e2-3d53388096fc</guid>
      <link>http://www.slashdotdash.net/articles/2006/04/29/acts_as_taggable-per-user-tagging#comment-46</link>
    </item>
    <item>
      <title>"acts_as_taggable per user tagging" by justin</title>
      <description>&lt;p&gt;I get the &amp;#8216;User expected, got User&amp;#8217; error also.  Any idea why require &amp;#8216;model/user&amp;#8217; fixes this?&lt;/p&gt;</description>
      <pubDate>Thu, 15 Feb 2007 21:25:22 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:3f57ca32-e357-456b-80cc-8d8bafb49294</guid>
      <link>http://www.slashdotdash.net/articles/2006/04/29/acts_as_taggable-per-user-tagging#comment-43</link>
    </item>
    <item>
      <title>"acts_as_taggable per user tagging" by caili</title>
      <description>Got the solution. The user model has to be required explicit in every controller, or in the application controller. 
&lt;pre&gt;
require 'models/user'
&lt;/pre&gt;
Probably it is for all classes. Seems that 1.2.2 is got much stricter.</description>
      <pubDate>Wed, 14 Feb 2007 11:52:04 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:6c47768b-03c8-4d0c-88d0-3f99efb9fd66</guid>
      <link>http://www.slashdotdash.net/articles/2006/04/29/acts_as_taggable-per-user-tagging#comment-40</link>
    </item>
    <item>
      <title>"acts_as_taggable per user tagging" by caili</title>
      <description>&lt;p&gt;It all works very well with Rails 1.1.6 until I recently upgraded to Rail 1.2.2.&lt;/p&gt;


	&lt;p&gt;With 1.2.2, when I submit the item with the tags, it goes through ok on the first time that it&amp;#8217;s submitted when the server is turned on&amp;#8230; after that it keeps giving me a &amp;#8220;User expected, got User&amp;#8221; error.&lt;/p&gt;


	&lt;p&gt;Does anybody else also experienced this strange error?&lt;/p&gt;</description>
      <pubDate>Wed, 14 Feb 2007 10:11:15 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:b5982a6b-f5e9-4216-a5f3-be55a38e3a85</guid>
      <link>http://www.slashdotdash.net/articles/2006/04/29/acts_as_taggable-per-user-tagging#comment-39</link>
    </item>
    <item>
      <title>"acts_as_taggable per user tagging" by DT</title>
      <description>&lt;p&gt;Phil,&lt;/p&gt;


	&lt;p&gt;You&amp;#8217;re trying to create a tag on object that you haven&amp;#8217;t saved, thus you have no primary key on the model.&lt;/p&gt;</description>
      <pubDate>Wed, 07 Feb 2007 14:50:28 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:1f05b6f8-34cf-44c4-a76b-96da8c0c30ae</guid>
      <link>http://www.slashdotdash.net/articles/2006/04/29/acts_as_taggable-per-user-tagging#comment-38</link>
    </item>
    <item>
      <title>"acts_as_taggable per user tagging" by phil</title>
      <description>&lt;p&gt;could&amp;#8217;nt get this working on 1.2 keep getting a syntax mysql error&lt;/p&gt;


	&lt;p&gt;line 1: SELECT * FROM taggings WHERE ( taggable_id =  and taggable_type = &amp;#8216;TransAction&amp;#8217; and client_id = 1)&lt;/p&gt;


	&lt;p&gt;seems to be missing a taggable id&lt;/p&gt;</description>
      <pubDate>Sun, 04 Feb 2007 14:40:05 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:7e66fb59-cf7a-4320-a194-f2ae43b8ae2e</guid>
      <link>http://www.slashdotdash.net/articles/2006/04/29/acts_as_taggable-per-user-tagging#comment-36</link>
    </item>
    <item>
      <title>"acts_as_taggable per user tagging" by BryanDonovan</title>
      <description>&lt;p&gt;Thanks Anna.&lt;/p&gt;


One other thing I&amp;#8217;ve added is a &amp;#8220;tag_list_for_form&amp;#8221; method.  I may not really need this, but I found that listing the tags in a form text field wouldn&amp;#8217;t work with the current method because of the single-quotes.  So I modified mine slightly:
&lt;pre&gt;
&lt;code&gt;
 def tag_list_for_form
   tags.collect { |tag| tag.name.include?(" ") ? '"' + tag.name + '"' : tag.name }.join(" ")
 end
&lt;/code&gt;
&lt;/pre&gt;</description>
      <pubDate>Tue, 28 Nov 2006 17:16:15 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:3d7a4a00-5843-4269-acb8-98f4c8baf581</guid>
      <link>http://www.slashdotdash.net/articles/2006/04/29/acts_as_taggable-per-user-tagging#comment-17</link>
    </item>
    <item>
      <title>"acts_as_taggable per user tagging" by Anna</title>
      <description>&lt;p&gt;Ben,&lt;/p&gt;


	&lt;p&gt;Thanks to Bryan for pointing that glaring omission! :) cuz, I am just taggin&amp;#8217; one thing, that&amp;#8217;s why ;) BTW, nice site :)&lt;/p&gt;


	&lt;p&gt;Love,
Anna.&lt;/p&gt;</description>
      <pubDate>Mon, 27 Nov 2006 06:38:57 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:611d73a4-e4be-434f-83f9-f485564ec9f0</guid>
      <link>http://www.slashdotdash.net/articles/2006/04/29/acts_as_taggable-per-user-tagging#comment-16</link>
    </item>
    <item>
      <title>"acts_as_taggable per user tagging" by BryanDonovan</title>
      <description>&lt;p&gt;Ben, thanks for the post!  I was about to try to do something similar, but I&amp;#8217;m glad you already did the hard part for me..&lt;/p&gt;


	&lt;p&gt;Anna, I think in the destroy_all method you need to add the taggable_type as well, otherwise if a user tags objects of two or more models that happen to have the same id, the destroy_all method will destroy the objects from the other model as well.&lt;/p&gt;


I&amp;#8217;m not sure if this is the best way, but it seems to work:
&lt;pre&gt;
&lt;code&gt;
 Tagging.destroy_all(" taggable_id = #{self.id} and taggable_type = '#{self.class}' and user_id = #{user.id}")
&lt;/code&gt;
&lt;/pre&gt;
I also added a method to find tags on an object by a specific user (in the InstanceMethods module):
&lt;pre&gt;
&lt;code&gt;
 def tags_for_user(user)
     tags = Tag.find_by_sql([
     "SELECT tags.* FROM tags INNER JOIN taggings ON taggings.tag_id = tags.id " +
     "WHERE taggings.user_id = #{user.id} " +
     "AND taggable_id = #{self.id} " +
     "AND taggable_type = '#{self.class}'" 
     ]).collect { |tag| tag.name }.join(", ")
 end
&lt;/code&gt;
&lt;/pre&gt;
(note that returns a comma-separated list..)
As an example, I&amp;#8217;m going to use the above on my site (&lt;a href="http://urbandrinks.com" rel="nofollow"&gt;http://urbandrinks.com&lt;/a&gt;) to show a logged-in user which global tags for a particular place he or she also has tagged the place with.
Another small change I&amp;#8217;ve found useful is to include two versions of the &amp;#8220;find_tagged_with&amp;#8221; method.  Basically, I keep the original find_tagged_with method and create a second called find_tagged_with_by_user which uses Ben&amp;#8217;s method.  That way I can still select all the global tags by any user or use the new method to get the tags by a particular user.

	&lt;p&gt;Bryan&lt;/p&gt;</description>
      <pubDate>Sun, 26 Nov 2006 18:28:53 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:cde50650-c70e-42d4-a641-77251c2aff47</guid>
      <link>http://www.slashdotdash.net/articles/2006/04/29/acts_as_taggable-per-user-tagging#comment-15</link>
    </item>
    <item>
      <title>"acts_as_taggable per user tagging" by ben</title>
      <description>&lt;p&gt;Thanks for the comments Anna, I&amp;#8217;ll have a quick look at the changes and update the post appropriately.&lt;/p&gt;


	&lt;p&gt;Ben&lt;/p&gt;</description>
      <pubDate>Tue, 21 Nov 2006 17:27:21 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:e6d99eb1-ef4b-4503-9cfc-49b5e8dac0e0</guid>
      <link>http://www.slashdotdash.net/articles/2006/04/29/acts_as_taggable-per-user-tagging#comment-14</link>
    </item>
    <item>
      <title>"acts_as_taggable per user tagging" by Anna</title>
      <description>&lt;p&gt;People were telling me that this doesn&amp;#8217;t work. ok, well, the following should, since I have tested it and I swear :) its fine:&lt;/p&gt;


	&lt;p&gt;change in the infamous destroy_all to:&lt;/p&gt;


	&lt;p&gt;Tagging.destroy_all(&amp;#8221; taggable_id = #{self.id} and user_id = #{user.id} &amp;#8220;)&lt;/p&gt;


	&lt;p&gt;Love,
Anna.&lt;/p&gt;</description>
      <pubDate>Tue, 21 Nov 2006 05:44:39 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:81c3ef97-7ccc-400f-967d-441d4736a0a9</guid>
      <link>http://www.slashdotdash.net/articles/2006/04/29/acts_as_taggable-per-user-tagging#comment-13</link>
    </item>
    <item>
      <title>"acts_as_taggable per user tagging" by Anna</title>
      <description>&lt;p&gt;ok, sorry about the last entry, after some more thoughts, I have decided to remove that .destroy_all from act_as_taggable.rb altogether.&lt;/p&gt;


	&lt;p&gt;The problem, as I see it, should be fixed directly  in tag.rb, at the on method. Before we add any taggings, there should be a check first to make sure that there are no duplicates.&lt;/p&gt;


	&lt;p&gt;So in tag.rb, at the on method, replace it with:&lt;/p&gt;


	&lt;p&gt;taggings.find_or_create_by_tag_id_and_taggable_id_and_taggable_type_and_person_id(id, taggable.id, taggable.class.to_s, person.id)&lt;/p&gt;


	&lt;p&gt;This is totally UGLY, but works for now, until some Ruby guru can show this noob a more elegant way.&lt;/p&gt;</description>
      <pubDate>Tue, 21 Nov 2006 00:25:04 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:830414cb-4dda-4a6b-b01b-98adaa924e78</guid>
      <link>http://www.slashdotdash.net/articles/2006/04/29/acts_as_taggable-per-user-tagging#comment-12</link>
    </item>
    <item>
      <title>"acts_as_taggable per user tagging" by Anna</title>
      <description>&lt;p&gt;To avoid destroying everything automatically, you can change the tag_with method in act_as_taggable.rb.&lt;/p&gt;


	&lt;p&gt;instead of:
taggings.destroy_all&lt;/p&gt;


	&lt;p&gt;change to:
user.taggings.destroy_all&lt;/p&gt;


	&lt;p&gt;that should do the trick.&lt;/p&gt;</description>
      <pubDate>Mon, 20 Nov 2006 22:11:47 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:5f755b7c-ab41-4596-8926-14b90350e799</guid>
      <link>http://www.slashdotdash.net/articles/2006/04/29/acts_as_taggable-per-user-tagging#comment-11</link>
    </item>
  </channel>
</rss>
