<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>blog.mhartl &#124; Michael Hartl&#039;s tech blog</title>
	<atom:link href="http://blog.mhartl.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.mhartl.com</link>
	<description>Michael Hartl&#039;s tech blog</description>
	<lastBuildDate>Sat, 26 May 2012 23:57:49 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='blog.mhartl.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://0.gravatar.com/blavatar/4e51c93cc9bb9db581ae0a1592703f01?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>blog.mhartl &#124; Michael Hartl&#039;s tech blog</title>
		<link>http://blog.mhartl.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://blog.mhartl.com/osd.xml" title="blog.mhartl &#124; Michael Hartl&#039;s tech blog" />
	<atom:link rel='hub' href='http://blog.mhartl.com/?pushpress=hub'/>
		<item>
		<title>The Ruby on Rails Tutorial book &amp; screencasts</title>
		<link>http://blog.mhartl.com/2010/12/01/the-ruby-on-rails-tutorial-book-screencasts/</link>
		<comments>http://blog.mhartl.com/2010/12/01/the-ruby-on-rails-tutorial-book-screencasts/#comments</comments>
		<pubDate>Wed, 01 Dec 2010 20:16:07 +0000</pubDate>
		<dc:creator>mhartl</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.mhartl.com/?p=324</guid>
		<description><![CDATA[In October, I launched a product I&#8217;ve been working on for more than a year&#8212;the Ruby on Rails Tutorial book and screencast series&#8212;and yet somehow I neglected to make an announcement on my own tech blog! This post is an attempt to remedy that lamentable situation. In celebration of the launch, you can use the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.mhartl.com&#038;blog=3368824&#038;post=324&#038;subd=insoshi&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In October, I launched a product I&#8217;ve been working on for more than a year&mdash;the <a href="http://railstutorial.org/">Ruby on Rails Tutorial book and screencast series</a>&mdash;and yet somehow I neglected to make an announcement on my own tech blog! This post is an attempt to remedy that lamentable situation. In celebration of the launch, you can use the coupon code</p>
<p><tt>blogmhartl</tt></p>
<p>on the shopping cart page to get a 10% discount on any combination of <a href="http://railstutorial.org/">Ruby on Rails Tutorial</a> products through the end of the year.</p>
<p>The entire text of the <a href="http://railstutorial.org/ruby-on-rails-tutorial-book"><em>Ruby on Rails Tutorial</em> book</a> is available for free online, and yet many people have found it worthwhile to <a href="http://railstutorial.org/#buy">purchase the PDF</a>. I especially recommend the <a href="http://railstutorial.org/#buy">Rails Tutorial PDF/screencast bundle</a>, which includes more than 500 pages of material and more than 15&nbsp;hours of video. If you need further convincing, you can read the <a href="http://railstutorial.org/book#foreword">foreword by Derek Sivers</a>, the <a href="http://railstutorial.org/#praise">praise section</a>, or the <a href="http://www.drdobbs.com/blog/archives/2010/11/ruby_on_rails_t.html">recent review in Dr. Dobb&#8217;s</a>.</p>
<p><em>Note:</em> The <tt>blogmhartl</tt> coupon code for the <a href="http://railstutorial.org/">Ruby on Rails Tutorial</a> expires at midnight on January 1, 2011. Get it while it&#8217;s hot!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/insoshi.wordpress.com/324/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/insoshi.wordpress.com/324/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/insoshi.wordpress.com/324/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/insoshi.wordpress.com/324/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/insoshi.wordpress.com/324/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/insoshi.wordpress.com/324/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/insoshi.wordpress.com/324/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/insoshi.wordpress.com/324/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/insoshi.wordpress.com/324/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/insoshi.wordpress.com/324/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/insoshi.wordpress.com/324/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/insoshi.wordpress.com/324/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/insoshi.wordpress.com/324/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/insoshi.wordpress.com/324/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.mhartl.com&#038;blog=3368824&#038;post=324&#038;subd=insoshi&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.mhartl.com/2010/12/01/the-ruby-on-rails-tutorial-book-screencasts/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/ffda7d145b83c4b118f982401f962ca6?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mhartl</media:title>
		</media:content>
	</item>
		<item>
		<title>Deploying to Heroku with Rails 3.0.0.rc</title>
		<link>http://blog.mhartl.com/2010/07/28/deploying-to-heroku-with-rails-3-0-0-rc/</link>
		<comments>http://blog.mhartl.com/2010/07/28/deploying-to-heroku-with-rails-3-0-0-rc/#comments</comments>
		<pubDate>Wed, 28 Jul 2010 23:34:12 +0000</pubDate>
		<dc:creator>mhartl</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://blog.mhartl.com/?p=307</guid>
		<description><![CDATA[UPDATE: Heroku now works with the latest Bundler, so this post is (or should be) obsolete. The Ruby on Rails Tutorial book uses the latest version of Rails, which is the current release candidate of 3.0.0.rc. Unfortunately, at the time of this writing, you can&#8217;t deploy applications to Heroku using the Rails release candidate because [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.mhartl.com&#038;blog=3368824&#038;post=307&#038;subd=insoshi&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><strong>UPDATE:</strong> Heroku now works with the latest Bundler, so this post is (or should be) obsolete.</p>
<p>The <a href="http://railstutorial.org/"><em>Ruby on Rails Tutorial</em> book</a> uses the latest version of Rails, which is the current release candidate of 3.0.0.rc. Unfortunately, at the time of this writing, you can&#8217;t deploy applications to Heroku using the Rails release candidate because of a conflict with the latest version of Bundler. Instead of a successful deploy, you get an error like this:</p>
<pre>$ git push heroku
Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 1018 bytes, done.
Total 3 (delta 1), reused 0 (delta 0)

-----&gt; Heroku receiving push
-----&gt; Rails app detected
-----&gt; Detected Rails is not set to serve static_assets
       Installing rails3_serve_static_assets... done
-----&gt; Gemfile detected, running Bundler
       Unresolved dependencies detected; Installing...
       Your Gemfile.lock was generated by Bundler 0.10.
       You must delete it if you wish to use Bundler 0.9.
       FAILED: Have you updated to use a 0.9 Gemfile?
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;http://docs.heroku.com/gems#gem-bundler

error: hooks/pre-receive exited with error code 1
</pre>
<p>Eventually, Heroku support for the Rails release candidate (or perhaps for the final release) will no doubt be ready, but for now you can work around this problem as follows:</p>
<pre>$ [sudo] gem uninstall bundler
$ [sudo] gem install bundler -v 0.9.26
$ rm -f Gemfile.lock</pre>
<p>Then use this as your <tt>Gemfile</tt>, which reverts back to Rails 3.0.0.beta4:</p>
<p><pre class="brush: ruby;">
source 'http://rubygems.org'

gem 'rails', '3.0.0.beta4'
gem 'sqlite3-ruby', '1.2.5', :require =&gt; 'sqlite3'
</pre></p>
<p>Then install the gems:</p>
<pre>$ bundle install</pre>
<p>At this point, the push to Heroku should work. (Of course, that doesn&#8217;t mean it will. :-)</p>
<pre>$ git add .
$ git commit -m "Ready to deploy"
$ git push heroku master</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/insoshi.wordpress.com/307/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/insoshi.wordpress.com/307/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/insoshi.wordpress.com/307/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/insoshi.wordpress.com/307/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/insoshi.wordpress.com/307/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/insoshi.wordpress.com/307/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/insoshi.wordpress.com/307/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/insoshi.wordpress.com/307/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/insoshi.wordpress.com/307/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/insoshi.wordpress.com/307/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/insoshi.wordpress.com/307/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/insoshi.wordpress.com/307/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/insoshi.wordpress.com/307/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/insoshi.wordpress.com/307/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.mhartl.com&#038;blog=3368824&#038;post=307&#038;subd=insoshi&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.mhartl.com/2010/07/28/deploying-to-heroku-with-rails-3-0-0-rc/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/ffda7d145b83c4b118f982401f962ca6?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mhartl</media:title>
		</media:content>
	</item>
		<item>
		<title>Some rvm gotchas</title>
		<link>http://blog.mhartl.com/2010/02/19/some-rvm-gotchas/</link>
		<comments>http://blog.mhartl.com/2010/02/19/some-rvm-gotchas/#comments</comments>
		<pubDate>Fri, 19 Feb 2010 19:59:23 +0000</pubDate>
		<dc:creator>mhartl</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.mhartl.com/?p=251</guid>
		<description><![CDATA[Like many in Rails-land, I started playing with the Rails&#160;3 beta as soon as it came out. And, like many, I discovered to my chagrin that (a) Rails&#160;3 doesn&#8217;t play nice with Rails&#160;2.3 and (b) it doesn&#8217;t uninstall cleanly. (I ended up manually removing all 3.0.0.beta gems by hand.) If you ask around a bit, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.mhartl.com&#038;blog=3368824&#038;post=251&#038;subd=insoshi&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Like many in Rails-land, I started playing with the <a href="http://weblog.rubyonrails.org/2010/2/5/rails-3-0-beta-release">Rails&nbsp;3 beta</a> as soon as it came out. And, like many, I discovered to my chagrin that (a) Rails&nbsp;3 doesn&#8217;t play nice with Rails&nbsp;2.3 and (b) it doesn&#8217;t uninstall cleanly. (I ended up manually removing all <tt>3.0.0.beta</tt> gems by hand.) If you ask around a bit, you&#8217;ll quickly discover a solution to this problem (if you&#8217;re running a Mac), which is to run multiple Ruby versions and then install different versions of Rails on different Rubies. This works great in principle, but I ran into a few gotchas in the actual implementation, so I thought I&#8217;d share them for the benefit of Google searchers everywhere.</p>
<p>In order to manage the feat of multiple Rubies, the amazing <a href="http://rvm.beginrescueend.com/">Ruby Version Manager</a> (rvm) by Wayne Seguin is simply essential. Here are the steps I followed:</p>
<p>After <a href="http://rvm.beginrescueend.com/rvm/install/">installing rvm</a>, I first tried to install the latest version of Ruby&nbsp;1.8.7, which works with both Rails 2 and Rails 3:</p>
<pre>$ rvm install 1.8.7</pre>
<p>At this point I got a readline error, but this was covered by the <a href="http://rvm.beginrescueend.com/support/troubleshooting/">rvm troubleshooting guide</a>:</p>
<pre>$ rvm remove 1.8.7; rvm install readline</pre>
<p>Unfortunately, the installation still fails to build a makefile:</p>
<pre>$ rvm install 1.8.7
.
.
.
FAIL
$ less ~/.rvm/log/ruby-1.8.7-p249/make.error.log

[2010-02-12 08:54:30] make 
make: *** No targets specified and no makefile found.  Stop.</pre>
<p>After searching around for a while, I finally found a page (location already forgotten) with the fix, which involves including an explicit path to the readline dependency:</p>
<pre>$ rvm remove 1.8.7
$ rvm install 1.8.7 -C --with-readline-dir=/Users/mhartl/.rvm/usr</pre>
<p>This still didn&#8217;t work, though; on my system (OS X 10.5 Leopard) I got a segfault, which shows up when installing certain gems:</p>
<pre>$ gem install metric_fu

/Users/mhartl/.rvm/rubies/ruby-1.8.7-p249/lib/ruby/site_ruby/1.8/rubygems/
spec_fetcher.rb:245: [BUG] Segmentation fault
ruby 1.8.7 (2010-01-10 patchlevel 249) [i686-darwin9.8.0]

Abort trap</pre>
<p>Luckily, I have a working Ruby 1.8.7 in <tt>/usr/local/bin</tt>, so I could check the patchlevel: <tt>p174</tt>. Since rvm installs the latest version by default (<tt>1.8.7-p249</tt> as of this writing), I had to indicate the patchlevel explicitly:</p>
<pre>$ rvm remove 1.8.7
$ rvm install 1.8.7-p174 -C --with-readline-dir=/Users/mhartl/.rvm/usr</pre>
<p>(Note that this also requires the explicit readline path.)</p>
<p>Finally, I had a working Ruby, but I wanted to set the rvm Ruby as the default, and my system wouldn&#8217;t remember it:</p>
<pre>$ rvm 1.8.7-p174 --default
$ which ruby 
/Users/mhartl/.rvm/rubies/ruby-1.8.7-p174/bin/ruby
new shell...
$ which ruby
/usr/local/bin/ruby</pre>
<p>The solution was to remove the explicit <tt>/usr/local/bin</tt> path from my <tt>.bash_profile</tt> file:</p>
<pre>before:
  export PATH="/usr/local/bin:$HOME/bin:/usr/local/mysql/bin:$PATH"
  if [[ -s /Users/mhartl/.rvm/scripts/rvm ]] ; then source /Users/mhartl/.rvm/scripts/rvm ; fi

after:
  export PATH="$HOME/bin:/usr/local/mysql/bin:$PATH"
  if [[ -s /Users/mhartl/.rvm/scripts/rvm ]] ; then source /Users/mhartl/.rvm/scripts/rvm ; fi</pre>
<p>Wayne Seguin assures me that this step should be unnecessary as long as the <tt>PATH</tt> appears before the rvm line, but this is the only way I could get it to work on my system. (Since <tt>/usr/local/bin</tt> is on the system path, all the executables there still run; I&#8217;m not sure why I ever included it in <tt>.bash_profile</tt> in the first place.)</p>
<p>Finally, we&#8217;re ready for the raison d&#8217;être of all this work: multiple Rubies, multiple Rails. Here&#8217;s how to get it working (note the lack of <tt>sudo</tt> in the gem installations):</p>
<pre>$ rvm use 1.8.7
$ gem install rails -v 2.3.5
$ rvm install 1.9.2
$ rvm use 1.9.2
$ gem install rails --pre</pre>
<p>Voil&agrave;! Now switching between Rails versions is as easy as</p>
<pre>$ rvm use 1.8.7</pre>
<p>and</p>
<pre>$ rvm use 1.9.2</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/insoshi.wordpress.com/251/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/insoshi.wordpress.com/251/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/insoshi.wordpress.com/251/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/insoshi.wordpress.com/251/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/insoshi.wordpress.com/251/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/insoshi.wordpress.com/251/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/insoshi.wordpress.com/251/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/insoshi.wordpress.com/251/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/insoshi.wordpress.com/251/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/insoshi.wordpress.com/251/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/insoshi.wordpress.com/251/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/insoshi.wordpress.com/251/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/insoshi.wordpress.com/251/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/insoshi.wordpress.com/251/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.mhartl.com&#038;blog=3368824&#038;post=251&#038;subd=insoshi&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.mhartl.com/2010/02/19/some-rvm-gotchas/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/ffda7d145b83c4b118f982401f962ca6?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mhartl</media:title>
		</media:content>
	</item>
		<item>
		<title>Running rcov with RSpec</title>
		<link>http://blog.mhartl.com/2009/05/15/running-rcov-with-rspec/</link>
		<comments>http://blog.mhartl.com/2009/05/15/running-rcov-with-rspec/#comments</comments>
		<pubDate>Fri, 15 May 2009 23:01:08 +0000</pubDate>
		<dc:creator>mhartl</dc:creator>
				<category><![CDATA[RSpec]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://blog.mhartl.com/?p=236</guid>
		<description><![CDATA[I recently wanted to run rcov, the Ruby code coverage tool, on a project tested with RSpec. I think I&#8217;d done it once before, but I&#8217;d forgotten how. After searching to no avail (both the rcov home page and the RSpec page on rcov proved unhelpful), I applied the tried-and-true Wild-Assed Guess&#8482; method and typed [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.mhartl.com&#038;blog=3368824&#038;post=236&#038;subd=insoshi&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I recently wanted to run <tt>rcov</tt>, the <a href="http://eigenclass.org/hiki.rb?rcov">Ruby code coverage tool</a>, on a project tested with <a href="http://rspec.info">RSpec</a>. I think I&#8217;d done it once before, but I&#8217;d forgotten how. After searching to no avail (both the <a href="http://eigenclass.org/hiki.rb?rcov">rcov home page</a> and <a href="http://rspec.info/documentation/tools/rcov.html">the RSpec page on rcov</a> proved unhelpful), I applied the tried-and-true Wild-Assed Guess&trade; method and typed </p>
<pre>$ rake spec:rcov</pre>
<p>That worked.</p>
<p>The reports themselves are in the <tt>coverage/</tt> directory:</p>
<pre>$ open coverage/index.html</pre>
<p>(or navigate your browser to <tt>file:///path/to/project/coverage/index.html</tt>). If you&#8217;re using Git, add <tt>coverage/*</tt> to your .gitignore file.</p>
<p>N.B. The inverse Rake task also exists:</p>
<pre>$ rake -T rcov
rake spec:clobber_rcov  # Remove rcov products for rcov
rake spec:rcov          # Run all specs in spec directory with RCov (excluding plugin specs)</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/insoshi.wordpress.com/236/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/insoshi.wordpress.com/236/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/insoshi.wordpress.com/236/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/insoshi.wordpress.com/236/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/insoshi.wordpress.com/236/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/insoshi.wordpress.com/236/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/insoshi.wordpress.com/236/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/insoshi.wordpress.com/236/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/insoshi.wordpress.com/236/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/insoshi.wordpress.com/236/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/insoshi.wordpress.com/236/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/insoshi.wordpress.com/236/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/insoshi.wordpress.com/236/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/insoshi.wordpress.com/236/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.mhartl.com&#038;blog=3368824&#038;post=236&#038;subd=insoshi&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.mhartl.com/2009/05/15/running-rcov-with-rspec/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/ffda7d145b83c4b118f982401f962ca6?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mhartl</media:title>
		</media:content>
	</item>
		<item>
		<title>New RSS feed</title>
		<link>http://blog.mhartl.com/2009/04/28/new-rss-feed/</link>
		<comments>http://blog.mhartl.com/2009/04/28/new-rss-feed/#comments</comments>
		<pubDate>Tue, 28 Apr 2009 15:22:51 +0000</pubDate>
		<dc:creator>mhartl</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.mhartl.com/?p=225</guid>
		<description><![CDATA[This blog&#8217;s RSS feed has changed; please re-subscribe here: http://feeds2.feedburner.com/mhartl. (It might take an hour or two to go live. If it doesn&#8217;t work for you now, come back in a bit and try again.)<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.mhartl.com&#038;blog=3368824&#038;post=225&#038;subd=insoshi&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>This blog&#8217;s RSS feed has changed; please re-subscribe here: <a title="Michael Hartl's tech blog RSS feed" href="http://feeds2.feedburner.com/mhartl">http://feeds2.feedburner.com/mhartl</a>. (It might take an hour or two to go live. If it doesn&#8217;t work for you now, come back in a bit and try again.)</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/insoshi.wordpress.com/225/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/insoshi.wordpress.com/225/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/insoshi.wordpress.com/225/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/insoshi.wordpress.com/225/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/insoshi.wordpress.com/225/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/insoshi.wordpress.com/225/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/insoshi.wordpress.com/225/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/insoshi.wordpress.com/225/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/insoshi.wordpress.com/225/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/insoshi.wordpress.com/225/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/insoshi.wordpress.com/225/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/insoshi.wordpress.com/225/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/insoshi.wordpress.com/225/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/insoshi.wordpress.com/225/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.mhartl.com&#038;blog=3368824&#038;post=225&#038;subd=insoshi&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.mhartl.com/2009/04/28/new-rss-feed/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/ffda7d145b83c4b118f982401f962ca6?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mhartl</media:title>
		</media:content>
	</item>
		<item>
		<title>Using a temporary branch when doing Git merges</title>
		<link>http://blog.mhartl.com/2008/10/28/using-a-temporary-branch-when-doing-git-merges/</link>
		<comments>http://blog.mhartl.com/2008/10/28/using-a-temporary-branch-when-doing-git-merges/#comments</comments>
		<pubDate>Tue, 28 Oct 2008 22:54:10 +0000</pubDate>
		<dc:creator>mhartl</dc:creator>
				<category><![CDATA[Git]]></category>
		<category><![CDATA[Insoshi]]></category>

		<guid isPermaLink="false">http://insoshi.wordpress.com/?p=202</guid>
		<description><![CDATA[Merging branches in Git is wonderfully easy compared to many other version control tools, but sometimes merging causes problems you&#8217;d rather undo. One common merge side effect is the creation of code conflicts, and sometimes a merge causes so many conflicts that you end up regretting doing the merge in the first place. In addition, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.mhartl.com&#038;blog=3368824&#038;post=202&#038;subd=insoshi&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Merging branches in Git is wonderfully easy compared to many other version control tools, but sometimes merging causes problems you&#8217;d rather undo.  One common merge side effect is the creation of code conflicts, and sometimes a merge causes so many conflicts that you end up regretting doing the merge in the first place.  In addition, for projects with many contributors (such as <a href="http://insoshi.com/">Insoshi</a>), sometimes you aren&#8217;t sure if you will even want to use the contribution on the branch you&#8217;re merging in.  Unfortunately, merges are very difficult to undo, so if you just do a direct merge of, say, a contributor branch into your main development branch, you&#8217;re stuck if you decide you don&#8217;t want the changes after all:</p>
<pre># Don't do this!
$ git checkout master
$ git merge contributor_branch</pre>
<p>The solution is always to use a <em>temporary branch</em> when doing any merge whose changes you&#8217;re not <em>sure</em> you&#8217;ll want to keep; if the merge proves intractable due to conflicts, or you just don&#8217;t want to use the contribution, then you can simply delete the temp branch.  Here&#8217;s how it works:</p>
<pre>$ git checkout master
$ git checkout -b temp_branch
$ git merge contributor_branch</pre>
<p>Then you can do stuff like</p>
<pre>$ git status
$ git diff master
&lt;resolve conflicts, polish contributed code&gt;</pre>
<p>If the new branch passes muster, you can then merge it in:</p>
<pre>$ git checkout master
$ git merge temp_branch
$ git branch -D temp_branch</pre>
<p>(Note here that I&#8217;ve deleted the temp branch in the final step, just to clean up.)  If, on the other hand, you decide not to continue with the merge, you can just delete the temp branch without merging it in:</p>
<pre>$ git checkout master
$ git branch -D temp_branch</pre>
<p>Either way, the lesson is the same: consistently using temp branches when doing dangerous merges is a great way to avoid the agony of merge remorse.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/insoshi.wordpress.com/202/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/insoshi.wordpress.com/202/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/insoshi.wordpress.com/202/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/insoshi.wordpress.com/202/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/insoshi.wordpress.com/202/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/insoshi.wordpress.com/202/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/insoshi.wordpress.com/202/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/insoshi.wordpress.com/202/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/insoshi.wordpress.com/202/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/insoshi.wordpress.com/202/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/insoshi.wordpress.com/202/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/insoshi.wordpress.com/202/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/insoshi.wordpress.com/202/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/insoshi.wordpress.com/202/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.mhartl.com&#038;blog=3368824&#038;post=202&#038;subd=insoshi&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.mhartl.com/2008/10/28/using-a-temporary-branch-when-doing-git-merges/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/ffda7d145b83c4b118f982401f962ca6?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mhartl</media:title>
		</media:content>
	</item>
		<item>
		<title>Setting up your Git repositories for open source projects at GitHub</title>
		<link>http://blog.mhartl.com/2008/10/14/setting-up-your-git-repositories-for-open-source-projects-at-github/</link>
		<comments>http://blog.mhartl.com/2008/10/14/setting-up-your-git-repositories-for-open-source-projects-at-github/#comments</comments>
		<pubDate>Tue, 14 Oct 2008 19:08:53 +0000</pubDate>
		<dc:creator>long</dc:creator>
				<category><![CDATA[Git]]></category>
		<category><![CDATA[Insoshi]]></category>

		<guid isPermaLink="false">http://insoshi.wordpress.com/?p=150</guid>
		<description><![CDATA[[This is a guest post from Long Nguyen. &#8212;mhartl] Like a lot of projects in the Ruby on Rails world, the Insoshi social networking platform uses Git and GitHub to manage its open source development and contributions. In setting up the repositories for Insoshi, I&#8217;ve applied the version control experience I gained at Discover, where [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.mhartl.com&#038;blog=3368824&#038;post=150&#038;subd=insoshi&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>[This is a guest post from Long Nguyen. &mdash;mhartl]</p>
<p>Like a lot of projects in the Ruby on Rails world, the <a href="http://insoshi.com/">Insoshi social networking platform</a> uses <a href="http://git.or.cz/">Git</a> and <a href="http://github.com/">GitHub</a> to manage its open source development and contributions.  In setting up the repositories for Insoshi, I&#8217;ve applied the version control experience I gained  at <a href="http://www.discovercard.com/">Discover</a>, where I was technical lead for the software configuration management (SCM) team. Since some aspects of our setup aren&#8217;t obvious if you haven&#8217;t managed large projects before, we at Insoshi decided to share the details so that other GitHub projects might benefit as well.</p>
<p>We&#8217;ll start by reviewing the typical Git workflow based on <em>pull requests</em>, then discuss some problems you might run into with a &#8220;typical&#8221; repository setup, and finally explain the details of preparing the Insoshi Git repository for collaboration.</p>
<h4>Why Pull Requests?</h4>
<p>Git was originally developed by Linus Torvalds to host the Linux kernel, and <a href="http://github.com/guides/pull-requests">pull requests</a> are the de-facto standard for submitting contributions in Git because that&#8217;s what Linus does.  (He talked about this in his <a href="http://www.youtube.com/watch?v=4XpnKHJAok8">Google Tech Talk on Git</a>.)  The concept of the pull request is straightforward: You notify someone that you&#8217;ve made an update via email, messaging on GitHub, etc. and let them know where to find it.  They can then pull in your changes and merge it with their work.</p>
<p>Except for that interaction, everyone works within their own repository and on their own schedule.  There&#8217;s no process waiting to be completed that blocks you from moving on to whatever you need/want to do next.  And you&#8217;re not forcing anyone to drop what they&#8217;re doing to right now to handle your request.</p>
<p>It&#8217;s all very polite.  And it works well in the context of distributed development since you avoid all kinds of coordination issues.</p>
<h4>What&#8217;s needed?</h4>
<p>If you want to contribute to an open source project, here&#8217;s really all that you need:</p>
<ol>
<li>A publicly accessible repository where your changes can be found</li>
<li>A local repository for your development</li>
</ol>
<p>Even if you&#8217;re new to <a href="http://git.or.cz/">Git</a>, these both seem like pretty straightforward things to do—especially if you&#8217;re using <a href="http://github.com">GitHub</a> for the public repository:  your repository is just a fork of the main project repository.</p>
<p>Let&#8217;s set up our repository by going to the <a href="http://github.com/insoshi/insoshi/">official Insoshi repository</a> and clicking on the fork button:</p>
<p style="text-align:center;"><a href="http://github.com/insoshi/insoshi/fork"><img class="aligncenter size-medium wp-image-190" title="Forking Insoshi at GitHub" src="http://insoshi.files.wordpress.com/2008/10/forking-insoshi-at-github.png?w=300&h=184" alt="" width="300" height="184" /></a></p>
<p>I&#8217;ll need make note of the public clone URL for the official repository and my private clone URL for my newly created fork:</p>
<ul>
<li>Official Insoshi public clone URL<br />
<tt>git://github.com/insoshi/insoshi.git</tt></li>
<li>My fork&#8217;s private clone URL<br />
<tt>git@github.com:long/insoshi.git</tt></li>
</ul>
<h4>Your local repository: The &#8220;obvious&#8221; thing to do</h4>
<p>At this point, I&#8217;ll be tempted to go ahead and make a local clone of my fork:</p>
<pre>$ git clone git@github.com:long/insoshi.git</pre>
<p>and immediately get to work.</p>
<p>Technically, there&#8217;s nothing wrong with that.  And as an individual developer starting a new project, it&#8217;s what you do, but there are several disadvantages to this seemingly straightforward approach.  One of the major benefits of a distributed version control system like Git is that each repository is on an equal footing; in particular, we would like <em>every</em> fork to have the same master branch, so that if the &#8220;official&#8221; Insoshi repository should ever be lost there would be plenty of redundant backups.  We also want it to be easy for each developer to pull in changes from the official repository; the &#8220;obvious&#8221; approach isn&#8217;t set up for that.  Finally, it&#8217;s a bad idea in general to work on the master branch; experienced Git users typically work on separate development branches and then merge those branches into master when they&#8217;re done.</p>
<p>What we&#8217;d like is a way to connect up the local repository in a way that will</p>
<ul>
<li>Keep the repositories in sync so that each contains the full &#8220;official&#8221; repository</li>
<li>Allow developers to pull in official updates</li>
<li>Encourage working on branches other than master</li>
</ul>
<p>In the &#8220;obvious&#8221; configuration, I&#8217;m not set up to do any of that:</p>
<ul>
<li>There&#8217;s no local connection to the official repository for updates</li>
<li>There&#8217;s no mechanism in place to push official updates to my fork on GitHub</li>
<li>We&#8217;re working directly on the master branch</li>
</ul>
<h4>Your local repository: The &#8220;right&#8221; way</h4>
<p>Keeping the big picture in mind, here are the commands I&#8217;ve run to set up my local repository (using the GitHub id <tt>long</tt>):</p>
<pre>$ git clone git://github.com/insoshi/insoshi.git
$ cd insoshi
$ git branch --track edge origin/edge
$ git branch long edge
$ git checkout long
$ git remote add long git@github.com:long/insoshi.git
$ git fetch long
$ git push long long:refs/heads/long
$ git config branch.long.remote long
$ git config branch.long.merge refs/heads/long</pre>
<p>Let&#8217;s take a detailed look at what these steps accomplish.</p>
<h4>So what does it all mean?</h4>
<h5>Step one</h5>
<p>Create a local clone of the Insoshi repository:</p>
<pre>$ git clone git://github.com/insoshi/insoshi.git</pre>
<p>You should note that the Git URL for the clone references the <em>official</em> Insoshi repository and not the URL of my own fork (i.e., the clone URL is <tt>git://github.com/insoshi/insoshi.git</tt> instead of <tt>git@github.com:long/insoshi.git</tt>).  This way, the official repository is the default remote (aka &#8216;origin&#8217;), and the local master branch tracks the official master.</p>
<h5>Step two</h5>
<p>I have to change into the repository to perform additional git setup:</p>
<pre>$ cd insoshi</pre>
<h5>Step three</h5>
<p>Insoshi also has an &#8216;edge&#8217; branch for changes that we want to make public but may require a bit more polishing before we&#8217;d consider them production-ready (in the past this has included migrating to Rails 2.1 and Sphinx/Ultrasphinx).  Our typical development lifecycle looks something like</p>
<p style="text-align:center;">development -&gt; edge -&gt; master</p>
<p>I want to create a local tracking branch for it:</p>
<pre>$ git branch --track edge origin/edge</pre>
<h5>Steps four and five</h5>
<p>As I mentioned before, I&#8217;m resisting the temptation to immediately start working on the local &#8216;master&#8217; and &#8216;edge&#8217; branches.  I want to keep those in sync with the official Insoshi repository.</p>
<p>I&#8217;ll keep my changes separate by creating a new branch &#8216;long&#8217; that&#8217;s based off edge and checking it out:</p>
<pre>$ git branch long edge
$ git checkout long</pre>
<p>By the way, you can actually combine the two commands if you like, using just the &#8216;git checkout&#8217; command with the <tt>-b</tt> flag:</p>
<pre>$ git checkout -b long edge</pre>
<p>You can name this branch anything that you want, but I&#8217;ve chosen my GitHub id so that it&#8217;s easy to identify.</p>
<p>I&#8217;m starting my changes off of &#8216;edge&#8217; since that contains all the latest updates and any contribution I submit a pull request for will be merged first into the official Insoshi &#8216;edge&#8217; branch to allow for public testing before it&#8217;s merged into the &#8216;master&#8217;.</p>
<h5>Steps six and seven</h5>
<p>I&#8217;m finally adding the remote reference to my fork on GitHub:</p>
<pre>$ git remote add long git@github.com:long/insoshi.git</pre>
<p>I&#8217;ve used my GitHub id once again, this time as the remote nickname.</p>
<p>We should run a fetch immediately in order to sync up the local repository with the fork:</p>
<pre>$ git fetch long</pre>
<h5>Step eight</h5>
<p>I&#8217;m pushing up my new local branch up to my fork.  Since it&#8217;ll be a new branch on the remote end, I need to fully specify the remote refspec:</p>
<pre>$ git push long long:refs/heads/long</pre>
<h5>Steps nine and ten</h5>
<p>Now that the new branch is up on my fork, I want to set the branch configuration to track it:</p>
<pre>$ git config branch.long.remote long
$ git config branch.long.merge refs/heads/long</pre>
<p>Setting the remote lets me just simply use</p>
<pre>$ git push</pre>
<p>to push changes on my development branch up to my fork</p>
<p>Setting the merge configuration is mainly for completeness at this point.  But if you end up working on more than one machine (work/home, desktop/laptop, etc.), it&#8217;ll allow you to just use</p>
<pre>$ git pull</pre>
<p>to grab the changes you&#8217;ve pushed up to your fork.</p>
<h4>Isn&#8217;t that a lot of extra work to do?</h4>
<p>This may seem like a lot work up front, but it&#8217;s all configuration work that you&#8217;d eventually do anyway.  If you&#8217;re really that concerned about the extra typing, I&#8217;ve got <a href="http://docs.insoshi.com/Git-Guides/quick-git-setup/configure_insoshi_local.sh?attredirects=0">a shell script for you</a>.</p>
<p>The extra work is worth the effort, because with this configuration</p>
<ul>
<li>My changes will be easily identifiable in my named branch</li>
<li>I can easily get updates from the main Insoshi repository</li>
<li>Any updates I&#8217;ve pulled into master and edge are automatically pushed up to my fork on GitHub</li>
</ul>
<p>The last one is a bonus because the default refspec for remotes is <tt>refs/heads/*:refs/heads/*</tt>. This means that the simple &#8216;git push&#8217; command will push up changes for all local branches that have a matching branch on the remote. And if I make it a point to pull in updates to my local master and edge but not work directly on them, my fork will match up with the official repository.</p>
<p>So what is the benefit of all this to open source projects like Insoshi?</p>
<ul>
<li>The easier it is for the contributor to pull in updates, the more likely it will be that the pull request will be for code that merges easily with the latest releases (with few conflicts)</li>
<li>You can tell if someone is pulling updates by looking at their master and edge branches and seeing if they match up with the latest branches on the main repository</li>
<li>By getting contributors in the habit of working on branches, you&#8217;re going to get better organized code contributions</li>
</ul>
<p>Basically, the less effort that&#8217;s required to bring in code via a pull request, the sooner it can be added to the project release.  And at the end of the day, that&#8217;s really what it&#8217;s all about.</p>
<h4>Putting (pushing and pulling) it all together</h4>
<p>Now that we&#8217;ve covered all the details, let&#8217;s go through the full set of steps needed to make a contribution to a project like Insoshi:</p>
<ol>
<li>Fork the Insoshi repository on GitHub:
<p style="text-align:center;"><a href="http://github.com/insoshi/insoshi/fork"><img class="aligncenter size-medium wp-image-190" title="Forking Insoshi at GitHub" src="http://insoshi.files.wordpress.com/2008/10/forking-insoshi-at-github.png?w=300&h=184" alt="" width="300" height="184" /></a></p>
</li>
<li>Follow the Git steps above or use the <a href="http://docs.insoshi.com/Git-Guides/quick-git-setup/configure_insoshi_local.sh?attredirects=0">shell script</a> to set up your local repository</li>
<li>Checkout the local branch, just to be sure:
<pre>$ git checkout long</pre>
</li>
<li>Make some changes (and remember your development branch is against &#8216;edge&#8217;) and commit them:
<pre>[make changes in a text editor]
$ git commit -m "My great contribution"
$ git push</pre>
</li>
<li>Go to your fork and branch at GitHub (I&#8217;m at <a href="http://github.com/long/insoshi/tree/long">long/insoshi @ long</a>) and click on the pull request button:
<p style="text-align:center;"><a href="http://insoshi.files.wordpress.com/2008/10/pull-request-from-branch-at-github.png"><img class="aligncenter size-medium wp-image-189" title="Pull Request from Branch at GitHub" src="http://insoshi.files.wordpress.com/2008/10/pull-request-from-branch-at-github.png?w=300&h=194" alt="" width="300" height="194" /></a></p>
</li>
<li>Tell us about what you just did and make sure &#8220;insoshi&#8221; is a recipient:<a href="http://insoshi.files.wordpress.com/2008/10/pull-request-message.png"><img class="aligncenter size-medium wp-image-191" title="Pull Request Message" src="http://insoshi.files.wordpress.com/2008/10/pull-request-message.png?w=300&h=223" alt="" width="300" height="223" /></a></li>
<li>Bask in the glory of being an open-source contributor!</li>
</ol>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/insoshi.wordpress.com/150/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/insoshi.wordpress.com/150/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/insoshi.wordpress.com/150/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/insoshi.wordpress.com/150/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/insoshi.wordpress.com/150/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/insoshi.wordpress.com/150/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/insoshi.wordpress.com/150/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/insoshi.wordpress.com/150/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/insoshi.wordpress.com/150/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/insoshi.wordpress.com/150/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/insoshi.wordpress.com/150/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/insoshi.wordpress.com/150/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/insoshi.wordpress.com/150/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/insoshi.wordpress.com/150/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.mhartl.com&#038;blog=3368824&#038;post=150&#038;subd=insoshi&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.mhartl.com/2008/10/14/setting-up-your-git-repositories-for-open-source-projects-at-github/feed/</wfw:commentRss>
		<slash:comments>30</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/25498f6df76787138fcd0a976abf2c0f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">long</media:title>
		</media:content>

		<media:content url="http://insoshi.files.wordpress.com/2008/10/forking-insoshi-at-github.png?w=300" medium="image">
			<media:title type="html">Forking Insoshi at GitHub</media:title>
		</media:content>

		<media:content url="http://insoshi.files.wordpress.com/2008/10/forking-insoshi-at-github.png?w=300" medium="image">
			<media:title type="html">Forking Insoshi at GitHub</media:title>
		</media:content>

		<media:content url="http://insoshi.files.wordpress.com/2008/10/pull-request-from-branch-at-github.png?w=300" medium="image">
			<media:title type="html">Pull Request from Branch at GitHub</media:title>
		</media:content>

		<media:content url="http://insoshi.files.wordpress.com/2008/10/pull-request-message.png?w=300" medium="image">
			<media:title type="html">Pull Request Message</media:title>
		</media:content>
	</item>
		<item>
		<title>Using Rails to serve different content to humans and robots</title>
		<link>http://blog.mhartl.com/2008/09/26/using-rails-to-serve-different-content-to-humans-and-robots/</link>
		<comments>http://blog.mhartl.com/2008/09/26/using-rails-to-serve-different-content-to-humans-and-robots/#comments</comments>
		<pubDate>Fri, 26 Sep 2008 18:38:54 +0000</pubDate>
		<dc:creator>mhartl</dc:creator>
				<category><![CDATA[Insoshi]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://insoshi.wordpress.com/?p=134</guid>
		<description><![CDATA[This post answers the question, How do you use Rails to do one thing for robots, and another thing for humans? Why would you want to do this? In our case, the Insoshi home page forwards to a portal page that uses frames in order to have an interface that unifies the sites on the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.mhartl.com&#038;blog=3368824&#038;post=134&#038;subd=insoshi&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>This post answers the question, How do you use Rails to do one thing for robots, and another thing for humans?  </p>
<p>Why would you want to do this?  In our case, the <a href="http://insoshi.com/">Insoshi home page</a> forwards to a <a href="http://portal.insoshi.com/">portal page</a> that uses frames in order to have an interface that unifies the sites on the <tt>insoshi.com</tt> domain with those off-site, such as our <a href="http://github.com/insoshi/insoshi/tree">GitHub repository</a> and <a href="http://insoshi.lighthouseapp.com/projects/9331-insoshi/overview">bug tracker</a>.  The frames page is horribly search-unfriendly, though, so we serve bots the actual content of <tt>http://insoshi.com/home/index</tt> (our routes map <tt>/</tt> to <tt>/home/index</tt>).  (<em>Note</em>:  The front of the portal page as seen by a human is the <em>same</em> as the index page served to bots; be careful about doing anything else, since bots can punish you if you use this technique for anything slimy.)</p>
<p>Our method is to use a before filter in the Home controller.  Here&#8217;s the code (minus some irrelevant bits):</p>
<p><pre class="brush: ruby;">
class HomeController &lt; ApplicationController
  before_filter :forward_nonbots_to_portal, :only =&gt; &quot;index&quot;
  
  .
  .
  .

  private
  
    # Return true if the user agent is a bot.
    def robot?
      bot = /(Baidu|bot|Google|SiteUptime|Slurp|WordPress|ZIBB|ZyBorg)/i
      request.user_agent =~ bot
    end
    
    # Allow an explicit override of the forward_nonbots_to_portal.
    def no_redirect?
      params[:redirect] == 'false' or RAILS_ENV['ENV'] != 'production'
    end
    
    def forward_nonbots_to_portal
      redirect_to &quot;http://portal.insoshi.com&quot; unless robot? or no_redirect?
    end
end
</pre></p>
<p>The key here is the <tt>robot?</tt> method, which has a regex with a list of the most common bots user agents:</p>
<p><pre class="brush: ruby;">
    # Return true if the user agent is a bot.
    def robot?
      bot = /(Baidu|bot|Google|SiteUptime|Slurp|WordPress|ZIBB|ZyBorg)/i
      request.user_agent =~ bot
    end
</pre></p>
<p>If the user <em>isn&#8217;t</em> a bot, we redirect them to the portal.  </p>
<p>N.B.  The second boolean, <tt>no_redirect?</tt>, prevents the forwarding in development mode and also allows us to override the redirect by passing a <tt>redirect=false</tt> parameter.  This latter condition allows us to link directly to the home page, <em>without</em> a redirect, by using <tt>http://insoshi.com/?redirect=false</tt>.  In particular, the portal menu home link itself uses this URL, because otherwise clicking on the home link repeatedly would cause a bunch of nested portal pages to appear.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/insoshi.wordpress.com/134/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/insoshi.wordpress.com/134/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/insoshi.wordpress.com/134/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/insoshi.wordpress.com/134/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/insoshi.wordpress.com/134/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/insoshi.wordpress.com/134/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/insoshi.wordpress.com/134/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/insoshi.wordpress.com/134/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/insoshi.wordpress.com/134/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/insoshi.wordpress.com/134/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/insoshi.wordpress.com/134/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/insoshi.wordpress.com/134/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/insoshi.wordpress.com/134/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/insoshi.wordpress.com/134/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.mhartl.com&#038;blog=3368824&#038;post=134&#038;subd=insoshi&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.mhartl.com/2008/09/26/using-rails-to-serve-different-content-to-humans-and-robots/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/ffda7d145b83c4b118f982401f962ca6?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mhartl</media:title>
		</media:content>
	</item>
		<item>
		<title>Finding and fixing mass assignment problems in Rails applications</title>
		<link>http://blog.mhartl.com/2008/09/21/finding-and-fixing-mass-assignment-problems-in-rails-applications/</link>
		<comments>http://blog.mhartl.com/2008/09/21/finding-and-fixing-mass-assignment-problems-in-rails-applications/#comments</comments>
		<pubDate>Mon, 22 Sep 2008 01:14:11 +0000</pubDate>
		<dc:creator>mhartl</dc:creator>
				<category><![CDATA[Insoshi]]></category>
		<category><![CDATA[mass assignment]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://insoshi.wordpress.com/?p=91</guid>
		<description><![CDATA[Last week I received an email from Eric Chapweske (of Slantwise Design and the Rail Spikes blog) alerting me to mass assignment vulnerabilities in the Insoshi social network sourcecode. (See my post on mass assignment for a quick review of the concept, and don&#8217;t miss Eric&#8217;s mass assignment article for a more thorough treatment.) I [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.mhartl.com&#038;blog=3368824&#038;post=91&#038;subd=insoshi&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Last week I received an email from Eric Chapweske (of <a href="http://slantwisedesign.com/">Slantwise Design</a> and the <a href="http://railspikes.com/">Rail Spikes blog</a>) alerting me to mass assignment vulnerabilities in the <a href="http://insoshi.com/">Insoshi social network</a> sourcecode.  (See my <a href="http://blog.insoshi.com/2008/09/21/mass-assignment-in-rails-applications/">post on mass assignment</a> for a quick review of the concept, and don&#8217;t miss <a href="http://railspikes.com/2008/9/22/is-your-rails-application-safe-from-mass-assignment">Eric&#8217;s mass assignment article</a> for a more thorough treatment.)  I quickly set to work fixing the problems, and within a few hours of receiving the email I&#8217;d pushed out a patched version to the <a href="http://github.com/insoshi/insoshi/tree">Insoshi GitHub repository</a>.  Since the process was so instructive, and since mass assignment vulnerabilities are so common, I thought I&#8217;d share some of the details of what it took to fix them. </p>
<h4>Fixing the models and controllers</h4>
<p>The first step in solving mass assignment problems is to find them, so I whipped up a little <a href="http://github.com/mhartl/find_mass_assignment/tree/master"><tt>find_mass_assignment</tt> plugin</a> to make it easier:</p>
<pre>$ script/plugin install git://github.com/mhartl/find_mass_assignment.git</pre>
<p>(You&#8217;ll need Git and Rails 2.1 or later for this to work.)</p>
<p>This defines a Rake task to find mass assignment vulnerabilities.  (It works by searching through the controllers for likely mass assignment and then looking in the models to see if they <em>don&#8217;t</em> define <tt>attr_accessible</tt>.)  Let&#8217;s run it on the buggy Insoshi code and see what we get:</p>
<pre>$ rake find_mass_assignment

/path/to/app/controllers/activities_controller.rb
    46      @activity = Activity.new(params[:event])
    68        if @activity.update_attributes(params[:event])

/path/to/app/controllers/comments_controller.rb
    20      @comment = parent.comments.new(params[:comment].

/path/to/app/controllers/messages_controller.rb
    50      @message = Message.new(:parent_id    =&gt; original_message.id,
    61      @message = Message.new(params[:message].merge(:sender =&gt; current_person,

/path/to/app/controllers/photos_controller.rb
    39      @photo = Photo.new(params[:photo].merge(person_data))
    61        if @photo.update_attributes(:primary =&gt; true)

/path/to/app/controllers/posts_controller.rb
    59        if @post.update_attributes(params[:post])
    157          post = @topic.posts.new(params[:post].merge(:person =&gt; current_person))
    159          post = @blog.posts.new(params[:post])

/path/to/app/controllers/topics_controller.rb
    28      @topic = @forum.topics.new(params[:topic].merge(:person =&gt; current_person))
    44        if @topic.update_attributes(params[:topic])</pre>
<p>Yikes!  That&#8217;s a lot of problems.  How do we squash all these bugs?</p>
<p>One of the vulnerable models is the Post model, which is the base class for the ForumPost and BlogPost models.  We&#8217;ll use the ForumPost model as our example.  First we disable <tt>attr_accessible</tt> in the Post model, since we want to force all the derived classes to redefine it:</p>
<p><tt>app/models/post.rb</tt><br />
<pre class="brush: ruby;">
class Post &lt; ActiveRecord::Base
  include ActivityLogger
  has_many :activities, :foreign_key =&gt; &quot;item_id&quot;, :dependent =&gt; :destroy
  attr_accessible nil
end
</pre></p>
<p>Then we set <tt>attr_accessible</tt> in the ForumPost model to allow only the post body to be set by mass assignment:</p>
<p><tt>app/models/forum_post.rb</tt><br />
<pre class="brush: ruby;">
class ForumPost &lt; Post
  .
  .
  .

  attr_accessible :body
  
  belongs_to :topic,  :counter_cache =&gt; true
  belongs_to :person, :counter_cache =&gt; true
  
  validates_presence_of :body, :person
  validates_length_of :body, :maximum =&gt; 5000
  .
  .
  .
end
</pre></p>
<p>Then in the Posts controller we update</p>
<p><pre class="brush: ruby;">
  post = @topic.posts.build(params[:post].merge(:person =&gt; current_person))
</pre></p>
<p>to set the person attribute explicitly:</p>
<p><pre class="brush: ruby;">
  post = @topic.posts.build(params[:post])
  post.person = current_person
</pre></p>
<h4>Bypassing <tt>attr_accessible</tt></h4>
<p>This fixes the controller action, but unfortunately the corresponding RSpec specs fail.  Having a good test suite proved invaluable in fixing the mass assignment problems, but the tests use mass assignment themselves, and much of that code fails.  For example, here is part of the Post spec:</p>
<p><tt>spec/models/post_spec.rb</tt><br />
<pre class="brush: ruby;">
describe ForumPost do
  
  before(:each) do
    @post = topics(:one).build(:body =&gt; &quot;Hey there&quot;,
                               :person =&gt; people(:quentin))
  end
  .
  .
  .
end
</pre></p>
<p>This fails because of the attempt to set the <tt>person</tt> attribute by mass assignment.  We <em>could</em> fix this as in the controller:</p>
<p><pre class="brush: ruby;">
describe ForumPost do
  
  before(:each) do
    @post = topics(:one).build(:body =&gt; &quot;Hey there&quot;)
    @post.topics.person = people(:quentin)
  end
  .
  .
  .
end
</pre></p>
<p>Unfortunately, the tests are <em>riddled</em> with this sort of code, and it&#8217;s a nightmare to make all such changes by hand.  Moreover, inside the tests we simply don&#8217;t care about mass assignment vulnerabilities, so making a bunch of cumbersome changes is particularly annoying.  Luckily, there&#8217;s a nice solution; after searching for a bit, I found an <a href="http://pastie.textmate.org/104042">inspiring Pastie</a>, which led me to open up <tt>ActiveRecord::Base</tt> and add some <em>unsafe</em> methods to create Active Record objects that bypass <tt>attr_accessible</tt>:  </p>
<p><tt>config/initializers/unsafe_build_and_create.rb</tt></p>
<p><pre class="brush: ruby;">
class ActiveRecord::Base

  # Build and create records unsafely, bypassing attr_accessible.
  # These methods are especially useful in tests and in the console.
  
  def self.unsafe_build(attrs)
    record = new
    record.unsafe_attributes = attrs
    record
  end
  
  def self.unsafe_create(attrs)
    record = unsafe_build(attrs)
    record.save
    record
  end
  
  def self.unsafe_create!(attrs)
    unsafe_build(attrs).save!
  end

  def unsafe_attributes=(attrs)
    attrs.each do |k, v|
      send(&quot;#{k}=&quot;, v)
    end
  end
end
</pre></p>
<p>(By putting in the <tt>config/initializers/</tt> directory, we ensure that the additions will be loaded automatically as part of the Rails environment.)</p>
<p>With these methods in hand, we still have to update the tests by hand, but the edits are much simpler (and many can be done by search-and-replace):</p>
<p><pre class="brush: ruby;">
describe ForumPost do
  
  before(:each) do
    @post = topics(:one).unsafe_build(:body =&gt; &quot;Hey there&quot;,
                                      :person =&gt; people(:quentin))
  end
  .
  .
  .
end
</pre></p>
<p>We can use these methods in the controllers, too, of course, but if we do the word &#8220;unsafe&#8221; serves as a constant reminder that we&#8217;d better be <em>really</em> sure we want to bypass <tt>attr_accessible</tt>.</p>
<p>After making all the fixes, running our Rake task shows only one potentially vulnerable model:</p>
<pre>$ rake find_mass_assignment
/Users/mhartl/rails/insoshi_core/app/controllers/photos_controller.rb
    40      @photo = Photo.new(params[:photo].merge(person_data))
    62        if @photo.update_attributes(:primary =&gt; true)
</pre>
<p>Checking the Photo model, we see that it defines <tt>attr_protected</tt> instead of <tt>attr_accessible</tt> (and explains why):</p>
<p><tt>app/models/photo.rb</tt><br />
<pre class="brush: ruby;">
class Photo &lt; ActiveRecord::Base
  include ActivityLogger
  UPLOAD_LIMIT = 5 # megabytes
  
  # attr_accessible is a nightmare with attachment_fu, so use
  # attr_protected instead.
  attr_protected :id, :person_id, :parent_id, :created_at, :updated_at
  .
  .
  .
end
</pre></p>
<p>With that, we&#8217;re done, and our application is secure.  Huzzah!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/insoshi.wordpress.com/91/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/insoshi.wordpress.com/91/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/insoshi.wordpress.com/91/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/insoshi.wordpress.com/91/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/insoshi.wordpress.com/91/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/insoshi.wordpress.com/91/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/insoshi.wordpress.com/91/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/insoshi.wordpress.com/91/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/insoshi.wordpress.com/91/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/insoshi.wordpress.com/91/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/insoshi.wordpress.com/91/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/insoshi.wordpress.com/91/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/insoshi.wordpress.com/91/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/insoshi.wordpress.com/91/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.mhartl.com&#038;blog=3368824&#038;post=91&#038;subd=insoshi&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.mhartl.com/2008/09/21/finding-and-fixing-mass-assignment-problems-in-rails-applications/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/ffda7d145b83c4b118f982401f962ca6?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mhartl</media:title>
		</media:content>
	</item>
		<item>
		<title>Mass assignment in Rails applications</title>
		<link>http://blog.mhartl.com/2008/09/21/mass-assignment-in-rails-applications/</link>
		<comments>http://blog.mhartl.com/2008/09/21/mass-assignment-in-rails-applications/#comments</comments>
		<pubDate>Mon, 22 Sep 2008 01:13:54 +0000</pubDate>
		<dc:creator>mhartl</dc:creator>
				<category><![CDATA[mass assignment]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://insoshi.wordpress.com/?p=82</guid>
		<description><![CDATA[This is a brief review of mass assignment in Rails. See the follow-up post on Finding and fixing mass assignment problems in Rails applications for some more tips on how to find and fix mass assignment problems. We&#8217;ll begin with a simple example. Suppose an application has a User model that looks like this: Note [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.mhartl.com&#038;blog=3368824&#038;post=82&#038;subd=insoshi&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>This is a brief review of mass assignment in Rails.  See the follow-up post on <a href="http://blog.insoshi.com/2008/09/21/finding-and-fixing-mass-assignment-problems-in-rails-applications/">Finding and fixing mass assignment problems in Rails applications</a> for some more tips on how to find and fix mass assignment problems.</p>
<p>We&#8217;ll begin with a simple example.  Suppose an application has a User model that looks like this:</p>
<p><pre class="brush: ruby;">
# == Schema Information
# Table name: users
#
#  id                         :integer(11)     not null, primary key
#  email                      :string(255)     
#  name                       :string(255)     
#  password                   :string(255)
#  admin                      :boolean(1)      not null
class User &lt; ActiveRecord::Base
  validates_presence_of :email, :password
  validates_uniqueness_of :email
  .
  .
  .
end
</pre></p>
<p>Note the presence of an <tt>admin</tt> boolean to identify administrative users.  With this model, the Users controller might have this standard update code:</p>
<p><pre class="brush: ruby;">
  def update
    @user = User.find(params[:id])

    respond_to do |format|
      if @user.update_attributes(params[:user])
        flash[:notice] = 'User was successfully updated.'
        format.html { redirect_to(@user) }
      else
        format.html { render :action =&gt; &quot;edit&quot; }
      end
    end
  end
</pre></p>
<p>This works fine, but note that the line</p>
<p><pre class="brush: ruby;">
  if @user.update_attributes(params[:user])
</pre></p>
<p>performs an update to the <tt>@user</tt> object through the <tt>params</tt> hash, assigning <em>all</em> the <tt>@user</tt> attributes at once&mdash;that is, as a <em>mass assignment</em>.</p>
<p>The problem with mass assignment is that some malicious [cr|h]acker might write a script to PUT something like <tt>name=New+Name&amp;admin=1</tt>, thereby adding himself as an administrative user!  This would be a Bad Thing&trade;. The standard solution to this problem is to use <tt>attr_accessible</tt> in the model to declare explicitly the attributes that can be modified by mass assignment.  To protect our User model, for example, we would write</p>
<p><pre class="brush: ruby;">
class User &lt; ActiveRecord::Base

  attr_accessible :email, :name, :password

  validates_presence_of :email, :password
  validates_uniqueness_of :email
  .
  .
  .
end
</pre></p>
<p>Since <tt>:admin</tt> isn&#8217;t included in the <tt>attr_accessible</tt> argument list, the User model&#8217;s <tt>admin</tt> attribute is safe from unwanted modification.</p>
<p>This seems simple enough, but the rub is that <em>remembering</em> to protect against mass assignment is difficult.  Using mass assignment doesn&#8217;t affect the normal operations of the site, so it&#8217;s hard to notice the problem.  Moreover, although you could shut off mass assignment globally, often there are many models that are used internally and never get modified directly by a web interface.  Not being able to use mass assignment for these models is inconvenient, and manually making all attributes <tt>attr_accessible</tt> is cumbersome and error-prone.  So, what&#8217;s a Rails developer to do?  </p>
<p>Spurred by an email from Eric Chapweske of <a href="http://slantwisedesign.com/">Slantwise Design</a>, I recently <a href="http://blog.insoshi.com/2008/09/21/finding-and-fixing-mass-assignment-problems-in-rails-applications/">audited the Insoshi social network for mass assignment vulnerabilities</a>.  Doing this manually was annoying, so in the process I developed a simple plugin to find likely vulnerabilities automatically, by searching through the controllers for likely mass assignment and then looking in the models to see if they <em>didn&#8217;t</em> define <tt>attr_accessible</tt>.  The result is a list of potential trouble spots.</p>
<p>To use the <a href="http://github.com/mhartl/find_mass_assignment/tree/master"><tt>find_mass_assignment</tt> plugin</a>, simply install it from GitHub as follows:</p>
<pre>$ script/plugin install git://github.com/mhartl/find_mass_assignment.git</pre>
<p>(You&#8217;ll need Git and Rails 2.1 or later for this to work.)  The plugin defines a Rake task to find mass assignment vulnerabilities; running it on the example Users controller from above would yield the following:</p>
<pre>$ rake find_mass_assignment

/path/to/app/controllers/users_controller.rb
  5  if @user.update_attributes(params[:user])</pre>
<p>This tells us that line 5 in the Users controller has a likely mass assignment vulnerability.  </p>
<p>The <tt>find_mass_assignment</tt> plugin doesn&#8217;t fix mass assignment problems automatically, but by making it more convenient to find them I hope it can significantly improve the odds that they will be caught (and fixed!) quickly.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/insoshi.wordpress.com/82/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/insoshi.wordpress.com/82/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/insoshi.wordpress.com/82/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/insoshi.wordpress.com/82/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/insoshi.wordpress.com/82/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/insoshi.wordpress.com/82/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/insoshi.wordpress.com/82/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/insoshi.wordpress.com/82/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/insoshi.wordpress.com/82/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/insoshi.wordpress.com/82/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/insoshi.wordpress.com/82/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/insoshi.wordpress.com/82/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/insoshi.wordpress.com/82/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/insoshi.wordpress.com/82/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.mhartl.com&#038;blog=3368824&#038;post=82&#038;subd=insoshi&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.mhartl.com/2008/09/21/mass-assignment-in-rails-applications/feed/</wfw:commentRss>
		<slash:comments>33</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/ffda7d145b83c4b118f982401f962ca6?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mhartl</media:title>
		</media:content>
	</item>
	</channel>
</rss>
