{"id":140,"date":"2006-07-18T22:37:43","date_gmt":"2006-07-19T03:37:43","guid":{"rendered":"http:\/\/www.juixe.com\/techknow\/index.php\/2006\/07\/18\/acts-as-taggable-conditions-hack\/"},"modified":"2006-07-18T22:37:43","modified_gmt":"2006-07-19T03:37:43","slug":"acts-as-taggable-conditions-hack","status":"publish","type":"post","link":"http:\/\/juixe.com\/techknow\/index.php\/2006\/07\/18\/acts-as-taggable-conditions-hack\/","title":{"rendered":"Acts As Taggable Conditions Hack"},"content":{"rendered":"<p>I&#8217;ve been happily using the Ruby on Rails acts_as_taggable plugin for some time now.  But recently I had a situation that where I had to hack the plugin.  The taggable plugin provides a <b>find_tagged_with<\/b> method that is used to find all records for a given model with a certain tag.  Here is the typical usage of this method:<\/p>\n<pre>\n@post = Post.find_tagged_with ['tag1', 'tag2']\n<\/pre>\n<p>What I needed to do is add some conditions to this method.  I wanted to find all post tagged &#8216;tag1&#8217; or &#8216;tag2&#8217; that were created by a given user.  To do this I needed to hack the acts_as_taggable find_tagged_with method as follows:<\/p>\n<pre>\ndef find_tagged_with(list, options = {})\n  query = \"SELECT #{table_name}.* FROM #{table_name}, tags, taggings \" +\n    \"WHERE #{table_name}.#{primary_key} = taggings.taggable_id \" +\n    \"AND taggings.taggable_type = ? \" +\n    \"AND taggings.tag_id = tags.id AND tags.name IN (?) \"\n\n  arr = [query, acts_as_taggable_options[:taggable_type], list]\n\n  if options[:conditions] != nil\n    conditions = options[:conditions]\n    arr.first &lt;&lt; \" AND #{conditions.first}\"\n    arr = arr + conditions[1..conditions.size-1]\n  end\n\n  found = find_by_sql(arr)\n  found.uniq!\n  found\nend\n<\/pre>\n<p>With this code in place, I was able to do what I wanted like this:<\/p>\n<pre>\n@posts = Post.find_tagged_with params[:id],\n  :conditions =&gt; ['posts.user_id = ?', session[:user].id]\n<\/pre>\n<p>Technorati Tags: <a href=\"http:\/\/technorati.com\/tag\/ruby\" rel=\"tag\">ruby<\/a>, <a href=\"http:\/\/technorati.com\/tag\/rubyonrails\" rel=\"tag\"> rubyonrails<\/a>, <a href=\"http:\/\/technorati.com\/tag\/ruby+on+rails\" rel=\"tag\"> ruby on rails<\/a>, <a href=\"http:\/\/technorati.com\/tag\/rails+plugins\" rel=\"tag\"> rails plugins<\/a>, <a href=\"http:\/\/technorati.com\/tag\/acts_as_taggable\" rel=\"tag\"> acts_as_taggable<\/a>, <a href=\"http:\/\/technorati.com\/tag\/acts+as+taggable\" rel=\"tag\"> acts as taggable<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been happily using the Ruby on Rails acts_as_taggable plugin for some time now. But recently I had a situation that where I had to hack the plugin. The taggable plugin provides a find_tagged_with method that is used to find all records for a given model with a certain tag. Here is the typical usage [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":""},"categories":[22,3],"tags":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p902K-2g","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/140"}],"collection":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/comments?post=140"}],"version-history":[{"count":0,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/140\/revisions"}],"wp:attachment":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/media?parent=140"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/categories?post=140"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/tags?post=140"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}