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

<channel>
	<title>Motion Standing Still &#187; Miscellaneous</title>
	<atom:link href="http://www.motionstandingstill.com/category/miscellaneous/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.motionstandingstill.com</link>
	<description>high performance ruby on rails</description>
	<lastBuildDate>Sun, 11 Jul 2010 03:46:07 +0000</lastBuildDate>
	
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Help!  I need an advisor</title>
		<link>http://www.motionstandingstill.com/help-i-need-an-advisor/2010-03-04/</link>
		<comments>http://www.motionstandingstill.com/help-i-need-an-advisor/2010-03-04/#comments</comments>
		<pubDate>Wed, 03 Mar 2010 21:16:28 +0000</pubDate>
		<dc:creator>nahum</dc:creator>
				<category><![CDATA[Features]]></category>
		<category><![CDATA[Miscellaneous]]></category>

		<guid isPermaLink="false">http://www.motionstandingstill.com/?p=260</guid>
		<description><![CDATA[Last year I started a personal project to up-skill in some aspects of web development I was lacking in, like Javascript.  I had an idea for a game I'd been bouncing around for several years so I created that.  One thing led to another and now it's become quite a serious project...


No related posts.]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify; ">
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden; text-align: justify;">Last year I started a personal project to up-skill in some aspects of web development I was lacking in, like Javascript.  I had an idea for a game I&#8217;d been bouncing around for several years so I created that.  One thing led to another and now it&#8217;s become quite a serious project.</div>
<p style="text-align: justify;">Last year I started a personal project to up-skill in some aspects of web development I was lacking in, like Javascript.  I had an idea for a game I&#8217;d been bouncing around for several years so I created that.  One thing led to another and now it&#8217;s become quite a serious project.</p>
<p>The main page for <a href="http://www.esdao.com" target="_blank">ESDAO</a> gives an overview of what it is about, you can use this <a href="http://www.esdao.com/users/new?invite=29f78c3067d2012c4124404034a16a51" target="_blank">invite link</a> to take the game for a spin.</p>
<p>There is a small base of committed testers enjoying the game &#8211; but it&#8217;s not really growing much and I&#8217;m kinda stumped about the best way to fix that.  I have some ideas but I have little experience in this type of thing and would love to get advice from someone with a successful background doing just that.</p>
<p style="text-align: justify;">The game itself is close to completion and my next major tasks are getting more people playing it and sorting out the look and feel.  I have a designer lined up, but have yet to commission a site design while I work out what to do next.</p>
<p style="text-align: justify;">So, I&#8217;m after an advisor who I can meet on a regular basis.  Someone who I can pay for good objective advice.  I&#8217;m based in Wellington and would obviously prefer someone local.</p>
<p style="text-align: justify;">If you&#8217;d like to know more about me I have a <a href="http://www.motionstandingstill.com/available-for-hire/" target="_blank">page for that</a>.</p>
<p style="text-align: justify;">I&#8217;m best contacted via email &#8211; nahum.wild@gmail.com</p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.motionstandingstill.com/help-i-need-an-advisor/2010-03-04/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Silence = Stupid Busy</title>
		<link>http://www.motionstandingstill.com/silence-stupid-busy/2009-04-26/</link>
		<comments>http://www.motionstandingstill.com/silence-stupid-busy/2009-04-26/#comments</comments>
		<pubDate>Sun, 26 Apr 2009 01:08:43 +0000</pubDate>
		<dc:creator>nahum</dc:creator>
				<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[mod_rails]]></category>
		<category><![CDATA[nginx]]></category>
		<category><![CDATA[passenger]]></category>
		<category><![CDATA[projectx]]></category>
		<category><![CDATA[ruby enterprise edition]]></category>

		<guid isPermaLink="false">http://www.motionstandingstill.com/?p=237</guid>
		<description><![CDATA[So, um yeah, it&#8217;s been a little while since my last blog entry.  This is to say that I&#8217;m still here, things have been too hectic but I&#8217;m starting to get a handle on everything again.
I was already quite busy when everything stepped up a couple of gears around February due to going into business [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">So, um yeah, it&#8217;s been a little while since my last blog entry.  This is to say that I&#8217;m still here, things have been too hectic but I&#8217;m starting to get a handle on everything again.</p>
<p style="text-align: justify;">I was already quite busy when everything stepped up a couple of gears around February due to going into business with  <a href="http://projectx.co.nz/site/staff" target="_blank" class="broken_link">ProjectX</a> and thus my friend <a href="http://twitter.com/johnclegg" target="_blank">John Clegg</a>.  I&#8217;m slowly taking over the back end of the business while he focuses on the front end.  Essentially I get more toys and larger projects to play with :-)</p>
<p style="text-align: justify;">To be realistic I reckon I&#8217;ll only get out a blog entry once every couple of weeks at this stage &#8211; which will be hard as there&#8217;s an increasing amount to blog about.  Top of my list is <a href="http://nginx.net/" target="_blank">Nginx</a> with the recently updated <a href="http://blog.phusion.nl/2009/04/18/phusion-passenger-221-released/" target="_blank">Phusion Passenger</a> and <a href="http://www.rubyenterpriseedition.com/" target="_blank">Ruby Enterprise Edition</a> which is powering our new <a href="http://www.redmine.org/" target="_blank">Redmine</a> server.</p>
<p>Stay tuned.</p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.motionstandingstill.com/silence-stupid-busy/2009-04-26/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Wellington based website optimisation group</title>
		<link>http://www.motionstandingstill.com/wellington-based-website-optimisation-group/2009-02-11/</link>
		<comments>http://www.motionstandingstill.com/wellington-based-website-optimisation-group/2009-02-11/#comments</comments>
		<pubDate>Wed, 11 Feb 2009 10:09:06 +0000</pubDate>
		<dc:creator>nahum</dc:creator>
				<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[optimisation]]></category>
		<category><![CDATA[user group]]></category>
		<category><![CDATA[welloptimised]]></category>

		<guid isPermaLink="false">http://www.motionstandingstill.com/?p=232</guid>
		<description><![CDATA[Something I get asked about a bit and which gets suggested for WellRailed meetings is the topic of optimisation, in particular using MemCache and infrastructure design.  While someone could present on these and other relevant topics within the bounds of Ruby on Rails I think it would be way more sensible to free presentors from [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Something I get asked about a bit and which gets suggested for <a href="http://groups.google.com/group/wellrailed" target="_blank">WellRailed</a> meetings is the topic of optimisation, in particular using MemCache and infrastructure design.  While someone could present on these and other relevant topics within the bounds of Ruby on Rails I think it would be way more sensible to free presentors from being tied to any one particullar framework, language or whatever.  This would interest a greater selection of people thus providing increased benefit to everyone.</p>
<p style="text-align: justify;">So with that in mind I&#8217;ve setup a new google group, <a href="http://groups.google.com/group/welloptimised" target="_blank">WellOptimised</a>.  If there is enough interest I&#8217;ll organise meetings for those here in Wellington.</p>
<p style="text-align: justify;">If you are interested pop along via the above link and introduce yourself.</p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.motionstandingstill.com/wellington-based-website-optimisation-group/2009-02-11/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Yep, the blog new looks better.</title>
		<link>http://www.motionstandingstill.com/yep-the-blog-new-looks-better/2008-11-30/</link>
		<comments>http://www.motionstandingstill.com/yep-the-blog-new-looks-better/2008-11-30/#comments</comments>
		<pubDate>Sun, 30 Nov 2008 04:39:41 +0000</pubDate>
		<dc:creator>nahum</dc:creator>
				<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[minify]]></category>
		<category><![CDATA[theme]]></category>
		<category><![CDATA[yslow]]></category>

		<guid isPermaLink="false">http://www.motionstandingstill.com/?p=207</guid>
		<description><![CDATA[The look and format of this here my blog has just undergone a much delayed overhaul.
Since I occasionally have quite a bit to say, like in my previous entry or this one about where business logic should reside, I&#8217;m now only showing excerpts on the index page rather than whole entries.  I&#8217;ve also gone though [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">The look and format of this here my blog has just undergone a much delayed overhaul.</p>
<p style="text-align: justify;">Since I occasionally have quite a bit to say, like in my <a href="http://www.motionstandingstill.com/starting-simple-with-rails-caching/2008-11-27/" target="_blank">previous entry</a> or this one about where <a href="http://www.motionstandingstill.com/i-dont-use-foreign-key-constraints-with-rails/2008-10-17/" target="_blank">business logic should reside</a>, I&#8217;m now only showing excerpts on the index page rather than whole entries.  I&#8217;ve also gone though and added in a couple of helpful plugins such as auto suggesting related posts and easy social bookmarking.  My next step is to change the source code syntax highlighting css &#8211; I&#8217;ll probably just use the Textmate bundle in Textmate.</p>
<p style="text-align: justify;">Currently the homepage <a href="http://developer.yahoo.com/yslow/" target="_blank">ySlow</a> score is 79, I can get this to 85 if I concatenate and minify the css as doing this reduces size and number of requests for empty caches.  This hasn&#8217;t happened yet as it&#8217;ll require a bit of hand tweaking due to not working out of the box.</p>
<p style="text-align: justify;">Overall I&#8217;ve gone for a more clean, simple and wider look by using <a href="http://themeshaper.com/acamas-theme-clarity-elegance-power/" target="_blank">Acamas</a> which is based on the opensource <a href="http://themeshaper.com/thematic-for-wordpress/" target="_blank" class="broken_link">Thematic framework</a>.  It looks great on a Mac, pretty good under Windows and terrible in IE6.  If you experience any problems, in particullar with the rss feed (now thru feedburner.com) please drop me a line.</p>
<p style="text-align: justify;"><strong>Update:</strong> I&#8217;ve gotten the ySlow score up to 90 now as the css is contenanted and minified plus the sidebar images are now being hosted locally allowing proper expires headers.  With the css files I&#8217;d simply been adding them together in the wrong order.  There is still a problem with <a href="http://developer.yahoo.net/blog/archives/2007/07/high_performanc_6.html" target="_blank">css expressions</a> though, but this will go away when I drop the syntax highlighting plugin &#8211; further boosting my yScore.</p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.motionstandingstill.com/yep-the-blog-new-looks-better/2008-11-30/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Attributes not saving for models with attachments</title>
		<link>http://www.motionstandingstill.com/attributes-not-saving-for-models-with-attachments/2008-11-05/</link>
		<comments>http://www.motionstandingstill.com/attributes-not-saving-for-models-with-attachments/2008-11-05/#comments</comments>
		<pubDate>Tue, 04 Nov 2008 21:53:54 +0000</pubDate>
		<dc:creator>nahum</dc:creator>
				<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[attachment_fu]]></category>
		<category><![CDATA[attr_accessible]]></category>
		<category><![CDATA[attr_protected]]></category>
		<category><![CDATA[blacklist]]></category>
		<category><![CDATA[mass-assign]]></category>
		<category><![CDATA[whitelist]]></category>

		<guid isPermaLink="false">http://www.motionstandingstill.com/?p=170</guid>
		<description><![CDATA[I was upgrading a client&#8217;s application to Rails 2.1.2 from 2.0.x the other day, this resulted in refreshing a number of gems and plugins including will_paginate and attachment_fu.  In doing so I found a stupid problem that I couldn&#8217;t find anything about online &#8211; it&#8217;s a pretty one off unique case so I thought I&#8217;d [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">I was upgrading a client&#8217;s application to Rails 2.1.2 from 2.0.x the other day, this resulted in refreshing a number of gems and plugins including <a href="http://github.com/mislav/will_paginate/tree/master" target="_blank">will_paginate</a> and <a href="http://github.com/technoweenie/attachment_fu/tree/master" target="_blank">attachment_fu</a>.  In doing so I found a stupid problem that I couldn&#8217;t find anything about online &#8211; it&#8217;s a pretty one off unique case so I thought I&#8217;d blog about it to help the next person.  &#8216;Pay it forward&#8217; I guess.</p>
<p style="text-align: justify;">The problem was this: somewhere along the line attachment_fu was updated to execute the following line of code on classes using the <em>has_attachment</em> directive:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">attr_accessible <span style="color:#ff3333; font-weight:bold;">:uploaded_data</span></pre></div></div>

<p style="text-align: justify;">This is taking advantage of a security feature in Rails, it instructs the model to only allow :uploaded_data to be set through <em>update_attributes</em> and any like methods such as <em>new</em> and <em>create</em>.  So <a href="http://en.wikipedia.org/wiki/Whitelist" target="_blank">whitelisting</a> bulk settable attributes.  It doesn&#8217;t raise any errors, just warnings in your log like this:</p>

<div class="wp_syntax"><div class="code"><pre class="log" style="font-family:monospace;">WARNING: Can't mass-assign these protected attributes: &lt;attribute_name&gt;</pre></div></div>

<p style="text-align: justify;">Due to no errors being raised the problem was presenting itself in other parts of the application as expected data was not being found.  Fortunately I was tailing my dev log and noticed the above warning message.</p>
<p style="text-align: justify;">The problem in this situation was the historic code creating instances of the model by passing a hash through to <em>SomeModel.create</em>.  As this area of code was not utilising data posted to an action it was not a security risk.  Very fustrating, especially as this was common practice throughout the codebase.</p>
<p style="text-align: justify;">As I didn&#8217;t want to do a huge bunch of re-keying my initial solution was to comment out the offending line in attachment_fu.  Yes this is a big nasty fix but there was time pressure, other problems to solve and I didn&#8217;t want to be held up having caused an unnecessary amount of manual testing.  In case it was forgotten about  and overriden with a future plugin update, a quick test with big comment was added.</p>
<p style="text-align: justify;">This project hadn&#8217;t updated it&#8217;s gems and plugins for a while, meaning it was a big shock to the system when they where.  This was the biggest headache, all other problems were mainly just gem interfaces changing, so a easy search and replace matter.</p>
<p style="text-align: justify;">I like passing a hash to mass-assign data in Rails as well as the obvious coding benefits it&#8217;s visually more readable too, but this is out weighed by my great dislike of having my sites hacked into.  So it&#8217;s a shame things have had to go this way.</p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.motionstandingstill.com/attributes-not-saving-for-models-with-attachments/2008-11-05/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Wordpress with Nginx on Slicehost</title>
		<link>http://www.motionstandingstill.com/wordpress-with-nginx-on-slicehost/2008-10-23/</link>
		<comments>http://www.motionstandingstill.com/wordpress-with-nginx-on-slicehost/2008-10-23/#comments</comments>
		<pubDate>Wed, 22 Oct 2008 22:28:32 +0000</pubDate>
		<dc:creator>nahum</dc:creator>
				<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[nginx]]></category>
		<category><![CDATA[slicehost]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://www.motionstandingstill.com/?p=162</guid>
		<description><![CDATA[Over the weekend I moved this blog from being hosted on my Dreamhost account to one of my 256mb Slicehost servers.
I&#8217;ve done this mainly to stop paying for hosting twice, but also because if I&#8217;m going to be talking about performance I can&#8217;t possibly have a slow blog and my options were limited with the [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Over the weekend I moved this blog from being hosted on my <a href="http://www.dreamhost.com" target="_blank">Dreamhost</a> account to one of my 256mb <a href="http://www.slicehost.com" target="_blank">Slicehost</a> servers.</p>
<p style="text-align: justify;">I&#8217;ve done this mainly to stop paying for hosting twice, but also because if I&#8217;m going to be talking about performance I can&#8217;t possibly have a slow blog and my options were limited with the Dreamhost account.</p>
<p style="text-align: justify;">My immediate problem being that I wanted to move it straight to being served by Ngnix rather than Apache &#8211; not something that Wordpress is setup for.  After a bit of googling I found that someone had solved all the problems I was going to have for me!  This <a href="http://www.mensk.com/webmaster-toolbox/perfect-ubuntu-hardy-nginx-mysql5-php5-wordpress/" target="_blank">post</a> has blow by blow instructions for setting up a wordpress blog on Slicehost and Ubuntu, I couldn&#8217;t have found anything better.</p>
<p style="text-align: justify;">It worked exactly as advertised, but me being me I could help tweaking and updated the nginx site config to include expire header for static assets, like so:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">server
  <span style="color: #7a0874; font-weight: bold;">&#123;</span>
&nbsp;
  listen   <span style="color: #000000;">80</span>;
  server_name www.motionstandingstill.com;
&nbsp;
  root   <span style="color: #000000; font-weight: bold;">&lt;</span>path_to_site<span style="color: #000000; font-weight: bold;">&gt;</span>;
&nbsp;
  access_log <span style="color: #000000; font-weight: bold;">&lt;</span>custom_log_path<span style="color: #000000; font-weight: bold;">&gt;/</span>access.log;
  error_log <span style="color: #000000; font-weight: bold;">&lt;</span>custom_log_path<span style="color: #000000; font-weight: bold;">&gt;/</span>error.log;
&nbsp;
  location ~<span style="color: #000000; font-weight: bold;">*</span> i.+\.<span style="color: #7a0874; font-weight: bold;">&#40;</span>css<span style="color: #000000; font-weight: bold;">|</span>js<span style="color: #000000; font-weight: bold;">|</span>jpg<span style="color: #000000; font-weight: bold;">|</span>jpeg<span style="color: #000000; font-weight: bold;">|</span>gif<span style="color: #000000; font-weight: bold;">|</span>png<span style="color: #7a0874; font-weight: bold;">&#41;</span>$
    <span style="color: #7a0874; font-weight: bold;">&#123;</span>
    expires      7d;
    <span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
  location <span style="color: #000000; font-weight: bold;">/</span>
    <span style="color: #7a0874; font-weight: bold;">&#123;</span>
    index  index.php index.html;
&nbsp;
    <span style="color: #666666; font-style: italic;"># Basic version of Wordpress parameters, supporting nice permalinks.</span>
    <span style="color: #666666; font-style: italic;"># include /etc/nginx/conf/wordpress_params.regular;</span>
    <span style="color: #666666; font-style: italic;"># Advanced version of Wordpress parameters supporting nice permalinks and WP Super Cache plugin</span>
    include <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>nginx<span style="color: #000000; font-weight: bold;">/</span>conf<span style="color: #000000; font-weight: bold;">/</span>wordpress_params.super_cache;
    <span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;"># pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000</span>
  location ~ \.php$
    <span style="color: #7a0874; font-weight: bold;">&#123;</span>
    fastcgi_pass 127.0.0.1:<span style="color: #000000;">9000</span>;
    fastcgi_index index.php;
    include <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>nginx<span style="color: #000000; font-weight: bold;">/</span>conf<span style="color: #000000; font-weight: bold;">/</span>fastcgi_params;
    fastcgi_param SCRIPT_FILENAME <span style="color: #000000; font-weight: bold;">&lt;</span>site_path<span style="color: #000000; font-weight: bold;">&gt;/</span>public<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$fastcgi_script_name</span>;
    <span style="color: #7a0874; font-weight: bold;">&#125;</span>
  <span style="color: #7a0874; font-weight: bold;">&#125;</span> <span style="color: #666666; font-style: italic;"># server</span></pre></div></div>

<p style="text-align: justify;">Later I found someone had posted a <a href="http://wpmu.org/nginx-wordpress-26/" target="_blank">different conf</a> with easy support for multiple blogs, but I&#8217;ve not moved to using it as what I&#8217;ve got works.  What it also does is add the expire clause support like I have done above.</p>
<p style="text-align: justify;">When moving this blog to Slicehost I initially set it up on a temp subdomain to make sure it was working sweet first and because I wanted to try out some different themes and play with a couple of other things.</p>
<p style="text-align: justify;">I quickly discovered that as it was running on a different doman than it was configured for, Wordpress would redirect my browser to the correct domain meaning I couldn&#8217;t get into the new site.  Ugh.  So I had to whip out my sql skills and manually update the appropriate settings.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">UPDATE</span> wp_options <span style="color: #993333; font-weight: bold;">SET</span> option_value <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'http://temp.motionstandingstill.com'</span> <span style="color: #993333; font-weight: bold;">WHERE</span> option_value <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'http://www.motionstandingstill.com'</span>;</pre></div></div>

<p style="text-align: justify;">I actually used a different domain name and explicitly updated two rows rather than the generic &#8216;where&#8217; clause I have, but you get the idea.  This allowed me in which is what I wanted.</p>
<p style="text-align: justify;">Then I took the opportunity to tidy up what plugins I&#8217;m using as I&#8217;d gotten a bit &#8216;plugin install happy&#8217; when first setting the blog up.  As well as getting rid of unwanted plugins I installed two new plugins I&#8217;d recently found, the first a <a href="http://urbangiraffe.com/plugins/redirection/" target="_blank">redirection plugin with regex support</a> and the second a <a href="http://wordpress.org/extend/plugins/wp-security-scan/" target="_blank">security checking</a> plugin.</p>
<p style="text-align: justify;">Once I&#8217;d got everything how I wanted it, just sticking with the same theme (the misty tree photo is mine), I grabbed a final db backup from Dreamhost and restored it on Slicehost then changed my DNS A record.</p>
<p style="text-align: justify;">All in all, much easier than I thought it would going to be thanks to some helpful results from google. :-)</p>
<p style="text-align: justify;">
<strong>Update</strong>: Slicehost has been <a href="http://www.slicehost.com/blog/" target="_blank">aquired</a> by <a href="http://www.rackspace.com/index.php" target="_blank">Rackspace</a>.</p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.motionstandingstill.com/wordpress-with-nginx-on-slicehost/2008-10-23/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>I don&#8217;t use Foreign Key Constraints with Rails</title>
		<link>http://www.motionstandingstill.com/i-dont-use-foreign-key-constraints-with-rails/2008-10-17/</link>
		<comments>http://www.motionstandingstill.com/i-dont-use-foreign-key-constraints-with-rails/2008-10-17/#comments</comments>
		<pubDate>Thu, 16 Oct 2008 23:45:58 +0000</pubDate>
		<dc:creator>nahum</dc:creator>
				<category><![CDATA[Features]]></category>
		<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[convention over configuration]]></category>
		<category><![CDATA[documentation]]></category>
		<category><![CDATA[dry]]></category>
		<category><![CDATA[foreign key constraints]]></category>
		<category><![CDATA[mvc]]></category>
		<category><![CDATA[pragmatism]]></category>
		<category><![CDATA[premature optmisation]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[ruby on rails]]></category>

		<guid isPermaLink="false">http://www.motionstandingstill.com/?p=152</guid>
		<description><![CDATA[A couple of months back we had Nigel talking at WellRailed about a Foreign Key Constraint plugin he uses with Rails and then just this week the topic was brought up again for me with a client.  I&#8217;ve found it&#8217;s something that pops up every now and again.  Given this is such a polarizing issue [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">A couple of months back we had <a href="http://workingwithrails.com/person/9576-nigel-ramsay" target="_blank">Nigel</a> talking at <a href="http://groups.google.com/group/WellRailed" target="_blank">WellRailed</a> about a Foreign Key Constraint plugin he uses with Rails and then just this week the topic was brought up again for me with a client.  I&#8217;ve found it&#8217;s something that pops up every now and again.  Given this is such a polarizing issue I thought I&#8217;d order my thoughts into a post explaining why I don&#8217;t use foreign key constraints, in particular cascading deletes.</p>
<p style="text-align: justify;">For me the reasoning is distilled down to my programming style.</p>
<p style="text-align: justify;"><strong>Pragmatism</strong></p>
<p style="text-align: justify;">When it comes down to it, what vital benefit does it give you over :dependent =&gt; :destroy or model validation?  Will this give you increased quality faster, quicker and cheaper?</p>
<p style="text-align: justify;"><strong>Premature Optmisation</strong></p>
<p style="text-align: justify;">Basically don&#8217;t waste time optimising something that isn&#8217;t slow or is hardly used.  Rails is so slow that some fractional speedup on the DB isn&#8217;t really going to be noticeable by the end user.  Instead examine your site with <a href="http://www.newrelic.com/" target="_blank">NewRelic</a> or <a href="http://www.fiveruns.com/" target="_blank" class="broken_link">FiveRuns</a> and fix your bottleneck code &#8211; way more bang for your buck.</p>
<p style="text-align: justify;"><strong>Data is Valuable</strong></p>
<p style="text-align: justify;">So don&#8217;t go around deleting it!  If a user wants to delete some data you should want to know why.  Looking at the data can aid in determining why without having to bug the user about it.  Yes there are occasions when this does need to happen, but they should be the exception to the rule and thus explicitly coded for.</p>
<p style="text-align: justify;">Just mark a record as deleted and filter it out automatically, that&#8217;s easy enough to do with rails &#8211; way easier than any other language I&#8217;ve used before.  Better yet, search <a href="http://www.github.com" target="_blank">github</a> and use someone else&#8217;s wheel.</p>
<p style="text-align: justify;"><strong>Convention Over Configuration</strong></p>
<p style="text-align: justify;">Basically put, you do things the way that everyone else has done it.  Walk the well walked path so to speak&#8217; and you won&#8217;t experience any bizzaro problems that someone else hasn&#8217;t already solved before you.  As you&#8217;ve created your app in a predictable way, someone else can easily come in and contribute to your project, or visa-versa and it makes the whole Rails community more accessible to you.</p>
<p style="text-align: justify;">So in following the conventions and as you are not encouraged to use foreign key constraints, Rails isn&#8217;t expecting data to be deleted out from underneath it.</p>
<p style="text-align: justify;"><strong>Don&#8217;t Repeat Yourself &amp;&amp; Model View Controller</strong></p>
<p style="text-align: justify;"><a href="http://en.wikipedia.org/wiki/Don%27t_repeat_yourself" target="_blank">DRY</a> is another of the core philosophies behind Rails &#8211; partials are a real good example of this especially when you start to think of them as essentially methods.  On top of the obvious &#8217;speed to deliver&#8217; benefits this concept provides, it also causes you to distill your code down to being more readable and accessible.  This ties in nicely with <a href="http://en.wikipedia.org/wiki/Model-view-controller" target="_blank">MVC</a> principles as you then tend to group like logic together which is what MVC is all about.</p>
<p style="text-align: justify;">Foreign key constraints are business logic as they describe and implement a behavior &#8211; &#8220;when you delete this record, also delete these other records over here&#8221;.  So why would you go and put some &#8216;important to know about&#8217; business logic in an isolated and often hard to reach place?</p>
<p style="text-align: justify;"><strong>Documentation</strong></p>
<p style="text-align: justify;">The one great thing I learned from <a href="http://www.stevemcconnell.com/" target="_blank">Steve McConnell&#8217;s</a> <a href="http://www.amazon.com/Code-Complete-Practical-Handbook-Construction/dp/0735619670/ref=sr_1_1?ie=UTF8&amp;s=books&amp;qid=1224199994&amp;sr=1-1" target="_blank">Code Complete</a> was how to write comments, they should describe the intention of the code, rather then repeat the code.  This nicely ties in with the DRY principle.</p>
<p style="text-align: justify;">I&#8217;m also a great believer in self documenting code, Rails and Ruby help heaps with this, and generally write comments to help simplify complex code.  Instead of my code being littered with redundant comments cluttering everything up, it just contains important ones as they are more likely to be read and updated when the code is next worked on.</p>
<p style="text-align: justify;">If you use foreign key constraints in your DB you&#8217;ll likely end up documenting them (ie repeating them) somewhere more accessible, in the models being an obvious place.  That wouldn&#8217;t be because the business logic lives there would it?.  Have a think about that.</p>
<p style="text-align: justify;"><strong>The Rails Way</strong></p>
<p style="text-align: justify;">Looking at the Convention over configuration, Don&#8217;t Repeat Yourself and Model View Controller concepts above &#8211; as much as Rails is a framework it&#8217;s also about a way of doing things.</p>
<p style="text-align: justify;">Over the years I&#8217;ve seen a notable number of people, new to rails and these concepts, trying to make rails work their way &#8211; often with less than desirable results.  If you embrace Rails and it&#8217;s &#8216;way&#8217; then it works beautifully and is a pure joy to use, rather than a frustrating experience.</p>
<p style="text-align: justify;"><strong>For me</strong></p>
<p style="text-align: justify;">Coding is an art form and as such a creative process.  If something doesn&#8217;t look, or even feel right then it&#8217;s wrong.  In the end foreign key constraints on a web app just rub me up the wrong way.</p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.motionstandingstill.com/i-dont-use-foreign-key-constraints-with-rails/2008-10-17/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Setting up a resumable remote login to ubuntu with vnc</title>
		<link>http://www.motionstandingstill.com/setting-up-a-resumable-remote-login-to-ubuntu-with-vnc/2008-10-13/</link>
		<comments>http://www.motionstandingstill.com/setting-up-a-resumable-remote-login-to-ubuntu-with-vnc/2008-10-13/#comments</comments>
		<pubDate>Mon, 13 Oct 2008 02:55:00 +0000</pubDate>
		<dc:creator>nahum</dc:creator>
				<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[deb]]></category>
		<category><![CDATA[fedora]]></category>
		<category><![CDATA[remote login]]></category>
		<category><![CDATA[rpm]]></category>
		<category><![CDATA[ubuntu]]></category>
		<category><![CDATA[vnc]]></category>

		<guid isPermaLink="false">http://www.motionstandingstill.com/?p=140</guid>
		<description><![CDATA[I&#8217;ve changed my home server from Centos 5.1 (32bit) to Ubuntu Desktop 8.04 (64bit) due to the CentOS drive crashing &#8211; it was real old.  As my server doesn&#8217;t actually have a screen when it&#8217;s carefully hidden away I initially enabled Remote Desktop for GUI access.  This worked ok, although slightly sluggish, in [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">I&#8217;ve changed my home server from Centos 5.1 (32bit) to Ubuntu Desktop 8.04 (64bit) due to the CentOS drive crashing &#8211; it was real old.  As my server doesn&#8217;t actually have a screen when it&#8217;s carefully hidden away I initially enabled Remote Desktop for GUI access.  This worked ok, although slightly sluggish, in conjunction with <a href="http://sourceforge.net/projects/cotvnc/" target="_blank">Chicken of the VNC</a> running on my MacBook as the viewer.</p>
<p style="text-align: justify;">Problems arose once I rebooted it without the monitor plugged in as it defaulted to 800&#215;600 without any obvious way of controlling this.  Things got real annoying though when I discovered that a number of dialogs are longer than 600 pixels and I couldn&#8217;t see half the windows.  That&#8217;s a real basic thing which shouldn&#8217;t be wrong.</p>
<p style="text-align: justify;">So I tried installing the VNC Server itself and running that with options specifying my desired resolution</p>
<p style="text-align: justify;">

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #c20cb9; font-weight: bold;">install</span> vnc4server
vncserver :<span style="color: #000000;">1</span> <span style="color: #660033;">-geometry</span> 1024x768 <span style="color: #660033;">-depth</span> <span style="color: #000000;">16</span></pre></div></div>

<p style="text-align: justify;">only to be presented with X11 rather than the Gnome Desktop Manager when I connected.  Of note is that when you first run vncserver it&#8217;ll ask you for a password and setup a ~/.vnc folder to store that and an xstartup script.</p>
<p style="text-align: justify;">After a bit of a google I found a helpful post showing <a href="http://ubuntuforums.org/showpost.php?p=5229232&amp;postcount=458" target="_blank">how to configure and launch vncserver through xinetd</a> as well as powering up GDM.  Follow it exactly as it works a treat.  It&#8217;s part of a thread titled <a href="http://ubuntuforums.org/showthread.php?p=5229232#post5229232" target="_blank">HOWTO: Set up VNC Server with resumable sessions</a>.</p>
<p style="text-align: justify;">Logging in though reveled a new problem &#8211; the Gnome Theme Setting manager was constantly crashing and causing the current theme to continually be applied then dropped.  I was given this error &#8220;<em>there was an error starting the GNOME settings daemon</em>&#8220;.</p>
<p style="text-align: justify;">Turns out this is a recent problem with the 4.1.1 version of vnc4server and the fix has yet to be rolled into a package for Ubuntu.  I found <a href="http://www.francescosantini.com/index.php?page=linux&amp;lang=en" target="_blank">custom deb files</a> with the latest version of vnc.  Francesco Santini has helpfully copied across the compiled binaries from the latest vncserver 4.1.2 Fedora 9 packages, which have the fix, into has own i386 deb files for people to download and install &#8211; no help for my 64bit install of Ubuntu though.</p>
<p style="text-align: justify;">I actually tried looking for the vnc source code, thinking I could just compile that directly, but that route started proving harder than I thought it should.  So, I&#8217;ve ended following Francesco&#8217;s lead and copying out the binaries from the Fedora VNC x86_64 packages manually like this:</p>
<p style="text-align: justify;">

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">wget</span> <span style="color: #c20cb9; font-weight: bold;">ftp</span>:<span style="color: #000000; font-weight: bold;">//</span>rpmfind.net<span style="color: #000000; font-weight: bold;">/</span>linux<span style="color: #000000; font-weight: bold;">/</span>fedora<span style="color: #000000; font-weight: bold;">/</span>updates<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">9</span><span style="color: #000000; font-weight: bold;">/</span>x86_64.newkey<span style="color: #000000; font-weight: bold;">/</span>vnc-server-4.1.2-31.fc9.x86_64.rpm
<span style="color: #c20cb9; font-weight: bold;">wget</span> <span style="color: #c20cb9; font-weight: bold;">ftp</span>:<span style="color: #000000; font-weight: bold;">//</span>rpmfind.net<span style="color: #000000; font-weight: bold;">/</span>linux<span style="color: #000000; font-weight: bold;">/</span>fedora<span style="color: #000000; font-weight: bold;">/</span>updates<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">9</span><span style="color: #000000; font-weight: bold;">/</span>x86_64.newkey<span style="color: #000000; font-weight: bold;">/</span>vnc-libs-4.1.2-31.fc9.x86_64.rpm
&nbsp;
rpm2cpio vnc-server-4.1.2-31.fc9.x86_64.rpm <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">cpio</span> <span style="color: #660033;">-dimv</span>
rpm2cpio vnc-libs-4.1.2-31.fc9.x86_64.rpm <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">cpio</span> <span style="color: #660033;">-dimv</span></pre></div></div>

<p style="text-align: justify;">And then copy them over top of my existing vnc binaries like so:</p>
<p style="text-align: justify;">

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">cp</span> usr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/*</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">cp</span> usr<span style="color: #000000; font-weight: bold;">/</span>lib64<span style="color: #000000; font-weight: bold;">/</span>xorg<span style="color: #000000; font-weight: bold;">/</span>modules<span style="color: #000000; font-weight: bold;">/</span>extensions<span style="color: #000000; font-weight: bold;">/</span>libvnc.so <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>lib64<span style="color: #000000; font-weight: bold;">/</span>xorg<span style="color: #000000; font-weight: bold;">/</span>modules<span style="color: #000000; font-weight: bold;">/</span>extensions
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">cp</span> usr<span style="color: #000000; font-weight: bold;">/</span>lib64<span style="color: #000000; font-weight: bold;">/</span>librfb.so.<span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>lib64</pre></div></div>

<p style="text-align: justify;">If you don&#8217;t have &#8216;rpm2cpio&#8217; you&#8217;ll have to install rpm itself, a bit unnecessary and self defeating sounding I know:</p>
<p style="text-align: justify;">

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #c20cb9; font-weight: bold;">install</span></pre></div></div>

<p style="text-align: justify;">I found the latest <a href="http://rpmfind.net/linux/rpm2html/search.php?query=vnc-server&amp;submit=Search+...&amp;system=&amp;arch=" target="_blank">vnc-server</a> and the <a href="http://rpmfind.net/linux/rpm2html/search.php?query=vnc-lib&amp;submit=Search+...&amp;system=&amp;arch=" target="_blank">vnc-libs</a> Fedora 9 x86_64 packages on <a href="http://rpmfind.net" target="_blank">rpmfind.net</a>.</p>
<p style="text-align: justify;">After doing all that I seemed to have made the situation worse and was getting a connection denied error from my viewer.  A but more hunting and the problem turned out to be that the Fedora binaries, Xvnc in particullar, were expecting the libcrypto package and for one of it&#8217;s binaries to be differently named.  I figure that&#8217;s due to them being compiled for Fedora.  The following sorted that problem:</p>
<p style="text-align: justify;">

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #c20cb9; font-weight: bold;">install</span> libcrypto++<span style="color: #000000;">7</span> libcrypto++-utils
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">ln</span> <span style="color: #660033;">-snf</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>libcrypto.so.0.9.8 <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>libcrypto.so.7</pre></div></div>

<p style="text-align: justify;">From memory I don&#8217;t think vncserver had this problem, just Xvnc, making the issue a bit harder to diagnose as running vncserver manually allowed me to connect, but not with the GDM login.</p>
<p style="text-align: justify;">Having done all that I can now login to GDM and don&#8217;t get the crazy theme errors anymore, plus it&#8217;s noticeably faster.  Something I discovered is that Chicken of the VNC doesn&#8217;t disconnect when you close the viewer window &#8211; and thus you can&#8217;t reconnect until you have quit the application.  A bit suck, but oh well.</p>
<p style="text-align: justify;">During my travels I found that I could manually do what xinetd was doing with this command:</p>
<p style="text-align: justify;">

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> vncserver :<span style="color: #000000;">5</span> <span style="color: #660033;">-geometry</span> 1200x750 <span style="color: #007800;">passwordFile</span>=~<span style="color: #000000; font-weight: bold;">/</span>.vnc<span style="color: #000000; font-weight: bold;">/</span><span style="color: #c20cb9; font-weight: bold;">passwd</span> <span style="color: #660033;">-query</span> localhost <span style="color: #660033;">-fp</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>share<span style="color: #000000; font-weight: bold;">/</span>fonts<span style="color: #000000; font-weight: bold;">/</span>X11<span style="color: #000000; font-weight: bold;">/</span>misc</pre></div></div>

<p style="text-align: justify;">Additionally I discovered the &#8216;alien&#8217; package which converts rpm files to deb, which might ultmately be an easier way to install the latest version of vncserver from Fedora while maintaining a single package system.</p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.motionstandingstill.com/setting-up-a-resumable-remote-login-to-ubuntu-with-vnc/2008-10-13/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Be careful how you count in Rails</title>
		<link>http://www.motionstandingstill.com/be-careful-how-you-count/2008-09-12/</link>
		<comments>http://www.motionstandingstill.com/be-careful-how-you-count/2008-09-12/#comments</comments>
		<pubDate>Fri, 12 Sep 2008 00:05:23 +0000</pubDate>
		<dc:creator>nahum</dc:creator>
				<category><![CDATA[Features]]></category>
		<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[eager loading]]></category>
		<category><![CDATA[named_scope]]></category>
		<category><![CDATA[relationships]]></category>

		<guid isPermaLink="false">http://www.motionstandingstill.com/?p=132</guid>
		<description><![CDATA[I&#8217;ve just encountered this problem yet again effecting code I&#8217;m working though, so I thought I&#8217;d quickly blog about it.
Put basically if you call count on a relationship it will always make a db call to see how many there are even if the relationship has been populated.  If you use size or any? they [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">I&#8217;ve just encountered this problem yet again effecting code I&#8217;m working though, so I thought I&#8217;d quickly blog about it.</p>
<p style="text-align: justify;">Put basically if you call <em>count</em> on a relationship it will always make a db call to see how many there are even if the relationship has been populated.  If you use <em>size</em> or <em>any?</em> they will too, unless the relationship is populated, in which case they treat it like an array and leave the DB the heck alone.</p>
<p style="text-align: justify;">For example, when displaying a bunch of users you want nice html when showing if they own any books , so you&#8217;ll probably end up checking user.books.count multiple times per user shown.  This will ask the db for a book count every time you call <em>count</em> and it will do it for all users shown.  Not fun.</p>
<p style="text-align: justify;">Unfortunately, simply using <em>size</em> or <em>any?</em> instead will make no difference unless the relationship is already populated or coincidentally 0 is returned as, for some odd reason, this result is cached by Rails.</p>
<p style="text-align: justify;">Additionally you should eager load if you&#8217;re accessing data within the relationship when rendering the user&#8217;s line  &#8211; you were doing this already ay?  If a &#8216;count&#8217; of books is simply being used rather than book related data then still use <em>size</em> and <em>any?</em> as this may change later down the line and it&#8217;s no extra effort.</p>
<p style="text-align: justify;">Now to throw a spanner in the works, named_scope is different, in fact it behaves the exact opposite. way to  <em>size</em> and <em>any?</em>.  If you call them on a named scope they grab the whole result set (ie all the models), tell you what you want to know and then discard the result set &#8211; EVERY TIME.</p>
<p style="text-align: justify;">Named scopes are not like relationships as they don&#8217;t cache their results, so successive calls to a named scope will hit up the DB each time, ignoring any previous calls made to it.  Ironically, calling <em>count</em> on a named scope makes a &#8216;count(*)&#8217; call to the DB so it&#8217;s much lighter weight than <em>size</em> or <em>any?</em>.</p>
<p style="text-align: justify;">I almost thinking that this behavior is inconsistent and could be changed so that calling <em>count</em>, <em>size</em> and <em>any?</em> on an unpopulated relationship would cache the result for that request as it could actually change during execution potentially causing errors or show something weirdly.</p>
<p style="text-align: justify;">This problem can also occur with caching as cached fragments are usually checked for in actions and then in the view.  The fragment could be expired by a different request in between these two checks causing the view to barf as the action won&#8217;t have set up any data thinking the view won&#8217;t need it.  I&#8217;ve had this unlikely situation happen multiple times and ended up building an in-request cache to combat it.</p>
<p style="text-align: justify;">I feel a plugin, or even attempted patch to Rails welling up from within&#8230;</p>
<p style="text-align: justify;">UPDATE: I forgot all about using <em>length</em>, which muddies the waters further!  <em>Length</em> populates the relationship and then returns it&#8217;s <em>size</em>.  Meaning subsequent calls don&#8217;t talk to the DB or if the relationship was already populated there isn&#8217;t any DB activity either &#8211; just like <em>size</em> and <em>any?</em>.  The big thing to note here though is that it attempts to populate the relationship first which you many not want happening.  Remember eager loading?</p>
<p style="text-align: justify;">With named scopes it behaves the same, but since named_scopes behave differently to relationships there is no caching or populating happening &#8211; so each call involves the database returning all the records and Rails turning them into models.</p>
<p style="text-align: justify;">I&#8217;m still going to be using <em>size</em> and <em>any?</em> as they&#8217;ll do &#8216;count(*)&#8217; calls to the db if the relationship isn&#8217;t populated, otherwise they&#8217;ll just return it&#8217;s array size &#8211; or a boolean in the case of <em>any?</em>.  Meaning a git a bit of code future proof happening &#8211; ie less &#8216;why is that suddenly real slow?&#8217; type situations.</p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.motionstandingstill.com/be-careful-how-you-count/2008-09-12/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Performance optimisation through named_scope</title>
		<link>http://www.motionstandingstill.com/performance-optimisation-through-named_scope/2008-09-11/</link>
		<comments>http://www.motionstandingstill.com/performance-optimisation-through-named_scope/2008-09-11/#comments</comments>
		<pubDate>Wed, 10 Sep 2008 23:42:27 +0000</pubDate>
		<dc:creator>nahum</dc:creator>
				<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[lambda]]></category>
		<category><![CDATA[named_scope]]></category>
		<category><![CDATA[optimisation]]></category>
		<category><![CDATA[performance]]></category>

		<guid isPermaLink="false">http://www.motionstandingstill.com/?p=124</guid>
		<description><![CDATA[I recently gave a presentation about using named_scopes to WellRailed and while it was still on my mind I thought that I&#8217;d cover named_scope in conjunction with some performance optimisation work I&#8217;ve been doing.
I used to use relationship extensions lots and it always irked me that I was putting business logic for a model, say [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">I recently gave a presentation about using named_scopes to <a href="http://groups.google.com/group/WellRailed" target="_blank">WellRailed</a> and while it was still on my mind I thought that I&#8217;d cover named_scope in conjunction with some performance optimisation work I&#8217;ve been doing.</p>
<p style="text-align: justify;">I used to use relationship extensions lots and it always irked me that I was putting business logic for a model, say Book, on a related model, Owner, like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Owner <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
&nbsp;
  has_many <span style="color:#ff3333; font-weight:bold;">:books</span>, <span style="color:#ff3333; font-weight:bold;">:as</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'owner'</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    <span style="color:#9966CC; font-weight:bold;">def</span> old
      all<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:conditions</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'published &lt; ?'</span>, 20.<span style="color:#9900CC;">years</span>.<span style="color:#9900CC;">ago</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p style="text-align: justify;">Especially if multiple related models wanted the same logic:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Company <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
&nbsp;
  has_many <span style="color:#ff3333; font-weight:bold;">:books</span>, <span style="color:#ff3333; font-weight:bold;">:as</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'owner'</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    <span style="color:#9966CC; font-weight:bold;">def</span> old
      all<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:conditions</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'published_at &lt; ?'</span>, 20.<span style="color:#9900CC;">years</span>.<span style="color:#9900CC;">ago</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p style="text-align: justify;">So to eliminate any code duplication problems down the line I would put the business logic in a module within book.rb:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">Module</span> BookExtention
  <span style="color:#9966CC; font-weight:bold;">def</span> old
    all<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:conditions</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'published_at &lt; ?'</span>, 20.<span style="color:#9900CC;">years</span>.<span style="color:#9900CC;">ago</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p style="text-align: justify;">and extend the relationships using that instead:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Owner <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  has_many <span style="color:#ff3333; font-weight:bold;">:books</span>, <span style="color:#ff3333; font-weight:bold;">:extend</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> BookExtention
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> Company <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  has_many <span style="color:#ff3333; font-weight:bold;">:books</span>, <span style="color:#ff3333; font-weight:bold;">:extend</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> BookExtention
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p style="text-align: justify;">That still didn&#8217;t completely resolve my discomfort though as the business logic was still outside the model itself.  It was enough for me to move on to other problems though due to the code duplication issue being negated.</p>
<p style="text-align: justify;">Why use extensions at all I expect you might think?  It means in your actions you don&#8217;t have to continually repeat the condition and working through relationships means you have no security worries due to Rails taking care of that for you &#8211; walk the relationships for tighter more secure code people</p>
<p style="text-align: justify;">Another way of getting a list of the user&#8217;s old books is this way:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">current_user.<span style="color:#9900CC;">books</span>.<span style="color:#CC0066; font-weight:bold;">select</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>b<span style="color:#006600; font-weight:bold;">|</span> b.<span style="color:#9900CC;">published_at</span> <span style="color:#006600; font-weight:bold;">&lt;</span> 20.<span style="color:#9900CC;">years</span>.<span style="color:#9900CC;">ago</span> <span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p style="text-align: justify;">Yep this works and is real quick on your dev machine with a limited dataset, say your fixtures for example.  Here is what it&#8217;s doing.  It&#8217;s getting ALL the user&#8217;s books from the DB, rails is then turning ALL that data into models, slow and memory hungry, and then the select is iterating over ALL the user&#8217;s books to find just the old ones.  If you&#8217;re planning on people actually using your website, then use the DB for what it&#8217;s good at and filter the data as much as you can BEFORE it hands it over to your app.</p>
<p style="text-align: justify;">Worse still is if this was done:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">Books.<span style="color:#9900CC;">all</span>.<span style="color:#CC0066; font-weight:bold;">select</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>b<span style="color:#006600; font-weight:bold;">|</span> b.<span style="color:#9900CC;">published_at</span> <span style="color:#006600; font-weight:bold;">&lt;</span> 20.<span style="color:#9900CC;">years</span>.<span style="color:#9900CC;">ago</span> <span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p style="text-align: justify;">Additionally, don&#8217;t use your fixtures for development data, use a recent backup form your production server, when that gets too big use a subset of that.  Don&#8217;t have user generated data?  Make some up.  I jam chunks of <a href="http://en.wikipedia.org/wiki/Wikipedia:Database_download" target="_blank">Wikipedia</a> into my database when I don&#8217;t have any real data to play with.  Unless you do this you won&#8217;t know how your code performs in real life.</p>
<p style="text-align: justify;">I&#8217;ve seen this style of coding quite a bit as Rails with it&#8217;s <a href="http://en.wikipedia.org/wiki/Object-relational_mapping" target="_blank">ORM</a> wrapper, ActiveRecord, encourages developers to discard the shackles of SQL and do it all in ruby using rails with highly readable code.  I&#8217;m all for that, but you have to write SQL as some point and this is where named scopes come in.  They allow you to define (name) bunches of business logic (scope) and then string them together &#8211; which you can&#8217;t do with relationship extensions, for example:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">Module</span> BookExtention
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> old
    all<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:conditions</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'published &lt; ?'</span>, 20.<span style="color:#9900CC;">years</span>.<span style="color:#9900CC;">ago</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> romantic
&nbsp;
    all<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:conditions</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;genre = 'romantic'&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p style="text-align: justify;">What if I want old romantic books?  I have to use one of the methods and then manually select based on the other&#8217;s condition.  Ugh!</p>
<p style="text-align: justify;">This is where named scope steps up to the plate and impresses the crowd, instead of the relationship extension do this in your model:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Book <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
&nbsp;
  belongs_to <span style="color:#ff3333; font-weight:bold;">:owner</span>, <span style="color:#ff3333; font-weight:bold;">:polymorphic</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span>
&nbsp;
  named_scope <span style="color:#ff3333; font-weight:bold;">:old</span>, <span style="color:#ff3333; font-weight:bold;">:conditions</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'published &lt; ?'</span>, 20.<span style="color:#9900CC;">years</span>.<span style="color:#9900CC;">ago</span><span style="color:#006600; font-weight:bold;">&#93;</span>
  named_scope <span style="color:#ff3333; font-weight:bold;">:romantic</span>, <span style="color:#ff3333; font-weight:bold;">:conditions</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;genre = 'romantic'&quot;</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p style="text-align: justify;">The beautiful thing here is that all your existing code which references the old methods off of the books relationship do not need to change as this still works:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">current_user.<span style="color:#9900CC;">books</span>.<span style="color:#9900CC;">old</span></pre></div></div>

<p style="text-align: justify;">Basically named_scope is jamming the :conditions into the current finder scope and applying that along with the relationships scope and defaulting to calling &#8216;all&#8217;.  This means you can also do these:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">current_user.<span style="color:#9900CC;">books</span>.<span style="color:#9900CC;">old</span>.<span style="color:#9900CC;">all</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:limit</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">20</span><span style="color:#006600; font-weight:bold;">&#41;</span>
Books.<span style="color:#9900CC;">romantic</span>.<span style="color:#9900CC;">count</span></pre></div></div>

<p style="text-align: justify;">Instead of &#8216;all&#8217; you can call what ever you normally could off of the relationship or the model&#8217;s class. And it generates the appropriate SQL given the scopes in play at the time. Nice. As the named_scope is just pushing itself onto the :find scope you can string them together like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">current_user.<span style="color:#9900CC;">books</span>.<span style="color:#9900CC;">old</span>.<span style="color:#9900CC;">romantic</span>.<span style="color:#9900CC;">all</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:limit</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">20</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p style="text-align: justify;">This means that the DB will only return the data that you want, so no time or memory is wasted on data that would immediately be discarded by running a select over the users old books.  Hurray.</p>
<p style="text-align: justify;">As named_scopes are defined at class level the 20.years.ago is actually executed as the class definition is read into memory when the rails app first spins up.  So if it was running for a year it would actually be 21.years.ago.  To handle this you can do the following:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">named_scope <span style="color:#ff3333; font-weight:bold;">:old</span>, <span style="color:#CC0066; font-weight:bold;">lambda</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">&#123;</span>:conditions <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'published &lt; ?'</span>, 20.<span style="color:#9900CC;">years</span>.<span style="color:#9900CC;">ago</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p style="text-align: justify;">Each time &#8216;old&#8217; is used the lambda is executed returning a hash of finder options, in this case just a condition.  Your eyes are probably lighting up at this stage thinking of the possibilities with dynamic named scopes and yes you can pass parameters to them.  Thats for my next blog entry though sorry.</p>
<p style="text-align: justify;">In summary, this gives two types of optimisation.  Firstly higher quality though better readability, cleaner and tighter code, no code duplication and business logic living where it should be.  Secondly, not only can you serve more requests from the same hardware due to faster execution, but because it&#8217;s more memory efficient it&#8217;s likely you can run more application instances, ie mongrel, on that same hardware.</p>
<p style="text-align: justify;">So that covers the basics of why I like named_scope, in my next entry I&#8217;ll take this a couple of big steps further and possibly get into some nitty gritty log examinations.</p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.motionstandingstill.com/performance-optimisation-through-named_scope/2008-09-11/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 1.554 seconds -->
