{"id":126,"date":"2006-07-05T08:14:25","date_gmt":"2006-07-05T13:14:25","guid":{"rendered":"http:\/\/www.juixe.com\/techknow\/index.php\/2006\/07\/05\/acts-as-rateable-plugin\/"},"modified":"2009-07-20T09:32:39","modified_gmt":"2009-07-20T16:32:39","slug":"acts-as-rateable-plugin","status":"publish","type":"post","link":"http:\/\/juixe.com\/techknow\/index.php\/2006\/07\/05\/acts-as-rateable-plugin\/","title":{"rendered":"Acts As Rateable Plugin"},"content":{"rendered":"<p>Continuing with my work with Ruby on Rails <strong>plugins<\/strong> here is my version of the acts_as_rateable plugin.  Use this plugin to make your model instances be rated by users.  The <strong>ratings<\/strong> are numeric and could be from 0 to any number you would like.  Typically you can rate articles, posts, reviews, etc from 0 to 5 but this plugin does not dictate the range.  To install this plugin run the following command:<\/p>\n<pre>script\/plugin install http:\/\/juixe.com\/svn\/acts_as_rateable<\/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 class=\"brush: ruby; title: ; notranslate\" title=\"\">\r\ndef self.up\r\n  create_table :ratings, :force =&gt; true do |t|\r\n    t.column :rating, :integer, :default =&gt; 0\r\n    t.column :created_at, :datetime, :null =&gt; false\r\n    t.column :rateable_type, :string, :limit =&gt; 15,\r\n      :default =&gt; &quot;&quot;, :null =&gt; false\r\n    t.column :rateable_id, :integer, :default =&gt; 0, :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 :ratings, &#x5B;&quot;user_id&quot;], :name =&gt; &quot;fk_ratings_user&quot;\r\nend\r\n\r\ndef self.down\r\n  drop_table :ratings\r\nend\r\n<\/pre>\n<p>Once you have the acts_as_rateable plugin installed you can make your ActiveRecord models act as rateable by calling the acts_as_rateable method.<\/p>\n<pre class=\"brush: ruby; title: ; notranslate\" title=\"\">\r\nclass Post &lt; ActiveRecord::Base\r\n  acts_as_rateable\r\nend\r\n<\/pre>\n<p>In your controller you can rate a post with just a few lines of code:<\/p>\n<pre class=\"brush: ruby; title: ; notranslate\" title=\"\">\r\npost = Post.find(params&#x5B;:id])\r\nrating = Rating.new(:rating =&gt; 2, :submitted =&gt; Time.new)\r\n@post.ratings &lt;&lt; rating\r\n<\/pre>\n<p>Similarly you can do this:<\/p>\n<pre class=\"brush: ruby; title: ; notranslate\" title=\"\">\r\npost = Post.find(params&#x5B;:id])\r\npost.add_rating Rating.new(:rating =&gt; 2)\r\n<\/pre>\n<p>And of course you can iterate through the ratings of a post by using the ratings property such as the following bit of code:<\/p>\n<pre class=\"brush: ruby; title: ; notranslate\" title=\"\">\r\npost.ratings.each { |r|\r\n  logger &lt;&lt; &quot;rating value: #{r.rating} for object: #{r.rateable}\\n&quot;\r\n}\r\n<\/pre>\n<p>You can also use the ratings property to get the number of ratings this post has been given.<\/p>\n<pre class=\"brush: ruby; title: ; notranslate\" title=\"\">\r\ncount = post.ratings.size\r\n<\/pre>\n<p>And if you want to display the average rating you can use the rating function.<\/p>\n<pre class=\"brush: ruby; title: ; notranslate\" title=\"\">\r\naverage_rating = post.rating\r\n<\/pre>\n<p>Stay tuned for more on Ruby on Rails plugins&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Continuing with my work with Ruby on Rails plugins here is my version of the acts_as_rateable plugin. Use this plugin to make your model instances be rated by users. The ratings are numeric and could be from 0 to any number you would like. Typically you can rate articles, posts, reviews, etc from 0 to [&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-22","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/126"}],"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=126"}],"version-history":[{"count":4,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/126\/revisions"}],"predecessor-version":[{"id":752,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/126\/revisions\/752"}],"wp:attachment":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/media?parent=126"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/categories?post=126"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/tags?post=126"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}