{"id":124,"date":"2006-06-24T10:48:07","date_gmt":"2006-06-24T15:48:07","guid":{"rendered":"http:\/\/www.juixe.com\/techknow\/index.php\/2006\/06\/24\/acts-as-voteable-rails-plugin\/"},"modified":"2006-06-24T10:48:07","modified_gmt":"2006-06-24T15:48:07","slug":"acts-as-voteable-rails-plugin","status":"publish","type":"post","link":"http:\/\/juixe.com\/techknow\/index.php\/2006\/06\/24\/acts-as-voteable-rails-plugin\/","title":{"rendered":"Acts As Voteable Rails Plugin"},"content":{"rendered":"<p>Right on the heels of my release of <a href=\"http:\/\/www.juixe.com\/techknow\/index.php\/2006\/06\/18\/acts-as-commentable-plugin\/\">acts_as_commentable<\/a>, I am now making available a acts_as_voteable Ruby on Rails plugin.  The Acts As Voteable plugin allows for model to be voted on by users.<\/p>\n<p>To install this plugin run the following command:<\/p>\n<pre>\nscript\/plugin install http:\/\/juixe.com\/svn\/acts_as_voteable\n<\/pre>\n<p>The installation process will add several ruby files 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>\ndef self.up\n  create_table :votes, :force =&gt; true do |t|\n    t.column :vote, :boolean, :default =&gt; false\n    t.column :created_at, :datetime, :null =&gt; false\n    t.column :voteable_type, :string, :limit =&gt; 15,\n      :default =&gt; \"\", :null =&gt; false\n    t.column :voteable_id, :integer, :default =&gt; 0, :null =&gt; false\n    t.column :user_id, :integer, :default =&gt; 0, :null =&gt; false\n  end\n\n  add_index :votes, [\"user_id\"], :name =&gt; \"fk_votes_user\"\nend\n\ndef self.down\n  drop_table :votes\nend\n<\/pre>\n<p>Once you have installed the plugin you can start using it in your ActiveRecord models simply by calling the acts_as_voteable method.<\/p>\n<pre>\nclass Post &lt; ActiveRecord::Base\n  acts_as_voteable\nend\n<\/pre>\n<p>To cast a vote for a post you can do the following:<\/p>\n<pre>\nvote = Vote.new(:vote =&gt; true)\npost = Post.find(params[:id])\npost.votes &lt;&lt; vote\n<\/pre>\n<p>ActiveRecord models that act as voteable can be queried for the positive votes, negative votes, and a total vote count by using the votes_for, votes_against, and votes_count methods respectively.  Here is an example:<\/p>\n<pre>\npositiveVoteCount = post.votes_for\nnegativeVoteCount = post.votes_against\ntotalVoteCount = post.votes_count\n<\/pre>\n<p>And because the Acts As Voteable plugin will add the has_many votes relationship to your model you can always get all the votes by using the votes property:<\/p>\n<pre>\nallVotes = post.votes\n<\/pre>\n<p>Technorati Tags: <a href=\"http:\/\/technorati.com\/tag\/ruby\" rel=\"tag\">ruby<\/a>, <a href=\"http:\/\/technorati.com\/tag\/rails\" rel=\"tag\"> rails<\/a>, <a href=\"http:\/\/technorati.com\/tag\/ruby+on+rails\" rel=\"tag\"> ruby on rails<\/a>, <a href=\"http:\/\/technorati.com\/tag\/plugin\" rel=\"tag\"> plugin<\/a>, <a href=\"http:\/\/technorati.com\/tag\/rails+plugin\" rel=\"tag\"> rails plugin<\/a>, <a href=\"http:\/\/technorati.com\/tag\/acts_as_commentable\" rel=\"tag\"> acts_as_commentable<\/a>, <a href=\"http:\/\/technorati.com\/tag\/acts_as_voteable\" rel=\"tag\"> acts_as_voteable<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Right on the heels of my release of acts_as_commentable, I am now making available a acts_as_voteable Ruby on Rails plugin. The Acts As Voteable plugin allows for model to be voted on by users. To install this plugin run the following command: script\/plugin install http:\/\/juixe.com\/svn\/acts_as_voteable The installation process will add several ruby files in the [&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-20","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/124"}],"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=124"}],"version-history":[{"count":0,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/124\/revisions"}],"wp:attachment":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/media?parent=124"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/categories?post=124"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/tags?post=124"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}