<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" version="2.0">

<channel>
	<title>Mike's stuff</title>
	
	<link>http://www.mfischer.com/wordpress</link>
	<description>Things from Mike's brain.</description>
	<lastBuildDate>Tue, 09 Feb 2010 22:59:04 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/blogspot/mikefischer99" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="blogspot/mikefischer99" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><feedburner:emailServiceId xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">blogspot/mikefischer99</feedburner:emailServiceId><feedburner:feedburnerHostname xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">http://feedburner.google.com</feedburner:feedburnerHostname><item>
		<title>Snowpocalypse</title>
		<link>http://www.mfischer.com/wordpress/2010/02/09/snowpocalypse/</link>
		<comments>http://www.mfischer.com/wordpress/2010/02/09/snowpocalypse/#comments</comments>
		<pubDate>Tue, 09 Feb 2010 22:59:04 +0000</pubDate>
		<dc:creator>mike</dc:creator>
				<category><![CDATA[dogs]]></category>
		<category><![CDATA[photography]]></category>
		<category><![CDATA[weather]]></category>

		<guid isPermaLink="false">http://www.mfischer.com/wordpress/?p=454</guid>
		<description><![CDATA[We didn&#8217;t panic, but we did lose power for two days (and went through pretty severe gadget and internet withdrawal, probably more-so than heat withdrawal). Between the news, facebook, twitter, and everything else, the big storm (32.4 inches of snow here in around 36 hours) has been well covered, so instead of going on about [...]]]></description>
			<content:encoded><![CDATA[<p>We didn&#8217;t panic, but we did lose power for two days (and went through pretty severe gadget and internet withdrawal, probably more-so than heat withdrawal). Between the news, facebook, twitter, and everything else, the big storm (32.4 inches of snow here in around 36 hours) has been well covered, so instead of going on about it, I&#8217;m just going to post some photos from the big event (and a couple from the December snowstorm).</p>
<p><a href="http://www.mfischer.com/wordpress/wp-content/uploads/2010/02/2009-12-19-05403.jpg"><img src="http://www.mfischer.com/wordpress/wp-content/uploads/2010/02/2009-12-19-05403-300x199.jpg" alt="2009-12-19-05403" title="2009-12-19-05403" width="300" height="199" class="center size-medium wp-image-455" /></a></p>
<p><a href="http://www.mfischer.com/wordpress/wp-content/uploads/2010/02/2009-12-19-05446.jpg"><img src="http://www.mfischer.com/wordpress/wp-content/uploads/2010/02/2009-12-19-05446-300x199.jpg" alt="2009-12-19-05446" title="2009-12-19-05446" width="300" height="199" class="center size-medium wp-image-456" /></a></p>
<p><a href="http://www.mfischer.com/wordpress/wp-content/uploads/2010/02/DSC_4869.jpg"><img src="http://www.mfischer.com/wordpress/wp-content/uploads/2010/02/DSC_4869-300x199.jpg" alt="DSC_4869" title="DSC_4869" width="300" height="199" class="center size-medium wp-image-457" /></a></p>
<p><a href="http://www.mfischer.com/wordpress/wp-content/uploads/2010/02/DSC_4889.jpg"><img src="http://www.mfischer.com/wordpress/wp-content/uploads/2010/02/DSC_4889-300x199.jpg" alt="DSC_4889" title="DSC_4889" width="300" height="199" class="center size-medium wp-image-458" /></a></p>
<p><a href="http://www.mfischer.com/wordpress/wp-content/uploads/2010/02/DSC_4894.jpg"><img src="http://www.mfischer.com/wordpress/wp-content/uploads/2010/02/DSC_4894-300x199.jpg" alt="DSC_4894" title="DSC_4894" width="300" height="199" class="center size-medium wp-image-459" /></a></p>
<p><a href="http://www.mfischer.com/wordpress/wp-content/uploads/2010/02/DSC_4899.jpg"><img src="http://www.mfischer.com/wordpress/wp-content/uploads/2010/02/DSC_4899-300x199.jpg" alt="DSC_4899" title="DSC_4899" width="300" height="199" class="center size-medium wp-image-460" /></a></p>
<p><a href="http://www.mfischer.com/wordpress/wp-content/uploads/2010/02/DSC_4902.jpg"><img src="http://www.mfischer.com/wordpress/wp-content/uploads/2010/02/DSC_4902-199x300.jpg" alt="DSC_4902" title="DSC_4902" width="199" height="300" class="center size-medium wp-image-461" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.mfischer.com/wordpress/2010/02/09/snowpocalypse/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Blog Bifurcation</title>
		<link>http://www.mfischer.com/wordpress/2010/01/31/blog-bifurcation/</link>
		<comments>http://www.mfischer.com/wordpress/2010/01/31/blog-bifurcation/#comments</comments>
		<pubDate>Mon, 01 Feb 2010 02:53:44 +0000</pubDate>
		<dc:creator>mike</dc:creator>
				<category><![CDATA[computing]]></category>

		<guid isPermaLink="false">http://www.mfischer.com/wordpress/?p=447</guid>
		<description><![CDATA[Originally this blog started out as a fairly personal journal, but has recently adopted a more technology-oriented theme. I plan to focus even more on technology blogging in the future, but I also want to occasionally blog about personal happenings, so I&#8217;ve decided to split my blog in two.
This site, mfischer.com, will return to its [...]]]></description>
			<content:encoded><![CDATA[<p>Originally this blog started out as a fairly personal journal, but has recently adopted a more technology-oriented theme. I plan to focus even more on technology blogging in the future, but I also want to occasionally blog about personal happenings, so I&#8217;ve decided to split my blog in two.</p>
<p>This site, <em><a href="http://mfischer.com/">mfischer.com</a></em>, will return to its roots as the personal blog about Mike Fischer. To publish my technology articles I&#8217;ve created a new blog under a new domain: <em><a href="http://thelastpixel.net/">thelastpixel.net</a></em>. I&#8217;ve just gotten started with it, but plan to post regularly on software development and other technology issues. If you&#8217;re interested, check it out and sign up for the RSS subscription! If not, just keep watching here for more non-technical news from my life&#8230;</p>
<p><a href="http://thelastpixel.net"><img src="http://www.mfischer.com/wordpress/wp-content/uploads/2010/01/the-last-pixel.png" alt="the-last-pixel" title="the-last-pixel" width="489" height="674" class="center size-full wp-image-449" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.mfischer.com/wordpress/2010/01/31/blog-bifurcation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Pet scan #8</title>
		<link>http://www.mfischer.com/wordpress/2009/10/10/pet-scan-8/</link>
		<comments>http://www.mfischer.com/wordpress/2009/10/10/pet-scan-8/#comments</comments>
		<pubDate>Sat, 10 Oct 2009 12:08:58 +0000</pubDate>
		<dc:creator>mike</dc:creator>
				<category><![CDATA[cancer]]></category>

		<guid isPermaLink="false">http://www.mfischer.com/wordpress/?p=443</guid>
		<description><![CDATA[I&#8217;ve become such a bad blogger: months between updates, and forgetting to mention things like my semi-annual PET scans.  After all, cancer is what prompted me to start this blog in the first place, almost exactly three years ago!
Well, in August I had my scan, and as usual, everything turned out fine.  Next [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve become such a bad blogger: months between updates, and forgetting to mention things like my semi-annual PET scans.  After all, cancer is what prompted me to start this blog in the first place, almost exactly three years ago!</p>
<p>Well, in August I had my scan, and as usual, everything turned out fine.  Next scan in January.  Happy to say that the last two and a half years have probably been my healthiest of the last 20!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mfischer.com/wordpress/2009/10/10/pet-scan-8/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Essential Ruby on Rails plug-ins and gems</title>
		<link>http://www.mfischer.com/wordpress/2009/07/13/essential-ruby-on-rails-plug-ins-and-gems/</link>
		<comments>http://www.mfischer.com/wordpress/2009/07/13/essential-ruby-on-rails-plug-ins-and-gems/#comments</comments>
		<pubDate>Mon, 13 Jul 2009 23:19:35 +0000</pubDate>
		<dc:creator>mike</dc:creator>
				<category><![CDATA[computing]]></category>

		<guid isPermaLink="false">http://www.mfischer.com/?p=427</guid>
		<description><![CDATA[Now that I&#8217;ve been using Rails for a while, I&#8217;ve accumulated a few gems and plugins that I end up using over and over, so I thought I&#8217;d share a little about each.
haml and sass
I was sold on Sass from the first few minutes I used it.  Sass lets you write CSS using a [...]]]></description>
			<content:encoded><![CDATA[<p>Now that I&#8217;ve been using Rails for a while, I&#8217;ve accumulated a few gems and plugins that I end up using over and over, so I thought I&#8217;d share a little about each.</p>
<p><strong><a href="http://haml.hamptoncatlin.com/">haml</a> and <a href="http://haml.hamptoncatlin.com/docs/rdoc/classes/Sass.html">sass</a></strong></p>
<p>I was sold on Sass from the first few minutes I used it.  Sass lets you write CSS using a simplified abstraction format, and compiles your sass code into CSS.  Best of all, it can be used without Rails (or Ruby), so I&#8217;ve been using it with static web pages as well.  Here&#8217;s an excellent example from the sass web site:</p>
<p><img src="http://www.mfischer.com/wordpress/wp-content/uploads/2009/07/haml-sass.png" alt="haml-sass" title="haml-sass" width="502" height="336" class="aligncenter size-full wp-image-429" /></p>
<p>Haml does the same for HTML.  It took me a little longer to get used to, probably because it&#8217;s actually a replacement for HTML and Embedded Ruby (ERB).  Haml really cuts down on the amount of typing needed to generate HTML, auto-closes tags (based on indentation), and makes it far easier to see the structure of your document.  This snapshot from the haml page is a great demonstration:</p>
<p><img src="http://www.mfischer.com/wordpress/wp-content/uploads/2009/07/haml.png" alt="haml" title="haml" width="500" height="198" class="aligncenter size-full wp-image-432" /></p>
<p><strong><a href="http://agilewebdevelopment.com/plugins/friendly_id">friendly_id</a></strong></p>
<p>This extends Rails&#8217; RESTful resources by making URLs look &#8220;friendlier&#8221;, using a user-specified attribute of a model instead of the auto-generated numeric ID.  In other words, by simply adding a statement like <code>has_friendly_id :name</code> to a <em>users</em> model, friendly_id converts URLs from <code>http://my.site.com/users/17</code> into the much nicer <code>http://my.site.com/users/mike</code>.</p>
<p><a href="http://github.com/binarylogic/authlogic/tree/master"><strong>Authlogic</strong></a></p>
<p>This is the leading user authentication framework for Rails.  It you need to allow users to register, sign in, change passwords, use OpenID, user LDAP, etc., this is the plug-in for you.  It takes some time to learn how to integrate it, since each web site&#8217;s implementation is going to be a little different.</p>
<p><a href="http://github.com/mislav/will_paginate/tree/master"><strong>mislav-will_paginate</strong></a></p>
<p>This makes it dead simple to take a model with many records, and display it in pages, where you can control how many are displayed per page, and the style of the page links:</p>
<p><img src="http://www.mfischer.com/wordpress/wp-content/uploads/2009/07/pagination.png" alt="pagination" title="pagination" width="300" height="37" class="aligncenter size-full wp-image-439" /></p>
<p><a href="http://thoughtbot.com/projects/paperclip"><strong>thoughtbot-paperclip</strong></a></p>
<p>I&#8217;ve researched and tried many file upload/attachment gems but Paperclip is the one I settled on.  It seems to be the most actively maintained, and one of the most flexible.  File uploading is still not as easy as it should be, especially if you want to provide feedback during upload (usually via a Flash plugin) or allow multiple attachments to a model (usually with an associated polymorphic upload model).  Paperclip doesn&#8217;t do these things out of the box, but it&#8217;s possible to wrangle it into submission&#8230;</p>
<p><a href="http://github.com/brianjlandau/unobtrusive_date_picker/tree/master"><strong>unobtrusive_date_picker</strong></a></p>
<p>These days there&#8217;s no reason to make your users select a month, day, and year from three separate drop-down boxes, or type a date in some pre-determined format.  This plug-in automagically adds a pop-up calendar to your date selection input fields.</p>
<p><img src="http://www.mfischer.com/wordpress/wp-content/uploads/2009/07/date-picker.png" alt="date-picker" title="date-picker" width="470" height="303" class="aligncenter size-full wp-image-440" /></p>
<p><a href="http://ennerchi.com/projects/jrails"><strong>jRails</strong></a></p>
<p>When I started learning Javascript a few years ago, it was a huge pain in the neck.  I quickly came across <a href="http://www.prototypejs.org/">Prototype</a>, which made my life much easier.  Before I got too far along with that however, I found <a href="http://jquery.com/">jQuery</a> and fell instantly in love.  This was clearly the way Javascript programming was meant to be.  I used it for everything.  Then last year when I started learning Rails, I was a little disappointed to find that the javascript library built-in to Rails was Prototype.  Fortunately, others apparently had the same complaint, and created JRails, which integrates jQuery with Rails.</p>
<p><strong>Others</strong></p>
<p>There are a few other gems and plugins I use, but not as frequently.  They include: <a href="http://maruku.rubyforge.org/">Maruku</a>, <a href="http://opensoul.org/tags/acts_as_audited">acts_as_audited</a>, <a href="http://mechanize.rubyforge.org/mechanize/">mechanize</a>, and <a href="http://wiki.github.com/why/hpricot">hpricot</a>.</p>
<p>Am I missing out on any must-have gems or plug-ins?  Let me know in the comments!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mfischer.com/wordpress/2009/07/13/essential-ruby-on-rails-plug-ins-and-gems/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>New bikes, iPhone tracking</title>
		<link>http://www.mfischer.com/wordpress/2009/05/25/new-bikes-iphone-tracking/</link>
		<comments>http://www.mfischer.com/wordpress/2009/05/25/new-bikes-iphone-tracking/#comments</comments>
		<pubDate>Mon, 25 May 2009 16:37:42 +0000</pubDate>
		<dc:creator>mike</dc:creator>
				<category><![CDATA[biking]]></category>

		<guid isPermaLink="false">http://www.mfischer.com/?p=415</guid>
		<description><![CDATA[ Kat and I have traded in our bikes for better models, only a couple weeks after buying them.  We both got really good deals from Performance Bike in Reston on the upgrades.  The new bikes are 21- and 24- speed instead of 7-speed, have front shock absorbers, and higher quality shifters, gears, [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.mfischer.com/wordpress/wp-content/uploads/2009/05/2009-05-25-05359.jpg"><img src="http://www.mfischer.com/wordpress/wp-content/uploads/2009/05/2009-05-25-05359-300x199.jpg" alt="Our bikes" title="2009-05-25-05359" width="300" height="199" class="alignleft size-medium wp-image-420" /></a> Kat and I have traded in our bikes for better models, only a couple weeks after buying them.  We both got really good deals from <a href="http://www.performancebike.com/">Performance Bike</a> in Reston on the upgrades.  The new bikes are 21- and 24- speed instead of 7-speed, have front shock absorbers, and higher quality shifters, gears, brakes, and other parts.  The additional gears are essential for dealing with the long and steep hills on the Fairfax Parkway trail.</p>
<p>I also have a new iPhone, and downloaded a great free application that uses the GPS to record your bike rides and map them on Google Maps.  Below shows the route that was recorded yesterday when Kat and I rode over 21 miles (round trip) to Leesburg and back on the W&#038;OD trail.  We kept our pace brisk, and it was a really good workout &#8212; almost 1,800 calories for me according to the bike computer. I&#8217;ve now ridden around 100 miles total, and Kat&#8217;s gone further since she&#8217;s done some biking trips while I&#8217;ve been at work.  She&#8217;s been logging our efforts <a href="http://spreadsheets.google.com/pub?key=rNZzVyNanhRYOTLfsuKlgXA">in an online document here</a>.</p>
<p><a href="http://www.everytrail.com/view_trip.php?trip_id=217595">W&#038;OD from Rt. 28 to Leesburg and back</a><br /><iframe src="http://www.everytrail.com/iframe2.php?trip_id=217595&#038;width=415&#038;height=300" marginheight=0 marginwidth=0 frameborder=0 scrolling=no width=415 height=300></iframe></p>
<p>I also rode to work last Monday, which was a lot of fun, but logistically complicated (backpack full of clothes, shower at work, etc.).  It took me about 40 minutes to get to work, vs. the usual 15-20 when driving.  But adding in all the prep time and shower time at work makes it more like 60, so 3-4 times as long.  I&#8217;ll probably do it occasionally, but I don&#8217;t think it&#8217;ll become my primary way of getting to work.</p>
<p>I&#8217;m really enjoying biking, and am glad that we decided to get bikes this year.  There are a lot of bike trails in Virginia, so we&#8217;ll probably find a way to start exploring more of them.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mfischer.com/wordpress/2009/05/25/new-bikes-iphone-tracking/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Summer cycling</title>
		<link>http://www.mfischer.com/wordpress/2009/04/25/summer-cycling/</link>
		<comments>http://www.mfischer.com/wordpress/2009/04/25/summer-cycling/#comments</comments>
		<pubDate>Sat, 25 Apr 2009 17:20:12 +0000</pubDate>
		<dc:creator>mike</dc:creator>
				<category><![CDATA[biking]]></category>
		<category><![CDATA[food/wine]]></category>
		<category><![CDATA[work]]></category>

		<guid isPermaLink="false">http://www.mfischer.com/?p=407</guid>
		<description><![CDATA[Kat and I bought bicycles last weekend, and are hoping to use them to get outdoors a bit more than we usually do.  So far it&#8217;s been quite fun, and we&#8217;re looking forward to exploring lots of the W&#038;OD trail and other areas.  I took off work Thursday and we had our first [...]]]></description>
			<content:encoded><![CDATA[<p>Kat and I bought bicycles last weekend, and are hoping to use them to get outdoors a bit more than we usually do.  So far it&#8217;s been quite fun, and we&#8217;re looking forward to exploring lots of the <a href="http://www.nvrpa.org/parks/wod/">W&#038;OD trail</a> and other areas.  I took off work Thursday and we had our first test-ride with <a href="http://sgx2.sgdragons.com/">Scott</a>, going around 12 miles on the trail, from the Rt 28 / AOL entrance, to the <a href="http://luckstone.com/crushed/locations/location.php?plantid=31">LuckStone Quarry overlook</a> and back.  Of course we had to finish it off with a chocolate malt and food at <a href="http://cheeburger.com/stepinside/stepinside.asp">Cheeburger Cheeburger</a>!</p>
<p><a href="http://www.mfischer.com/wordpress/wp-content/uploads/2009/04/2009-04-25-05296.jpg"><img src="http://www.mfischer.com/wordpress/wp-content/uploads/2009/04/2009-04-25-05296-225x300.jpg" alt="2009-04-25-05296" title="2009-04-25-05296" width="225" height="300" class="alignright size-medium wp-image-409" /></a></p>
<p>Today we left the car in the garage and rode from the house to my office in Herndon.  I&#8217;d been toying with the idea of biking to work occasionally, but Kathie had the good idea of trying it together during a weekend first.  It was 7.1 miles each way, and surprisingly almost all of it was on residential roads, the Fairfax County Parkway Trail (really just a wide sidewalk along the parkway), and the W&#038;OD.  There&#8217;s also a short jaunt through the woods at the end of our street, a 100-yard path that saves 1.5 miles and eliminates &#8220;roads you&#8217;d be crazy to bike on&#8221; like Seneca Road, Georgetown Pike, and Rt. 7.</p>
<p>The last mile of the trip, on Van Buren St. was more of a challenge, since we had to choose between the sidewalk or the road with no shoulder.  We tried both, and while the road was fine on a Saturday morning at 9am, I&#8217;m not sure if I want to do that during rush hour on a weekday.  Fortunately the speed limit on the road is 25mph and it&#8217;s aggressively enforced (don&#8217;t speed in Herndon!).  Bicycles are allowed on sidewalks in Virginia (yield to pedestrians) so I&#8217;ll have to try both.  It was a fun morning, and we&#8217;re both looking forward to a lot more biking!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mfischer.com/wordpress/2009/04/25/summer-cycling/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Aspen Skiing</title>
		<link>http://www.mfischer.com/wordpress/2009/02/25/aspen-skiing/</link>
		<comments>http://www.mfischer.com/wordpress/2009/02/25/aspen-skiing/#comments</comments>
		<pubDate>Wed, 25 Feb 2009 14:47:46 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Friends]]></category>
		<category><![CDATA[photography]]></category>
		<category><![CDATA[vacation]]></category>

		<guid isPermaLink="false">http://www.mfischer.com/?p=401</guid>
		<description><![CDATA[Kathie and I just returned from a great vacation: Skiing in Aspen, Colorado!  Dan graciously invited us and some other friends to stay at his family&#8217;s condo, which was in a great location, convenient to everything in Aspen.
Kathie&#8217;s blog has our day-by-day journal, so I&#8217;m not going to repeat everything here&#8230; just click through [...]]]></description>
			<content:encoded><![CDATA[<p>Kathie and I just returned from a great vacation: Skiing in Aspen, Colorado!  Dan graciously invited us and some other friends to stay at his family&#8217;s condo, which was in a great location, convenient to everything in Aspen.</p>
<p><a href="http://kathiefischer.blogspot.com/search?updated-max=2009-02-26T17%3A12%3A00-05%3A00&#038;max-results=4">Kathie&#8217;s blog</a> has our day-by-day journal, so I&#8217;m not going to repeat everything here&#8230; just click through and check out the story there!  We&#8217;ve also uploaded our vacation photos to an <a href="http://mfischer.com/gallery2/v/mikekathie/travels/aspen2009/">Aspen Vacation album</a> in our Gallery.</p>
<p><img src="http://www.mfischer.com/wordpress/wp-content/uploads/2009/02/2009-02-22-05282-300x225.jpg" alt="Mike and Kat in Aspen" title="Mike and Kat in Aspen" width="300" height="225" class="center size-medium wp-image-402" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.mfischer.com/wordpress/2009/02/25/aspen-skiing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Multiple image upload and crop with Rails</title>
		<link>http://www.mfischer.com/wordpress/2009/02/02/multiple-image-upload-and-crop-with-rails/</link>
		<comments>http://www.mfischer.com/wordpress/2009/02/02/multiple-image-upload-and-crop-with-rails/#comments</comments>
		<pubDate>Tue, 03 Feb 2009 00:25:32 +0000</pubDate>
		<dc:creator>mike</dc:creator>
				<category><![CDATA[computing]]></category>

		<guid isPermaLink="false">http://www.mfischer.com/?p=356</guid>
		<description><![CDATA[For a project I&#8217;m currently working on, I needed a way to upload several photos to attach to a &#8220;dog&#8221; model, and allow easy cropping of the photos.  In Ruby on Rails, this turned out to be harder than I expected.
The main problems I encountered were:

There are several attachment uploading plug-ins for Rails, like [...]]]></description>
			<content:encoded><![CDATA[<p>For a project I&#8217;m currently working on, I needed a way to upload several photos to attach to a &#8220;dog&#8221; model, and allow easy cropping of the photos.  In Ruby on Rails, this turned out to be harder than I expected.</p>
<p>The main problems I encountered were:</p>
<ol>
<li>There are several attachment uploading plug-ins for Rails, like attachment_fu, Paperclip, or UploadColumn, with various features and complexities, but none had good references on handling multiple uploads associated with one model instance.</li>
<li>To add multiple attachments to an object requires a separate model for attachments, associated with the main model via a has_many/belongs_to relationship. This isn&#8217;t hard if the model views are kept separate and RESTful (create a new dog, then add a photo, then add another photo, etc).  That&#8217;s not a good user experience however &#8212; I want to be able to add a new dog, <em>and</em> upload many associated photos at the same time.</li>
<li>It turns out that complex forms to handle multiple associated models is pretty tricky in Rails (but should get better with Rails 2.3, coming soon).</li>
<li>I also wanted to let the user manually crop the photos after upload, to just select the dog&#8217;s face for example.</li>
</ol>
<p>After several weekends of experimenting, reading lots of blogs and tutorials, and playing with various plug-ins, I finally have a working solution I&#8217;m pretty happy with.  Here&#8217;s what I ended up doing:</p>
<ul>
<li>I&#8217;m using the <a href="http://www.thoughtbot.com/projects/paperclip">Paperclip</a> gem to handle the actual uploads.  The main reason I chose this over other options was that Paperclip lets me easily create additional &#8220;styles&#8221; of an uploaded photo, like a small thumbnail, a page-sized version, etc.</li>
<li>I&#8217;m using the <a href="http://jamesgolick.com/2007/12/5/introducing-attributefu-multi-model-forms-made-easy">attribute_fu</a> plug-in to handle the multi-model forms so I can upload any number of images at the same time as a new dog is created.</li>
<li>I&#8217;m using the <a href="http://www.defusion.org.uk/code/javascript-image-cropper-ui-using-prototype-scriptaculous/">jsCropperUI</a> JavaScript library to let the user select an area to crop.</li>
<li>Since Paperclip requires <a href="http://www.imagemagick.org/script/index.php">ImageMagick</a> to do its resizing, I installed <a href="http://rmagick.rubyforge.org/">RMagick</a> and use that for my cropping.</li>
</ul>
<h2>Getting started</h2>
<p>I have two models: &#8220;Dog&#8221; and &#8220;Upload&#8221;.  I started by calling my second model &#8220;Attachment&#8221;, but after running into problems, I learned that Paperclip uses that name internally and will not work correctly if you name your model &#8220;Attachment&#8221;.</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="color: #FCFFBA;">$ rails dogs
$ cd dogs
$ script<span style="color:#006600; font-weight:bold;color: #CCC;">/</span>plugin install git:<span style="color:#006600; font-weight:bold;color: #CCC;">//</span>github.<span style="color:#9900CC;color: #f6f;">com</span><span style="color:#006600; font-weight:bold;color: #CCC;">/</span>giraffesoft<span style="color:#006600; font-weight:bold;color: #CCC;">/</span>attribute_fu.<span style="color:#9900CC;color: #f6f;">git</span>
$ script<span style="color:#006600; font-weight:bold;color: #CCC;">/</span>generate model dog name:string
$ script<span style="color:#006600; font-weight:bold;color: #CCC;">/</span>generate model upload description:string dog_id:integer \
         photo_file_name:string photo_file_size:integer
$ script<span style="color:#006600; font-weight:bold;color: #CCC;">/</span>generate controller dogs
$ script<span style="color:#006600; font-weight:bold;color: #CCC;">/</span>generate controller uploads</pre></div></div>

<h2>Models</h2>
<p>The Dog model just has a name for now.  The Upload model will represent each uploaded photo.  It has a description which the user can enter, a dog_id foreign key, and two attributes for Paperclip: a photo_file_name string and a photo_file_size integer.  See the Paperclip documentation for more details on how these (and other) special attributes are used.</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="color: #FCFFBA;"><span style="color:#008000; font-style:italic;color: #CDC;"># models/dog.rb</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;color: #B83A24;">class</span> Dog <span style="color:#006600; font-weight:bold;color: #CCC;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  <span style="color:#5A0A0A; font-weight:bold;color: #9bf;">has_many</span> <span style="color:#ff3333; font-weight:bold;">:uploads</span>,
           <span style="color:#ff3333; font-weight:bold;">:attributes</span> <span style="color:#006600; font-weight:bold;color: #CCC;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;color: #577A61;">true</span>,
           <span style="color:#ff3333; font-weight:bold;">:discard_if</span> <span style="color:#006600; font-weight:bold;color: #CCC;">=&gt;</span> <span style="color:#CC0066; font-weight:bold;color: #8FB394;">proc</span> <span style="color:#006600; font-weight:bold;color: #CCC;">&#123;</span> <span style="color:#006600; font-weight:bold;color: #CCC;">|</span>upload<span style="color:#006600; font-weight:bold;color: #CCC;">|</span> upload.<span style="color:#9900CC;color: #f6f;">photo_file_size</span>.<span style="color:#0000FF; font-weight:bold;color: #577A61;">nil</span>? <span style="color:#006600; font-weight:bold;color: #CCC;">&#125;</span>
<span style="color:#9966CC; font-weight:bold;color: #B83A24;">end</span></pre></div></div>

<p>I updated the Dog model to have many uploads.  The &#8220;:attributes&#8221; parameter is part of attribute_fu, and lets the Dog controller create associated models at the same time a new Dog is created.  The &#8220;:discard_if&#8221; tells attribute_fu not to create an associated model if certain conditions are met &#8212; in this case, if there&#8217;s no file that was uploaded.  This is needed because we may put many file upload fields in the &#8220;new Dog&#8221; form, but a user may only select one or two photos.  In that case, all the blank form elements would be submitted too, and created as empty Upload instances.</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="color: #FCFFBA;"><span style="color:#008000; font-style:italic;color: #CDC;">#models/upload.rb</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;color: #B83A24;">class</span> Upload <span style="color:#006600; font-weight:bold;color: #CCC;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  <span style="color:#5A0A0A; font-weight:bold;color: #9bf;">belongs_to</span> <span style="color:#ff3333; font-weight:bold;">:dog</span>
  has_attached_file <span style="color:#ff3333; font-weight:bold;">:photo</span>,
                    <span style="color:#ff3333; font-weight:bold;">:styles</span> <span style="color:#006600; font-weight:bold;color: #CCC;">=&gt;</span> <span style="color:#006600; font-weight:bold;color: #CCC;">&#123;</span>
                      <span style="color:#ff3333; font-weight:bold;">:thumb</span> <span style="color:#006600; font-weight:bold;color: #CCC;">=&gt;</span> <span style="color:#006600; font-weight:bold;color: #CCC;">&#91;</span><span style="color:#996600;color: #666666;">&quot;100x100&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:jpg</span><span style="color:#006600; font-weight:bold;color: #CCC;">&#93;</span>,
                      <span style="color:#ff3333; font-weight:bold;">:pagesize</span> <span style="color:#006600; font-weight:bold;color: #CCC;">=&gt;</span> <span style="color:#006600; font-weight:bold;color: #CCC;">&#91;</span><span style="color:#996600;color: #666666;">&quot;500x400&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:jpg</span><span style="color:#006600; font-weight:bold;color: #CCC;">&#93;</span>,
                    <span style="color:#006600; font-weight:bold;color: #CCC;">&#125;</span>,
                    <span style="color:#ff3333; font-weight:bold;">:default_style</span> <span style="color:#006600; font-weight:bold;color: #CCC;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:pagesize</span>
<span style="color:#9966CC; font-weight:bold;color: #B83A24;">end</span></pre></div></div>

<p>This Upload model belongs_to a dog, to match the has_many association above.  &#8220;has_attached_file&#8221; is part of Paperclip, and the &#8220;photo&#8221; attribute must match the prefix of the database fields above (e.g. photo_file_name).  Paperclip can automatically re-size photos and keep multiple versions, based on the &#8220;styles&#8221; hash.  Here we create a small thumbnail, and a version good for embedding in web pages.  In both cases, the photo is converted to a JPEG if needed.  The original photo is stored with a style of &#8220;original&#8221;.</p>
<h2>Views</h2>
<p>The main view we&#8217;re concerned about is the &#8220;new dog&#8221; view.  This will give the user a form to add a new dog, and upload photos to associate with the dog.</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="color: #FCFFBA;"># views/dogs/new.html.erb
&nbsp;
&lt;h1&gt;New dog&lt;/h1&gt;
&nbsp;
<span style="color:#006600; font-weight:bold;color: #CCC;">&lt;%</span> <span style="color:#5A0A0A; font-weight:bold;color: #9bf;">form_for</span> <span style="color:#0066ff; font-weight:bold;color: #88f;">@dog</span>, <span style="color:#ff3333; font-weight:bold;">:html</span> <span style="color:#006600; font-weight:bold;color: #CCC;">=&gt;</span> <span style="color:#006600; font-weight:bold;color: #CCC;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:multipart</span> <span style="color:#006600; font-weight:bold;color: #CCC;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;color: #577A61;">true</span> <span style="color:#006600; font-weight:bold;color: #CCC;">&#125;</span> <span style="color:#9966CC; font-weight:bold;color: #B83A24;">do</span> <span style="color:#006600; font-weight:bold;color: #CCC;">|</span>f<span style="color:#006600; font-weight:bold;color: #CCC;">|</span> <span style="color:#006600; font-weight:bold;color: #CCC;">%&gt;</span>
  <span style="color:#006600; font-weight:bold;color: #CCC;">&lt;%</span>= f.<span style="color:#9900CC;color: #f6f;">error_messages</span> <span style="color:#006600; font-weight:bold;color: #CCC;">%&gt;</span>
&nbsp;
  &lt;p&gt;
    <span style="color:#006600; font-weight:bold;color: #CCC;">&lt;%</span>= f.<span style="color:#9900CC;color: #f6f;">label</span> <span style="color:#ff3333; font-weight:bold;">:name</span> <span style="color:#006600; font-weight:bold;color: #CCC;">%&gt;</span>&lt;br /&gt;
    <span style="color:#006600; font-weight:bold;color: #CCC;">&lt;%</span>= f.<span style="color:#9900CC;color: #f6f;">text_field</span> <span style="color:#ff3333; font-weight:bold;">:name</span> <span style="color:#006600; font-weight:bold;color: #CCC;">%&gt;</span>
  &lt;/p&gt;
&nbsp;
  &lt;div id='uploads' style='border: 1px solid silver'&gt;
    <span style="color:#006600; font-weight:bold;color: #CCC;">&lt;%</span>= f.<span style="color:#9900CC;color: #f6f;">render_associated_form</span><span style="color:#006600; font-weight:bold;color: #CCC;">&#40;</span>@dog.<span style="color:#9900CC;color: #f6f;">uploads</span>, <span style="color:#ff3333; font-weight:bold;">:new</span> <span style="color:#006600; font-weight:bold;color: #CCC;">=&gt;</span> <span style="color:#006666;color: #DDD;">5</span><span style="color:#006600; font-weight:bold;color: #CCC;">&#41;</span> <span style="color:#006600; font-weight:bold;color: #CCC;">%&gt;</span>
  &lt;/div&gt;
&nbsp;
  <span style="color:#006600; font-weight:bold;color: #CCC;">&lt;%</span>= f.<span style="color:#9900CC;color: #f6f;">add_associated_link</span><span style="color:#006600; font-weight:bold;color: #CCC;">&#40;</span><span style="color:#996600;color: #666666;">'Add another photo'</span>, <span style="color:#0066ff; font-weight:bold;color: #88f;">@dog</span>.<span style="color:#9900CC;color: #f6f;">uploads</span>.<span style="color:#9900CC;color: #f6f;">build</span><span style="color:#006600; font-weight:bold;color: #CCC;">&#41;</span> <span style="color:#006600; font-weight:bold;color: #CCC;">%&gt;</span>
&nbsp;
  &lt;p&gt;
    <span style="color:#006600; font-weight:bold;color: #CCC;">&lt;%</span>= f.<span style="color:#9900CC;color: #f6f;">submit</span> <span style="color:#996600;color: #666666;">&quot;Create&quot;</span> <span style="color:#006600; font-weight:bold;color: #CCC;">%&gt;</span>
  &lt;/p&gt;
<span style="color:#006600; font-weight:bold;color: #CCC;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;color: #B83A24;">end</span> <span style="color:#006600; font-weight:bold;color: #CCC;">%&gt;</span></pre></div></div>

<p>The first part of the above view gives us a form for a new dog instance.  It also calls attribute_fu&#8217;s &#8220;render_associated_form&#8221; to create a nested form for upload instances. Here we create five blank instance forms. The instance forms are generated by a partial named after the associated model&#8230; in this case the partial is _upload.html.erb (below).  We also add a link to create additional file upload forms on the fly via attribute_fu JavaScript with the &#8220;add_associated_link&#8221; call.</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="color: #FCFFBA;"># views/dogs/_upload.html.erb
&nbsp;
&lt;p class='upload'&gt;
  &lt;label for=&quot;upload_description&quot;&gt;Description:&lt;/label&gt;
  <span style="color:#006600; font-weight:bold;color: #CCC;">&lt;%</span>= f.<span style="color:#9900CC;color: #f6f;">text_field</span> <span style="color:#ff3333; font-weight:bold;">:description</span> <span style="color:#006600; font-weight:bold;color: #CCC;">%&gt;</span>
&nbsp;
  &lt;label for=&quot;upload_photo&quot;&gt;Photo:&lt;/label&gt;
  <span style="color:#006600; font-weight:bold;color: #CCC;">&lt;%</span>= f.<span style="color:#5A0A0A; font-weight:bold;color: #9bf;">file_field</span> <span style="color:#ff3333; font-weight:bold;">:photo</span> <span style="color:#006600; font-weight:bold;color: #CCC;">%&gt;</span>
&nbsp;
  <span style="color:#006600; font-weight:bold;color: #CCC;">&lt;%</span>= f.<span style="color:#9900CC;color: #f6f;">remove_link</span> <span style="color:#996600;color: #666666;">&quot;remove&quot;</span> <span style="color:#006600; font-weight:bold;color: #CCC;">%&gt;</span>
&lt;/p&gt;</pre></div></div>

<p>The partial is pretty standard, just a field for our photo description, and a file upload field.  The only unusual part is attribute_fu&#8217;s &#8220;remove_link&#8221; JavaScript call, which lets the user remove one of the photo upload forms.</p>
<p>Here&#8217;s what the completed form looks like:</p>
<p><img src="http://www.mfischer.com/wordpress/wp-content/uploads/2009/02/form-example.png" alt="New dog form" title="form-example" width="543" height="257" class="size-full wp-image-378" /></p>
<h2>Controllers</h2>
<p>The nice thing about attribute_fu is that no special controller logic is needed!  The dog controller is just a standard RESTful controller!  When the form is submitted, the dog instance is created, and all the associated upload instances are created simultaneously:</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="color: #FCFFBA;"><span style="color:#008000; font-style:italic;color: #CDC;"># controllers/dogs_controller.rb</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;color: #B83A24;">class</span> DogsController <span style="color:#006600; font-weight:bold;color: #CCC;">&lt;</span> ApplicationController
&nbsp;
  <span style="color:#9966CC; font-weight:bold;color: #B83A24;">def</span> index
    <span style="color:#0066ff; font-weight:bold;color: #88f;">@dogs</span> = Dog.<span style="color:#9900CC;color: #f6f;">find</span> <span style="color:#ff3333; font-weight:bold;">:all</span>
  <span style="color:#9966CC; font-weight:bold;color: #B83A24;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;color: #B83A24;">def</span> <span style="color:#5A0A0A; font-weight:bold;color: #9bf;">show</span>
    <span style="color:#0066ff; font-weight:bold;color: #88f;">@dog</span> = Dog.<span style="color:#9900CC;color: #f6f;">find</span> params<span style="color:#006600; font-weight:bold;color: #CCC;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:id</span><span style="color:#006600; font-weight:bold;color: #CCC;">&#93;</span>
  <span style="color:#9966CC; font-weight:bold;color: #B83A24;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;color: #B83A24;">def</span> <span style="color:#5A0A0A; font-weight:bold;color: #9bf;">new</span>
    <span style="color:#0066ff; font-weight:bold;color: #88f;">@dog</span> = Dog.<span style="color:#5A0A0A; font-weight:bold;color: #9bf;">new</span>
  <span style="color:#9966CC; font-weight:bold;color: #B83A24;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;color: #B83A24;">def</span> create
    <span style="color:#0066ff; font-weight:bold;color: #88f;">@dog</span> = Dog.<span style="color:#5A0A0A; font-weight:bold;color: #9bf;">new</span> params<span style="color:#006600; font-weight:bold;color: #CCC;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:dog</span><span style="color:#006600; font-weight:bold;color: #CCC;">&#93;</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;color: #B83A24;">if</span> <span style="color:#0066ff; font-weight:bold;color: #88f;">@dog</span>.<span style="color:#5A0A0A; font-weight:bold;color: #9bf;">save</span>
      <span style="color:#5A0A0A; font-weight:bold;color: #9bf;">flash</span><span style="color:#006600; font-weight:bold;color: #CCC;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:notice</span><span style="color:#006600; font-weight:bold;color: #CCC;">&#93;</span> = <span style="color:#996600;color: #666666;">'Dog was successfully created.'</span>
      <span style="color:#5A0A0A; font-weight:bold;color: #9bf;">redirect_to</span> <span style="color:#0066ff; font-weight:bold;color: #88f;">@dog</span>
    <span style="color:#9966CC; font-weight:bold;color: #B83A24;">else</span>
      <span style="color:#5A0A0A; font-weight:bold;color: #9bf;">render</span> <span style="color:#ff3333; font-weight:bold;">:action</span> <span style="color:#006600; font-weight:bold;color: #CCC;">=&gt;</span> <span style="color:#996600;color: #666666;">&quot;new&quot;</span>
    <span style="color:#9966CC; font-weight:bold;color: #B83A24;">end</span>
  <span style="color:#9966CC; font-weight:bold;color: #B83A24;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;color: #B83A24;">end</span></pre></div></div>

<h2>Image cropping</h2>
<p>The last feature to add is the image cropping functionality.  We&#8217;re going to do this in the &#8220;edit upload&#8221; action.  The edit form will show the image, and let the user drag a rectangle around the region to crop.  The form will return the rectangle coordinates, and we&#8217;ll override the standard update_attributes method of the Upload model, to perform the crop.</p>
<p>In the main view template, I&#8217;m including the jsCropperUI JavaScript code and its dependencies.  The scripts go in the public/javascripts directory when you install the jsCropperUI code.</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="color: #FCFFBA;"># views/layouts/application.html.erb
&nbsp;
<span style="color:#006600; font-weight:bold;color: #CCC;">&lt;%</span>= <span style="color:#5A0A0A; font-weight:bold;color: #9bf;">javascript_include_tag</span> <span style="color:#996600;color: #666666;">'cropper/lib/prototype.js'</span> <span style="color:#006600; font-weight:bold;color: #CCC;">%&gt;</span>
<span style="color:#006600; font-weight:bold;color: #CCC;">&lt;%</span>= <span style="color:#5A0A0A; font-weight:bold;color: #9bf;">javascript_include_tag</span> <span style="color:#996600;color: #666666;">'cropper/lib/scriptaculous.js?load=builder,dragdrop'</span> <span style="color:#006600; font-weight:bold;color: #CCC;">%&gt;</span>
<span style="color:#006600; font-weight:bold;color: #CCC;">&lt;%</span>= <span style="color:#5A0A0A; font-weight:bold;color: #9bf;">javascript_include_tag</span> <span style="color:#996600;color: #666666;">'cropper/cropper.js'</span> <span style="color:#006600; font-weight:bold;color: #CCC;">%&gt;</span></pre></div></div>

<p>The &#8220;edit upload&#8221; view is a little complex.  Here&#8217;s how it&#8217;s put together:</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="color: #FCFFBA;"># view/uploads/edit.html.erb
&nbsp;
&lt;h1&gt;Editing upload&lt;/h1&gt;
&nbsp;
<span style="color:#006600; font-weight:bold;color: #CCC;">&lt;%</span> <span style="color:#5A0A0A; font-weight:bold;color: #9bf;">form_for</span><span style="color:#006600; font-weight:bold;color: #CCC;">&#40;</span>@upload<span style="color:#006600; font-weight:bold;color: #CCC;">&#41;</span> <span style="color:#9966CC; font-weight:bold;color: #B83A24;">do</span> <span style="color:#006600; font-weight:bold;color: #CCC;">|</span>f<span style="color:#006600; font-weight:bold;color: #CCC;">|</span> <span style="color:#006600; font-weight:bold;color: #CCC;">%&gt;</span>
  <span style="color:#006600; font-weight:bold;color: #CCC;">&lt;%</span>= f.<span style="color:#9900CC;color: #f6f;">error_messages</span> <span style="color:#006600; font-weight:bold;color: #CCC;">%&gt;</span>
&nbsp;
  &lt;p&gt;
    <span style="color:#006600; font-weight:bold;color: #CCC;">&lt;%</span>= f.<span style="color:#9900CC;color: #f6f;">label</span> <span style="color:#ff3333; font-weight:bold;">:description</span> <span style="color:#006600; font-weight:bold;color: #CCC;">%&gt;</span>&lt;br /&gt;
    <span style="color:#006600; font-weight:bold;color: #CCC;">&lt;%</span>= f.<span style="color:#9900CC;color: #f6f;">text_field</span> <span style="color:#ff3333; font-weight:bold;">:description</span> <span style="color:#006600; font-weight:bold;color: #CCC;">%&gt;</span>
  &lt;/p&gt;
&nbsp;
  &lt;!-- CROP FORM --&gt;
  &lt;div id='cropwrap'&gt;
    <span style="color:#006600; font-weight:bold;color: #CCC;">&lt;%</span>= <span style="color:#5A0A0A; font-weight:bold;color: #9bf;">image_tag</span> <span style="color:#0066ff; font-weight:bold;color: #88f;">@upload</span>.<span style="color:#9900CC;color: #f6f;">photo</span>.<span style="color:#9900CC;color: #f6f;">url</span>, <span style="color:#ff3333; font-weight:bold;">:id</span> <span style="color:#006600; font-weight:bold;color: #CCC;">=&gt;</span> <span style="color:#996600;color: #666666;">'cropimage'</span> <span style="color:#006600; font-weight:bold;color: #CCC;">%&gt;</span>
  &lt;/div&gt;
&nbsp;
  &lt;div id='cropresults'&gt;
    <span style="color:#006600; font-weight:bold;color: #CCC;">&lt;%</span>= f.<span style="color:#9900CC;color: #f6f;">label</span> <span style="color:#996600;color: #666666;">'x1'</span> <span style="color:#006600; font-weight:bold;color: #CCC;">%&gt;</span>
    <span style="color:#006600; font-weight:bold;color: #CCC;">&lt;%</span>= f.<span style="color:#9900CC;color: #f6f;">text_field</span> <span style="color:#996600;color: #666666;">'x1'</span>, <span style="color:#ff3333; font-weight:bold;">:size</span> <span style="color:#006600; font-weight:bold;color: #CCC;">=&gt;</span> <span style="color:#006666;color: #DDD;">6</span> <span style="color:#006600; font-weight:bold;color: #CCC;">%&gt;</span>
    &lt;br /&gt;
    <span style="color:#006600; font-weight:bold;color: #CCC;">&lt;%</span>= f.<span style="color:#9900CC;color: #f6f;">label</span> <span style="color:#996600;color: #666666;">'y1'</span> <span style="color:#006600; font-weight:bold;color: #CCC;">%&gt;</span>
    <span style="color:#006600; font-weight:bold;color: #CCC;">&lt;%</span>= f.<span style="color:#9900CC;color: #f6f;">text_field</span> <span style="color:#996600;color: #666666;">'y1'</span>, <span style="color:#ff3333; font-weight:bold;">:size</span> <span style="color:#006600; font-weight:bold;color: #CCC;">=&gt;</span> <span style="color:#006666;color: #DDD;">6</span> <span style="color:#006600; font-weight:bold;color: #CCC;">%&gt;</span>
    &lt;br /&gt;
    <span style="color:#006600; font-weight:bold;color: #CCC;">&lt;%</span>= f.<span style="color:#9900CC;color: #f6f;">label</span> <span style="color:#996600;color: #666666;">'width'</span> <span style="color:#006600; font-weight:bold;color: #CCC;">%&gt;</span>
    <span style="color:#006600; font-weight:bold;color: #CCC;">&lt;%</span>= f.<span style="color:#9900CC;color: #f6f;">text_field</span> <span style="color:#996600;color: #666666;">'width'</span>, <span style="color:#ff3333; font-weight:bold;">:size</span> <span style="color:#006600; font-weight:bold;color: #CCC;">=&gt;</span> <span style="color:#006666;color: #DDD;">6</span> <span style="color:#006600; font-weight:bold;color: #CCC;">%&gt;</span>
    &lt;br /&gt;
    <span style="color:#006600; font-weight:bold;color: #CCC;">&lt;%</span>= f.<span style="color:#9900CC;color: #f6f;">label</span> <span style="color:#996600;color: #666666;">'height'</span> <span style="color:#006600; font-weight:bold;color: #CCC;">%&gt;</span>
    <span style="color:#006600; font-weight:bold;color: #CCC;">&lt;%</span>= f.<span style="color:#9900CC;color: #f6f;">text_field</span> <span style="color:#996600;color: #666666;">'height'</span>, <span style="color:#ff3333; font-weight:bold;">:size</span> <span style="color:#006600; font-weight:bold;color: #CCC;">=&gt;</span> <span style="color:#006666;color: #DDD;">6</span> <span style="color:#006600; font-weight:bold;color: #CCC;">%&gt;</span>
    &lt;br /&gt;
  &lt;/div&gt; &lt;!-- cropresults --&gt;
  &lt;!-- END CROP FORM --&gt;
&nbsp;
  &lt;p&gt;
    <span style="color:#006600; font-weight:bold;color: #CCC;">&lt;%</span>= f.<span style="color:#9900CC;color: #f6f;">submit</span> <span style="color:#996600;color: #666666;">&quot;Update&quot;</span> <span style="color:#006600; font-weight:bold;color: #CCC;">%&gt;</span>
  &lt;/p&gt;
<span style="color:#006600; font-weight:bold;color: #CCC;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;color: #B83A24;">end</span> <span style="color:#006600; font-weight:bold;color: #CCC;">%&gt;</span></pre></div></div>

<p>In addition to the edit field for our model&#8217;s normal attributes (&#8221;description&#8221;, in this case), we also have fields for the geometry of our crop rectangle.  These fields will be filled in automatically by the jsCropperUI.  We also need to display the image we&#8217;re cropping, and identify it with a CSS ID so the jsCropperUI can attach to it.  To attach the jsCropperUI, we need to add some JavaScript to the page:</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="color: #FCFFBA;"><span style="color:#008000; font-style:italic;color: #CDC;"># view/uploads/edit.html.erb</span>
&nbsp;
<span style="color:#006600; font-weight:bold;color: #CCC;">&lt;</span>script type=<span style="color:#996600;color: #666666;">&quot;text/javascript&quot;</span> language=<span style="color:#996600;color: #666666;">&quot;JavaScript&quot;</span><span style="color:#006600; font-weight:bold;color: #CCC;">&gt;</span>
function onEndCrop<span style="color:#006600; font-weight:bold;color: #CCC;">&#40;</span> coords, dimensions <span style="color:#006600; font-weight:bold;color: #CCC;">&#41;</span> <span style="color:#006600; font-weight:bold;color: #CCC;">&#123;</span>
  $<span style="color:#006600; font-weight:bold;color: #CCC;">&#40;</span> <span style="color:#996600;color: #666666;">'upload_x1'</span> <span style="color:#006600; font-weight:bold;color: #CCC;">&#41;</span>.<span style="color:#9900CC;color: #f6f;">value</span> = coords.<span style="color:#9900CC;color: #f6f;">x1</span>;
  $<span style="color:#006600; font-weight:bold;color: #CCC;">&#40;</span> <span style="color:#996600;color: #666666;">'upload_y1'</span> <span style="color:#006600; font-weight:bold;color: #CCC;">&#41;</span>.<span style="color:#9900CC;color: #f6f;">value</span> = coords.<span style="color:#9900CC;color: #f6f;">y1</span>;
  $<span style="color:#006600; font-weight:bold;color: #CCC;">&#40;</span> <span style="color:#996600;color: #666666;">'upload_width'</span> <span style="color:#006600; font-weight:bold;color: #CCC;">&#41;</span>.<span style="color:#9900CC;color: #f6f;">value</span> = dimensions.<span style="color:#9900CC;color: #f6f;">width</span>;
  $<span style="color:#006600; font-weight:bold;color: #CCC;">&#40;</span> <span style="color:#996600;color: #666666;">'upload_height'</span> <span style="color:#006600; font-weight:bold;color: #CCC;">&#41;</span>.<span style="color:#9900CC;color: #f6f;">value</span> = dimensions.<span style="color:#9900CC;color: #f6f;">height</span>;
<span style="color:#006600; font-weight:bold;color: #CCC;">&#125;</span>
&nbsp;
Event.<span style="color:#9900CC;color: #f6f;">observe</span><span style="color:#006600; font-weight:bold;color: #CCC;">&#40;</span> window, <span style="color:#996600;color: #666666;">'load'</span>, function<span style="color:#006600; font-weight:bold;color: #CCC;">&#40;</span><span style="color:#006600; font-weight:bold;color: #CCC;">&#41;</span> <span style="color:#006600; font-weight:bold;color: #CCC;">&#123;</span>
  <span style="color:#5A0A0A; font-weight:bold;color: #9bf;">new</span> Cropper.<span style="color:#9900CC;color: #f6f;">Img</span><span style="color:#006600; font-weight:bold;color: #CCC;">&#40;</span><span style="color:#996600;color: #666666;">'cropimage'</span>, <span style="color:#006600; font-weight:bold;color: #CCC;">&#123;</span>
    minWidth: <span style="color:#006666;color: #DDD;">50</span>,
    minHeight: <span style="color:#006666;color: #DDD;">50</span>,
    displayOnInit: <span style="color:#0000FF; font-weight:bold;color: #577A61;">true</span>,
    onEndCrop: onEndCrop
  <span style="color:#006600; font-weight:bold;color: #CCC;">&#125;</span> <span style="color:#006600; font-weight:bold;color: #CCC;">&#41;</span>;
<span style="color:#006600; font-weight:bold;color: #CCC;">&#125;</span> <span style="color:#006600; font-weight:bold;color: #CCC;">&#41;</span>;
<span style="color:#006600; font-weight:bold;color: #CCC;">&lt;/</span>script<span style="color:#006600; font-weight:bold;color: #CCC;">&gt;</span></pre></div></div>

<p>This attaches the jsCropperUI to the photo we want to edit, identified by the &#8220;cropimage&#8221; CSS ID.  It also sets up a callback function to set our form fields with values based on the crop coordinates.  Check the jsCropperUI documentation for more details.</p>
<p>Here&#8217;s what our page looks like:</p>
<p><img src="http://www.mfischer.com/wordpress/wp-content/uploads/2009/02/crop-example.png" alt="crop-example" title="crop-example" width="519" height="584" class="aligncenter size-full wp-image-385" /></p>
<p>There are two other things to be aware of here:</p>
<p>First, the &#8220;image_tag&#8221; is displaying the &#8220;:default&#8221; style of the uploaded image.  If you look back to the Upload model code above, that means it&#8217;s an image that&#8217;s been scaled down to at most 500&#215;400 pixels.  This is important, since we don&#8217;t want to display a full-sized image if someone&#8217;s uploaded a giant photo from a 10 megapixel digital camera!</p>
<p>Second, we&#8217;re telling the form helpers to create form fields for attributes like &#8220;x1&#8243; and &#8220;width&#8221;, which aren&#8217;t attributes of our Upload model.  To make this work, we need to add some virtual attributes to the model (virtual, because they&#8217;re not associated with any fields in our database table):</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="color: #FCFFBA;"><span style="color:#008000; font-style:italic;color: #CDC;"># models/upload/upload.rb</span>
&nbsp;
attr_accessor <span style="color:#ff3333; font-weight:bold;">:x1</span>, <span style="color:#ff3333; font-weight:bold;">:y1</span>, <span style="color:#ff3333; font-weight:bold;">:width</span>, <span style="color:#ff3333; font-weight:bold;">:height</span></pre></div></div>

<p>Now, when the form is submitted, the standard &#8220;update&#8221; action in the uploads controller will call &#8220;update_attributes&#8221;, just like any other form and controller:</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="color: #FCFFBA;"><span style="color:#008000; font-style:italic;color: #CDC;"># controllers/uploads_controller.rb</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;color: #B83A24;">def</span> <span style="color:#5A0A0A; font-weight:bold;color: #9bf;">update</span>
  <span style="color:#0066ff; font-weight:bold;color: #88f;">@upload</span> = Upload.<span style="color:#9900CC;color: #f6f;">find</span> params<span style="color:#006600; font-weight:bold;color: #CCC;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:id</span><span style="color:#006600; font-weight:bold;color: #CCC;">&#93;</span>
  <span style="color:#9966CC; font-weight:bold;color: #B83A24;">if</span> <span style="color:#0066ff; font-weight:bold;color: #88f;">@upload</span>.<span style="color:#9900CC;color: #f6f;">update_attributes</span> params<span style="color:#006600; font-weight:bold;color: #CCC;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:upload</span><span style="color:#006600; font-weight:bold;color: #CCC;">&#93;</span>
    <span style="color:#5A0A0A; font-weight:bold;color: #9bf;">flash</span><span style="color:#006600; font-weight:bold;color: #CCC;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:notice</span><span style="color:#006600; font-weight:bold;color: #CCC;">&#93;</span> = <span style="color:#996600;color: #666666;">'Upload was successfully updated.'</span>
    <span style="color:#5A0A0A; font-weight:bold;color: #9bf;">redirect_to</span> <span style="color:#0066ff; font-weight:bold;color: #88f;">@upload</span>
  <span style="color:#9966CC; font-weight:bold;color: #B83A24;">else</span>
    <span style="color:#5A0A0A; font-weight:bold;color: #9bf;">render</span> <span style="color:#ff3333; font-weight:bold;">:action</span> <span style="color:#006600; font-weight:bold;color: #CCC;">=&gt;</span> <span style="color:#996600;color: #666666;">&quot;edit&quot;</span>
  <span style="color:#9966CC; font-weight:bold;color: #B83A24;">end</span>
<span style="color:#9966CC; font-weight:bold;color: #B83A24;">end</span></pre></div></div>

<p>To make this actually crop the image, we need to override the update_attributes method in the upload model:</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="color: #FCFFBA;"><span style="color:#008000; font-style:italic;color: #CDC;"># models/upload.rb</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;color: #8FB394;">require</span> <span style="color:#996600;color: #666666;">'RMagick'</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;color: #B83A24;">def</span> update_attributes<span style="color:#006600; font-weight:bold;color: #CCC;">&#40;</span>att<span style="color:#006600; font-weight:bold;color: #CCC;">&#41;</span>
&nbsp;
  <span style="color:#008000; font-style:italic;color: #CDC;"># Should we crop?</span>
  scaled_img = <span style="color:#6666ff; font-weight:bold;">Magick::ImageList</span>.<span style="color:#5A0A0A; font-weight:bold;color: #9bf;">new</span><span style="color:#006600; font-weight:bold;color: #CCC;">&#40;</span><span style="color:#0000FF; font-weight:bold;color: #577A61;">self</span>.<span style="color:#9900CC;color: #f6f;">photo</span>.<span style="color:#9900CC;color: #f6f;">path</span><span style="color:#006600; font-weight:bold;color: #CCC;">&#41;</span>
  orig_img = <span style="color:#6666ff; font-weight:bold;">Magick::ImageList</span>.<span style="color:#5A0A0A; font-weight:bold;color: #9bf;">new</span><span style="color:#006600; font-weight:bold;color: #CCC;">&#40;</span><span style="color:#0000FF; font-weight:bold;color: #577A61;">self</span>.<span style="color:#9900CC;color: #f6f;">photo</span>.<span style="color:#9900CC;color: #f6f;">path</span><span style="color:#006600; font-weight:bold;color: #CCC;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:original</span><span style="color:#006600; font-weight:bold;color: #CCC;">&#41;</span><span style="color:#006600; font-weight:bold;color: #CCC;">&#41;</span>
  scale = orig_img.<span style="color:#9900CC;color: #f6f;">columns</span>.<span style="color:#9900CC;color: #f6f;">to_f</span> <span style="color:#006600; font-weight:bold;color: #CCC;">/</span> scaled_img.<span style="color:#9900CC;color: #f6f;">columns</span>
&nbsp;
  args = <span style="color:#006600; font-weight:bold;color: #CCC;">&#91;</span> att<span style="color:#006600; font-weight:bold;color: #CCC;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:x1</span><span style="color:#006600; font-weight:bold;color: #CCC;">&#93;</span>, att<span style="color:#006600; font-weight:bold;color: #CCC;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:y1</span><span style="color:#006600; font-weight:bold;color: #CCC;">&#93;</span>, att<span style="color:#006600; font-weight:bold;color: #CCC;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:width</span><span style="color:#006600; font-weight:bold;color: #CCC;">&#93;</span>, att<span style="color:#006600; font-weight:bold;color: #CCC;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:height</span><span style="color:#006600; font-weight:bold;color: #CCC;">&#93;</span> <span style="color:#006600; font-weight:bold;color: #CCC;">&#93;</span>
  args = args.<span style="color:#9900CC;color: #f6f;">collect</span> <span style="color:#006600; font-weight:bold;color: #CCC;">&#123;</span> <span style="color:#006600; font-weight:bold;color: #CCC;">|</span>a<span style="color:#006600; font-weight:bold;color: #CCC;">|</span> a.<span style="color:#5A0A0A; font-weight:bold;color: #9bf;">to_i</span> <span style="color:#006600; font-weight:bold;color: #CCC;">*</span> scale <span style="color:#006600; font-weight:bold;color: #CCC;">&#125;</span>
&nbsp;
  orig_img.<span style="color:#9900CC;color: #f6f;">crop</span>!<span style="color:#006600; font-weight:bold;color: #CCC;">&#40;</span><span style="color:#006600; font-weight:bold;color: #CCC;">*</span>args<span style="color:#006600; font-weight:bold;color: #CCC;">&#41;</span>
  orig_img.<span style="color:#9900CC;color: #f6f;">write</span><span style="color:#006600; font-weight:bold;color: #CCC;">&#40;</span><span style="color:#0000FF; font-weight:bold;color: #577A61;">self</span>.<span style="color:#9900CC;color: #f6f;">photo</span>.<span style="color:#9900CC;color: #f6f;">path</span><span style="color:#006600; font-weight:bold;color: #CCC;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:original</span><span style="color:#006600; font-weight:bold;color: #CCC;">&#41;</span><span style="color:#006600; font-weight:bold;color: #CCC;">&#41;</span>
&nbsp;
  <span style="color:#0000FF; font-weight:bold;color: #577A61;">self</span>.<span style="color:#9900CC;color: #f6f;">photo</span>.<span style="color:#9900CC;color: #f6f;">reprocess</span>!
  <span style="color:#0000FF; font-weight:bold;color: #577A61;">self</span>.<span style="color:#5A0A0A; font-weight:bold;color: #9bf;">save</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;color: #B83A24;">super</span><span style="color:#006600; font-weight:bold;color: #CCC;">&#40;</span>att<span style="color:#006600; font-weight:bold;color: #CCC;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;color: #B83A24;">end</span></pre></div></div>

<p>What&#8217;s going on here?  We want to crop our original, high-resolution image, but we used a scaled down version in the jsCropperUI to select our crop rectangle. To adjust for that, we need to check the pixel width (&#8221;columns&#8221;) for the original and scaled images, and compute a scaling factor.  Next we prepare the four arguments for the RMagick crop function, which expects x1, y2, width, and height.  We create an array with the arguments, convert to integer (they&#8217;re still strings, since they came from a text field in the form), and scale them.  We can pass multiple arguments to a method by prefacing an array with an asterisk.  That&#8217;s something new I came across in this project, and I suspect may come in handy again some time.  The image is cropped, and then written back to disk, replacing the original.</p>
<p>&#8220;reprocess!&#8221; is a Paperclip method to re-generate thumbnails and other scaled versions of an image from the original.  And finally, we call the true &#8220;update_attributes&#8221; method to handle things like the &#8220;description&#8221; or other actual attributes of our model.</p>
<p>So there you have it&#8230; a framework to deal with multiple image uploads to an associated model, with user-selected cropping.</p>
<p>I&#8217;ve packaged up the example code <a href="http://github.com/greendog99/multiple_image_upload_crop_example/tree/master">into a GitHub repository</a>. It&#8217;s a fully functional Rails 2.2.2 app, assuming you have the Paperclip gem and RMagick already installed.</p>
<p>Comments and improvements are welcomed.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mfischer.com/wordpress/2009/02/02/multiple-image-upload-and-crop-with-rails/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>PET Scan #7</title>
		<link>http://www.mfischer.com/wordpress/2009/01/09/pet-scan-7/</link>
		<comments>http://www.mfischer.com/wordpress/2009/01/09/pet-scan-7/#comments</comments>
		<pubDate>Fri, 09 Jan 2009 14:46:22 +0000</pubDate>
		<dc:creator>mike</dc:creator>
				<category><![CDATA[cancer]]></category>
		<category><![CDATA[crohn's]]></category>
		<category><![CDATA[food/wine]]></category>

		<guid isPermaLink="false">http://www.mfischer.com/?p=354</guid>
		<description><![CDATA[Went for PET scan #7 last week, and my doctor this week said it was completely clean, as expected.  I&#8217;m now switching to twice a year for future scans.  It was a little over two years ago that I had my Crohn&#8217;s surgery and started chemo, but it feels like forever &#8212; which [...]]]></description>
			<content:encoded><![CDATA[<p>Went for PET scan #7 last week, and my doctor this week said it was completely clean, as expected.  I&#8217;m now switching to twice a year for future scans.  It was a little over two years ago that I had my Crohn&#8217;s surgery and started chemo, but it feels like forever &#8212; which is fine by me!</p>
<p>Fortunately, my Crohn&#8217;s has also been completely gone since the surgery, so for the last 18 months I&#8217;ve been healthier than any time in the last 20 years.  The only undesirable side-effect of having working intestines is that actually digesting my food has caused me to gain considerable weight &#8212; 30 pounds compared to where I&#8217;ve been the last 10 years or so.  This year I&#8217;m making some lifestyle changes&#8230; I&#8217;m not calling it a diet since diets tend to be temporary things.  We&#8217;ll see how it goes, but the goal is to exercise more and eat healthier in general.  It&#8217;s only been 1-2 weeks so far, but by writing it down here, I&#8217;m creating more incentive to continue.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mfischer.com/wordpress/2009/01/09/pet-scan-7/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Ruby memory leakage</title>
		<link>http://www.mfischer.com/wordpress/2009/01/04/ruby-memory-leakage/</link>
		<comments>http://www.mfischer.com/wordpress/2009/01/04/ruby-memory-leakage/#comments</comments>
		<pubDate>Sun, 04 Jan 2009 21:49:27 +0000</pubDate>
		<dc:creator>mike</dc:creator>
				<category><![CDATA[computing]]></category>

		<guid isPermaLink="false">http://www.mfischer.com/?p=349</guid>
		<description><![CDATA[I&#8217;ve been working on a Ruby on Rails web application to track and compare Xbox player scores and achievements&#8212;a direct ripoff of a similar Perl/Mason app that Ty wrote.  I recently discovered that the web site started getting very slow to respond, and Ruby was using up lots of memory.
I did a fair bit [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been working on a Ruby on Rails web application to track and compare Xbox player scores and achievements&mdash;a direct ripoff of a similar Perl/Mason app that Ty wrote.  I recently discovered that the web site started getting very slow to respond, and Ruby was using up lots of memory.</p>
<p>I did a fair bit of Google research into memory usage and garbage collection in Rails, but couldn&#8217;t find any smoking gun.  I did come across a really tiny <a href="http://github.com/binarylogic/memory_usage_logger/tree/master">Memory Usage Logger</a> for Rails, that just adds the process memory size to the server log after each request.  I added this handy code, and used the <a href="http://httpd.apache.org/docs/2.0/programs/ab.html">Apache Benchmarking Tool</a> to send several hundred web requests to my app, and graphed the results.  Memory was definitely increasing linearly with the number of requests.</p>
<p>After doing some basic troubleshooting and code analysis, I was getting nowhere.  Instead, I created a new, simple Rails app.  Just an empty app, with one model and controller generated with Rails&#8217;s scaffolding and default views.  This exhibited the same ever-growing memory problem.  Weird.  Rails is used widely for major web sites, so I knew this couldn&#8217;t be a widespread problem.  I next installed <a href="http://www.rubyenterpriseedition.com/">Ruby Enterprise Edition</a>, which was fortunately a fast and painless process.  When I restarted my application and re-ran my stress test, memory was completely flat!</p>
<p><img src="http://chart.apis.google.com/chart?chtt=Ruby+App+Memory+Usage&amp;chts=000000,12&amp;chs=500x250&amp;chf=bg,s,ffffff|c,s,ffffff&amp;chxt=x,y&amp;chxl=0:||1:|33,224.00|39,180.00|45,136.00&amp;cht=lc&amp;chd=t:0.00,.60,1.14,1.67,2.21,5.74,10.94,16.18,21.42,23.23,23.30,23.33,23.33,23.33,23.33,23.33,23.33,23.33,23.70,28.17,33.34,38.58,41.80,41.80,41.80,41.84,41.90,41.90,41.90,41.90,41.94,41.94,41.97,41.97,43.88,48.52,48.52,48.55,48.55,48.55,48.55,48.55,48.55,48.55,48.55,48.55,48.55,49.05,54.23,54.70,54.70,54.70,54.70,54.70,54.73,54.73,54.76,54.76,54.76,54.76,54.76,58.93,60.61,60.61,60.61,60.61,60.61,60.64,60.64,60.64,60.64,60.64,60.64,60.64,64.60,67.76,67.76,67.79,67.79,67.79,67.79,67.79,67.79,67.83,67.83,67.83,67.83,69.74,74.31,74.31,74.31,74.31,74.31,74.34,74.34,74.34,74.34,74.34,74.34,74.37,74.88,80.08,80.45,80.45,80.45,80.45,80.45,80.45,80.45,80.45,80.45,80.45,80.49,80.52,84.85,86.60,86.60,86.60,86.60,86.60,86.60,86.60,86.60,86.60,86.60,86.60,86.60,90.42,93.51,93.51,93.51,93.51,93.51,93.51,93.51,93.51,93.51,93.51,93.51,93.51,95.50,100.00,100.00,100.00,100.00|8.59,8.59,8.59,8.59,8.59,8.59,8.59,9.16,12.25,15.78,19.44,23.06,26.79,30.45,33.98,37.74,39.42,39.42,39.42,39.42,39.45,39.48,39.52,39.55,39.59,39.62,39.65,39.69,39.69,39.69,39.69,39.69,39.69,39.69,39.69,39.69,39.72,39.75,39.79,39.82,39.85,39.89,39.92,39.92,39.92,39.92,39.92,39.92,39.92,39.92,39.92,39.92,39.92,39.92,39.92,39.92,39.92,39.92,39.92,39.92,39.92,39.92,39.92,39.92,39.92,39.92,39.92,39.92,39.92,39.92,39.92,39.92,39.92,39.92,39.92,39.92,39.92,39.95,39.99,39.99,39.99,39.99,39.99,39.99,39.99,39.99,39.99,39.99,39.99,39.99,39.99,39.99,39.99,39.99,39.99,39.99,39.99,39.99,39.99,39.99,39.99,39.99,39.99,39.99,39.99,39.99,39.99,39.99,39.99,39.99,39.99,39.99,39.99,39.99,39.99,39.99,39.99,39.99,39.99,39.99,39.99,39.99,39.99,39.99,39.99,39.99,39.99,39.99,39.99,39.99,39.99,39.99,39.99,39.99,39.99,39.99,39.99,39.99,39.99,39.99,39.99,39.99,39.99,39.99,39.99&amp;chdl=Ruby|REE&amp;chco=ff0000,0000ff&amp;chls=1,1,0|1,1,0" alt="Google Chart"/></p>
<p>I notice now that although both versions of ruby (standard and Enterprise Edition) are version 1.8.6, they are different patchlevels, so I&#8217;m not sure if the memory fix is due to REE or due to some more recent ruby patches.  Either way, I&#8217;ll probably stick with REE in the future.</p>
<p><img src="http://www.mfischer.com/wordpress/wp-content/uploads/2009/01/picture-2.png" alt="ruby-versions" title="ruby-versions" width="447" height="73" class="alignnone size-full wp-image-350" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.mfischer.com/wordpress/2009/01/04/ruby-memory-leakage/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss><!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->
