<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>acts_as_developer: Now When Did I Add That?</title>
    <link>http://www.robertshowalter.com/articles/2007/10/16/now-when-did-i-add-that</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description></description>
    <item>
      <title>Now When Did I Add That?</title>
      <description>&lt;p&gt;A colleague noticed today that on of our Rails models was raising a validation error when attempting to update a previously-entered row. A &lt;code&gt;validates_uniqueness_of&lt;/code&gt; constraint was being violated.&lt;/p&gt;


	&lt;p&gt;He wondered how the data could have been saved in the first place? As it turns out, the &lt;code&gt;validates_uniqueness_of&lt;/code&gt; had been added to the model fairly recently. Here&amp;#8217;s how we found out exactly when and by whom:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;$ svn blame app/models/widget.rb | grep validates_uniqueness_of
   494   showaltb   validates_uniqueness_of :name&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;The Subversion &lt;code&gt;blame&lt;/code&gt; command annotates each line of a file with the revision number in which that line was last changed and the user that committed that revision.  I used &lt;code&gt;grep&lt;/code&gt; to just find any &lt;code&gt;validates_uniqeness_of&lt;/code&gt; lines in the file.&lt;/p&gt;


	&lt;p&gt;From the output, you can see that the line was added (or changed) in revision 494 by user &lt;code&gt;showaltb&lt;/code&gt; (that&amp;#8217;s me).&lt;/p&gt;


	&lt;p&gt;Hmm, was the line added or changed? Let&amp;#8217;s ask Subversion:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;$ svn diff -c494 app/models/widget.rb | grep validates_uniqueness_of
+  validates_uniqueness_of :name&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;code&gt;-c494&lt;/code&gt; tells Subversion to show me the changes that were made by revision 494 (i.e. between 493 and 494). Yep, it was an add (the &lt;code&gt;+&lt;/code&gt; indicates an added line).

	&lt;p&gt;Now, when did I add that line? We need to know when changeset 494 was committed:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;$ svn info -r494 app/models/widget.rb
Path: app/models/widget.rb
...(snip)...
Last Changed Date: 2007-10-08 09:51:03 -0400 (Mon, 08 Oct 2007)&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;You can pass a revision number to &lt;code&gt;svn info&lt;/code&gt;. Cool. OK, so that line was added on October 8.&lt;/p&gt;


	&lt;p&gt;Now, generally, if you add or change a validation rule, you need to consider what to do about your existing data, because some of it may not be valid any more. One option would be to create a migration that would fix any validation problems.&lt;/p&gt;


	&lt;p&gt;For example, with my example here, I could write a migration to find any rows with duplicate names and add some kind of &amp;#8220;tie-breaker&amp;#8221; to the names to avoid the duplication problem.&lt;/p&gt;</description>
      <pubDate>Tue, 16 Oct 2007 13:14:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:2aeec311-8d23-4a2f-a7ff-b068a75b3c05</guid>
      <author>Bob Showalter</author>
      <link>http://www.robertshowalter.com/articles/2007/10/16/now-when-did-i-add-that</link>
      <category>Rails</category>
    </item>
    <item>
      <title>"Now When Did I Add That?" by chad</title>
      <description>&lt;p&gt;i love svn blame but it always blames me, so i stopped using it :)&lt;/p&gt;</description>
      <pubDate>Tue, 16 Oct 2007 16:31:54 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:131e42c9-c6c2-4f4b-8a72-1287b8bdd97d</guid>
      <link>http://www.robertshowalter.com/articles/2007/10/16/now-when-did-i-add-that#comment-923</link>
    </item>
  </channel>
</rss>
