{"id":130,"date":"2006-07-05T08:21:02","date_gmt":"2006-07-05T13:21:02","guid":{"rendered":"http:\/\/www.juixe.com\/techknow\/index.php\/2006\/07\/05\/riff-rails-plugin\/"},"modified":"2006-07-05T08:21:02","modified_gmt":"2006-07-05T13:21:02","slug":"riff-rails-plugin","status":"publish","type":"post","link":"http:\/\/juixe.com\/techknow\/index.php\/2006\/07\/05\/riff-rails-plugin\/","title":{"rendered":"Riff Rails Plugin"},"content":{"rendered":"<p>Riff is a handy <b>diff tool<\/b> for ActiveRecord models.  The Riff plugin adds just three methods to the ActiveRecord class, diff?, diff, and diff_against_attributes.  You install Riff like any other plugin using the script\/plugin install command:<\/p>\n<pre>\nscript\/plugin install http:\/\/tfletcher.com\/svn\/rails-plugins\/riff\/\n<\/pre>\n<p>Once installed all you have to do to use riff is to create model records.<\/p>\n<pre>\nfirstPost = Post.new(:title =&gt; 'Rails Plugin')\nsecondPost = Post.new(:title =&gt; 'Plugin Rails')\n<\/pre>\n<p>To check if two objects are different from each other you can use the diff? method.<\/p>\n<pre>\nfirstPost.diff?(secondPost) # true\n<\/pre>\n<p>To check what the diferences are use the diff method.  The differences are returned as a hash of attributes with the two distinct values for the attribute as an array.  Let&#8217;s see if the code can explain this a bit further:<\/p>\n<pre>\ndiff_hash = one.diff(two)\ndiff_hash.each { |key, value|\n  # value is an array\n  value.each { |diff_value|\n    logger &lt;&lt; \"\\n + #{diff_value}\"\n  }\n}\n<\/pre>\n<p>The key for the diff_hash is a symbol for the attribute that differs.<\/p>\n<p>You can also diff a model instance against what is saved in the database by using the diff? and diff methods without a parameter.  Here is an example of that.<\/p>\n<pre>\npost = Post.find(params[:id])\npost.title = 'Rails Rocks'\nif post.diff?\n  # post is diff dan that in da db\nend\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\/activerecord\" rel=\"tag\"> activerecord<\/a>, <a href=\"http:\/\/technorati.com\/tag\/diff\" rel=\"tag\"> diff<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Riff is a handy diff tool for ActiveRecord models. The Riff plugin adds just three methods to the ActiveRecord class, diff?, diff, and diff_against_attributes. You install Riff like any other plugin using the script\/plugin install command: script\/plugin install http:\/\/tfletcher.com\/svn\/rails-plugins\/riff\/ Once installed all you have to do to use riff is to create model records. firstPost [&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-26","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/130"}],"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=130"}],"version-history":[{"count":0,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/130\/revisions"}],"wp:attachment":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/media?parent=130"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/categories?post=130"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/tags?post=130"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}