{"id":121,"date":"2006-06-18T14:08:00","date_gmt":"2006-06-18T19:08:00","guid":{"rendered":"http:\/\/www.juixe.com\/techknow\/index.php\/2006\/06\/18\/acts-as-commentable-plugin\/"},"modified":"2009-07-20T09:35:16","modified_gmt":"2009-07-20T16:35:16","slug":"acts-as-commentable-plugin","status":"publish","type":"post","link":"http:\/\/juixe.com\/techknow\/index.php\/2006\/06\/18\/acts-as-commentable-plugin\/","title":{"rendered":"Acts As Commentable Plugin"},"content":{"rendered":"<p>I am happy to announce that I have released the acts_as_commentable plugin, my first Ruby on Rails plugin.  The Acts As Commentable plugin allows for comments to be added to your Rails ActiveRecord classes.<\/p>\n<p>To install the Acts As Commentable plugin run the following command:<\/p>\n<pre>script\/plugin install http:\/\/juixe.com\/svn\/acts_as_commentable<\/pre>\n<p>The installation process will add several ruby scripts in the vendor\/plugins directory.  Create a new rails migration and cut and past the following self.up and self.down methods:<\/p>\n<pre class=\"brush: ruby; title: ; notranslate\" title=\"\">\r\ndef self.up\r\n  create_table :comments, :force =&gt; true do |t|\r\n    t.column :title, :string, :limit =&gt; 50, :default =&gt; &quot;&quot;\r\n    t.column :comment, :string, :default =&gt; &quot;&quot;\r\n    t.column :created_at, :datetime, :null =&gt; false\r\n    t.column :commentable_id, :integer, :default =&gt; 0, :null =&gt; false\r\n    t.column :commentable_type, :string, :limit =&gt; 15,\r\n      :default =&gt; &quot;&quot;, :null =&gt; false\r\n    t.column :user_id, :integer, :default =&gt; 0, :null =&gt; false\r\n  end\r\n\r\n  add_index :comments, &#x5B;&quot;user_id&quot;], :name =&gt; &quot;fk_comments_user&quot;\r\nend\r\n\r\ndef self.down\r\n  drop_table :comments\r\nend\r\n<\/pre>\n<p>Once you have installed the plugin you can start using it in your ActiveRecord class simply by calling the acts_as_commentable method.<\/p>\n<pre class=\"brush: ruby; title: ; notranslate\" title=\"\">\r\nclass Post &lt; ActiveRecord::Base\r\n  acts_as_commentable\r\nend\r\n<\/pre>\n<p>To add a comment to a post object you can do the following:<\/p>\n<pre class=\"brush: ruby; title: ; notranslate\" title=\"\">\r\ncomment = Comment.new(:title =&gt; titleStr, :comment =&gt; commentStr)\r\nlogger &lt;&lt; &quot;COMMENT #{comment.comment}\\n&quot;\r\npost.comments &lt;&lt; comment\r\n<\/pre>\n<p>Or you could have use the add_comment method on post.<\/p>\n<pre class=\"brush: ruby; title: ; notranslate\" title=\"\">\r\npost.add_comment comment\r\n<\/pre>\n<p>You can also use the post&#8217;s comments property to read all comments for the given post.  Once a comment has been added to a post you can always reference the post object using the comment&#8217;s commentable property.<\/p>\n<pre class=\"brush: ruby; title: ; notranslate\" title=\"\">\r\ncomment.commentable # references the post\r\n<\/pre>\n<p>One note, the default implementation of Acts As Commentable requires you to use a user model to link all comments to a user.  This requirement can easily be removed or enhanced in the Comment class.  But if you have a user model you can retrieve all comments for a user by executing the following statement:<\/p>\n<pre class=\"brush: ruby; title: ; notranslate\" title=\"\">\r\ncomments = Comment.find_comments_by_user(userInstance)\r\n<\/pre>\n<p>If you want to retrieve only the comments for a user for a particular model you can do something like:<\/p>\n<pre class=\"brush: ruby; title: ; notranslate\" title=\"\">\r\npostComments = Post.find_comments_by_user(userInstance)\r\n<\/pre>\n<p>If you have any comments, questions, and\/or suggestions please don&#8217;t hesitate to drop me a <a href=\"http:\/\/www.juixe.com\/contact.html\">line<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I am happy to announce that I have released the acts_as_commentable plugin, my first Ruby on Rails plugin. The Acts As Commentable plugin allows for comments to be added to your Rails ActiveRecord classes. To install the Acts As Commentable plugin run the following command: script\/plugin install http:\/\/juixe.com\/svn\/acts_as_commentable The installation process will add several ruby [&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-1X","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/121"}],"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=121"}],"version-history":[{"count":2,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/121\/revisions"}],"predecessor-version":[{"id":756,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/121\/revisions\/756"}],"wp:attachment":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/media?parent=121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/categories?post=121"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/tags?post=121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}