<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Ketan&#039;s Musings &#187; rails</title>
	<atom:link href="http://ketan.padegaonkar.name/category/rails/feed" rel="self" type="application/rss+xml" />
	<link>http://ketan.padegaonkar.name</link>
	<description>Where he blogs about his eclipse musings</description>
	<lastBuildDate>Sat, 26 Feb 2011 18:24:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Using Models In Rails Migrations</title>
		<link>http://ketan.padegaonkar.name/2010/11/20/using-models-in-rails-migrations.html</link>
		<comments>http://ketan.padegaonkar.name/2010/11/20/using-models-in-rails-migrations.html#comments</comments>
		<pubDate>Sun, 21 Nov 2010 05:55:53 +0000</pubDate>
		<dc:creator>Ketan</dc:creator>
				<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://ketan.padegaonkar.name/?p=484</guid>
		<description><![CDATA[It is quite tempting to use models in rails migrations. At the time the migration is expected to run, the model class will have been updated already, so it is hard use that in the migration itself, even though it would be useful. Consider this example: class CreateUsers &#60; ActiveRecord::Migration def self.up t.string :email, :null [...]]]></description>
			<content:encoded><![CDATA[<p>It is quite tempting to use models in rails migrations.</p>
<p>At the time the migration is expected to run, the model class will have been updated already, so it is hard use that in the migration itself, even though it would be useful.</p>
<p>Consider this example:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> CreateUsers <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Migration</span>
    <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">up</span>
      t.<span style="color:#CC0066; font-weight:bold;">string</span> <span style="color:#ff3333; font-weight:bold;">:email</span>, <span style="color:#ff3333; font-weight:bold;">:null</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span>
      t.<span style="color:#CC0066; font-weight:bold;">string</span> <span style="color:#ff3333; font-weight:bold;">:password</span>, <span style="color:#ff3333; font-weight:bold;">:null</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#008000; font-style:italic;"># create a dummy user - with newer rails versions this should really go in seeds.rb!</span>
    User.<span style="color:#9900CC;">create</span>!<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:email</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'user@example.com'</span>, <span style="color:#ff3333; font-weight:bold;">:password</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'demo'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">down</span>
    drop_table <span style="color:#ff3333; font-weight:bold;">:users</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Doesn&#8217;t get simpler than this!</p>
<p>Now lets add another migration:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> AddSSNForUser <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Migration</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">up</span>
    add_column, <span style="color:#ff3333; font-weight:bold;">:users</span>, <span style="color:#ff3333; font-weight:bold;">:ssn</span>, :<span style="color:#CC0066; font-weight:bold;">string</span>, <span style="color:#ff3333; font-weight:bold;">:null</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span>
  <span style="color:#9966CC; font-weight:bold;">end</span> 
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">down</span>
    remove_column, <span style="color:#ff3333; font-weight:bold;">:users</span>, <span style="color:#ff3333; font-weight:bold;">:ssn</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>And a corresponding validation to the <code>User</code> model:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> User <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  <span style="color:#008000; font-style:italic;"># ...</span>
  validates_presence_of <span style="color:#ff3333; font-weight:bold;">:ssn</span>
  <span style="color:#008000; font-style:italic;"># ...</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Now if you were to run the database migrations on an empty database, the first migration would fail because of the lack of a SSN. Worse, it is quite possible that the User class is now called something else.</p>
<p>How do you get around this issue:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> CreateUsers <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Migration</span>
  <span style="color:#008000; font-style:italic;"># define your own User class</span>
  <span style="color:#9966CC; font-weight:bold;">class</span> User <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
    <span style="color:#008000; font-style:italic;">#...</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">up</span>
    <span style="color:#008000; font-style:italic;"># clear all cache that rails maintains about the User class/table mapping</span>
    User.<span style="color:#9900CC;">reset_column_information</span>
    create_table <span style="color:#ff3333; font-weight:bold;">:users</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>t<span style="color:#006600; font-weight:bold;">|</span>
      <span style="color:#008000; font-style:italic;"># whatever we did in the example above</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#008000; font-style:italic;"># this statement will always work no matter what.</span>
    User.<span style="color:#9900CC;">create</span>!<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:email</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'user@example.com'</span>, <span style="color:#ff3333; font-weight:bold;">:password</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'demo'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">down</span>
    drop_table <span style="color:#ff3333; font-weight:bold;">:users</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Sometimes using SQL for performing data migrations could get quite cumbersome and unreadable, and using model objects is the simplest way to run any form of data migration. In such a case, it&#8217;s better to copy your model code into the migration.</p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center">
<ul class="socials">
		<li class="shr-googlebuzz">
			<a href="http://www.shareaholic.com/api/share/?title=Using+Models+In+Rails+Migrations&amp;link=http://ketan.padegaonkar.name/2010/11/20/using-models-in-rails-migrations.html&amp;notes=It%20is%20quite%20tempting%20to%20use%20models%20in%20rails%20migrations.%0D%0A%0D%0AAt%20the%20time%20the%20migration%20is%20expected%20to%20run%2C%20the%20model%20class%20will%20have%20been%20updated%20already%2C%20so%20it%20is%20hard%20use%20that%20in%20the%20migration%20itself%2C%20even%20though%20it%20would%20be%20useful.%0D%0A%0D%0AConsider%20this%20example%3A%0D%0A%0D%0A%0D%0Aclass%20CreateUsers%20%3C%20ActiveRecord%3A%3AMi&amp;short_link=&amp;shortener=bitly&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=257&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-reddit">
			<a href="http://www.shareaholic.com/api/share/?title=Using+Models+In+Rails+Migrations&amp;link=http://ketan.padegaonkar.name/2010/11/20/using-models-in-rails-migrations.html&amp;notes=It%20is%20quite%20tempting%20to%20use%20models%20in%20rails%20migrations.%0D%0A%0D%0AAt%20the%20time%20the%20migration%20is%20expected%20to%20run%2C%20the%20model%20class%20will%20have%20been%20updated%20already%2C%20so%20it%20is%20hard%20use%20that%20in%20the%20migration%20itself%2C%20even%20though%20it%20would%20be%20useful.%0D%0A%0D%0AConsider%20this%20example%3A%0D%0A%0D%0A%0D%0Aclass%20CreateUsers%20%3C%20ActiveRecord%3A%3AMi&amp;short_link=&amp;shortener=bitly&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=40&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-twitter">
			<a href="http://www.shareaholic.com/api/share/?title=Using+Models+In+Rails+Migrations&amp;link=http://ketan.padegaonkar.name/2010/11/20/using-models-in-rails-migrations.html&amp;notes=It%20is%20quite%20tempting%20to%20use%20models%20in%20rails%20migrations.%0D%0A%0D%0AAt%20the%20time%20the%20migration%20is%20expected%20to%20run%2C%20the%20model%20class%20will%20have%20been%20updated%20already%2C%20so%20it%20is%20hard%20use%20that%20in%20the%20migration%20itself%2C%20even%20though%20it%20would%20be%20useful.%0D%0A%0D%0AConsider%20this%20example%3A%0D%0A%0D%0A%0D%0Aclass%20CreateUsers%20%3C%20ActiveRecord%3A%3AMi&amp;short_link=&amp;shortener=bitly&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=RT+%40ketanpkr+%24%7Btitle%7D+-+%24%7Bshort_link%7D&amp;service=7&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.shareaholic.com/api/share/?title=Using+Models+In+Rails+Migrations&amp;link=http://ketan.padegaonkar.name/2010/11/20/using-models-in-rails-migrations.html&amp;notes=It%20is%20quite%20tempting%20to%20use%20models%20in%20rails%20migrations.%0D%0A%0D%0AAt%20the%20time%20the%20migration%20is%20expected%20to%20run%2C%20the%20model%20class%20will%20have%20been%20updated%20already%2C%20so%20it%20is%20hard%20use%20that%20in%20the%20migration%20itself%2C%20even%20though%20it%20would%20be%20useful.%0D%0A%0D%0AConsider%20this%20example%3A%0D%0A%0D%0A%0D%0Aclass%20CreateUsers%20%3C%20ActiveRecord%3A%3AMi&amp;short_link=&amp;shortener=bitly&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=5&amp;tags=&amp;ctype=" rel="nofollow" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-dzone">
			<a href="http://www.shareaholic.com/api/share/?title=Using+Models+In+Rails+Migrations&amp;link=http://ketan.padegaonkar.name/2010/11/20/using-models-in-rails-migrations.html&amp;notes=It%20is%20quite%20tempting%20to%20use%20models%20in%20rails%20migrations.%0D%0A%0D%0AAt%20the%20time%20the%20migration%20is%20expected%20to%20run%2C%20the%20model%20class%20will%20have%20been%20updated%20already%2C%20so%20it%20is%20hard%20use%20that%20in%20the%20migration%20itself%2C%20even%20though%20it%20would%20be%20useful.%0D%0A%0D%0AConsider%20this%20example%3A%0D%0A%0D%0A%0D%0Aclass%20CreateUsers%20%3C%20ActiveRecord%3A%3AMi&amp;short_link=&amp;shortener=bitly&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=102&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Add this to DZone">Add this to DZone</a>
		</li>
</ul><div style="clear: both;"></div><div class="shr-getshr" style="visibility:hidden;font-size:10px !important"><a target="_blank" href="http://www.shareaholic.com/?src=pub">Get Shareaholic</a></div><div style="clear: both;"></div></div>

]]></content:encoded>
			<wfw:commentRss>http://ketan.padegaonkar.name/2010/11/20/using-models-in-rails-migrations.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Controller Testing in Active Scaffold</title>
		<link>http://ketan.padegaonkar.name/2007/12/27/controller-testing-in-active-scaffold.html</link>
		<comments>http://ketan.padegaonkar.name/2007/12/27/controller-testing-in-active-scaffold.html#comments</comments>
		<pubDate>Thu, 27 Dec 2007 18:10:44 +0000</pubDate>
		<dc:creator>Ketan</dc:creator>
				<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[technology]]></category>
		<category><![CDATA[active scaffold]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://ketan.padegaonkar.name/2007/12/27/controller-testing-in-active-scaffold.html</guid>
		<description><![CDATA[I just started my adventure with rails a few nights ago. Figured that Active Scaffold based controllers do something that is different from the default controllers generated by the scaffold generator. For one all controller tests broke when I moved to active scaffold. Here&#8217;s a blog post that talks about testing active scaffold based controllers. [...]]]></description>
			<content:encoded><![CDATA[<p>I just started my adventure with rails a few nights ago. Figured that <a href="http://activescaffold.com">Active Scaffold</a> based controllers do something that is different from the default controllers generated by the scaffold generator.</p>
<p>For one all controller tests broke when I moved to active scaffold. Here&#8217;s a <a href="http://www.42.mach7x.com/2007/11/08/controller-testing-in-active-scaffold/">blog post</a> that talks about testing active scaffold based controllers.</p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center">
<ul class="socials">
		<li class="shr-googlebuzz">
			<a href="http://www.shareaholic.com/api/share/?title=Controller+Testing+in+Active+Scaffold&amp;link=http://ketan.padegaonkar.name/2007/12/27/controller-testing-in-active-scaffold.html&amp;notes=I%20just%20started%20my%20adventure%20with%20rails%20a%20few%20nights%20ago.%20Figured%20that%20Active%20Scaffold%20based%20controllers%20do%20something%20that%20is%20different%20from%20the%20default%20controllers%20generated%20by%20the%20scaffold%20generator.%0A%0AFor%20one%20all%20controller%20tests%20broke%20when%20I%20moved%20to%20active%20scaffold.%20Here%27s%20a%20blog%20post%20that%20talks%20&amp;short_link=&amp;shortener=bitly&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=257&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-reddit">
			<a href="http://www.shareaholic.com/api/share/?title=Controller+Testing+in+Active+Scaffold&amp;link=http://ketan.padegaonkar.name/2007/12/27/controller-testing-in-active-scaffold.html&amp;notes=I%20just%20started%20my%20adventure%20with%20rails%20a%20few%20nights%20ago.%20Figured%20that%20Active%20Scaffold%20based%20controllers%20do%20something%20that%20is%20different%20from%20the%20default%20controllers%20generated%20by%20the%20scaffold%20generator.%0A%0AFor%20one%20all%20controller%20tests%20broke%20when%20I%20moved%20to%20active%20scaffold.%20Here%27s%20a%20blog%20post%20that%20talks%20&amp;short_link=&amp;shortener=bitly&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=40&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-twitter">
			<a href="http://www.shareaholic.com/api/share/?title=Controller+Testing+in+Active+Scaffold&amp;link=http://ketan.padegaonkar.name/2007/12/27/controller-testing-in-active-scaffold.html&amp;notes=I%20just%20started%20my%20adventure%20with%20rails%20a%20few%20nights%20ago.%20Figured%20that%20Active%20Scaffold%20based%20controllers%20do%20something%20that%20is%20different%20from%20the%20default%20controllers%20generated%20by%20the%20scaffold%20generator.%0A%0AFor%20one%20all%20controller%20tests%20broke%20when%20I%20moved%20to%20active%20scaffold.%20Here%27s%20a%20blog%20post%20that%20talks%20&amp;short_link=&amp;shortener=bitly&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=RT+%40ketanpkr+%24%7Btitle%7D+-+%24%7Bshort_link%7D&amp;service=7&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.shareaholic.com/api/share/?title=Controller+Testing+in+Active+Scaffold&amp;link=http://ketan.padegaonkar.name/2007/12/27/controller-testing-in-active-scaffold.html&amp;notes=I%20just%20started%20my%20adventure%20with%20rails%20a%20few%20nights%20ago.%20Figured%20that%20Active%20Scaffold%20based%20controllers%20do%20something%20that%20is%20different%20from%20the%20default%20controllers%20generated%20by%20the%20scaffold%20generator.%0A%0AFor%20one%20all%20controller%20tests%20broke%20when%20I%20moved%20to%20active%20scaffold.%20Here%27s%20a%20blog%20post%20that%20talks%20&amp;short_link=&amp;shortener=bitly&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=5&amp;tags=&amp;ctype=" rel="nofollow" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-dzone">
			<a href="http://www.shareaholic.com/api/share/?title=Controller+Testing+in+Active+Scaffold&amp;link=http://ketan.padegaonkar.name/2007/12/27/controller-testing-in-active-scaffold.html&amp;notes=I%20just%20started%20my%20adventure%20with%20rails%20a%20few%20nights%20ago.%20Figured%20that%20Active%20Scaffold%20based%20controllers%20do%20something%20that%20is%20different%20from%20the%20default%20controllers%20generated%20by%20the%20scaffold%20generator.%0A%0AFor%20one%20all%20controller%20tests%20broke%20when%20I%20moved%20to%20active%20scaffold.%20Here%27s%20a%20blog%20post%20that%20talks%20&amp;short_link=&amp;shortener=bitly&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=102&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Add this to DZone">Add this to DZone</a>
		</li>
</ul><div style="clear: both;"></div><div class="shr-getshr" style="visibility:hidden;font-size:10px !important"><a target="_blank" href="http://www.shareaholic.com/?src=pub">Get Shareaholic</a></div><div style="clear: both;"></div></div>

]]></content:encoded>
			<wfw:commentRss>http://ketan.padegaonkar.name/2007/12/27/controller-testing-in-active-scaffold.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

