<?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>Floorplanner Tech Blog &#187; rails</title>
	<atom:link href="http://techblog.floorplanner.com/tag/rails/feed/" rel="self" type="application/rss+xml" />
	<link>http://techblog.floorplanner.com</link>
	<description>Our latest geek adventures!</description>
	<lastBuildDate>Tue, 16 Mar 2010 18:45:44 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>RESTful uploading of files using XML</title>
		<link>http://techblog.floorplanner.com/2010/02/15/restful-uploading-of-files-using-xml/</link>
		<comments>http://techblog.floorplanner.com/2010/02/15/restful-uploading-of-files-using-xml/#comments</comments>
		<pubDate>Mon, 15 Feb 2010 11:34:31 +0000</pubDate>
		<dc:creator>Willem van Bergen</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[embed]]></category>
		<category><![CDATA[file]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[upload]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://techblog.floorplanner.com/?p=940</guid>
		<description><![CDATA[After searching for too long to find any documentation on this topic, this is just as a reminder for myself. Hopefully, it&#8217;ll solve somebody else&#8217;s problem as well.
To send a file to a Rails application, using a RESTful XML API call, use the following XML snippet:

&#60;field type=&#34;file&#34; name=&#34;filename.ext&#34; content-type=&#34;mime/type&#34;&#62;
  base64-encoded file contents
&#60;/field&#62;

The file contents [...]]]></description>
			<content:encoded><![CDATA[<p>After searching for too long to find any documentation on this topic, this is just as a reminder for myself. Hopefully, it&#8217;ll solve somebody else&#8217;s problem as well.</p>
<p>To send a file to a Rails application, using a RESTful XML API call, use the following XML snippet:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family: Monaco,monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;field</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;file&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;filename.ext&quot;</span> <span style="color: #000066;">content-type</span>=<span style="color: #ff0000;">&quot;mime/type&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  base64-encoded file contents
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/field<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>The file contents should be base64 encoded, and may be encapsulated in a CDATA block:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family: Monaco,monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;greeting</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;file&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;greeting.txt&quot;</span> <span style="color: #000066;">content-type</span>=<span style="color: #ff0000;">&quot;text/plain&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #339933;">&lt;![CDATA[SGVsbG8gd29ybGQh]]&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/greeting<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>You can test this by opening a console and feed the XML to <code>Hash.from_xml</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="sh" style="font-family: Monaco,monospace;">&gt;&gt; greeting = &lt;&lt;-EOXML
    &lt;greeting type=&quot;file&quot; name=&quot;greeting.txt&quot; content-type=&quot;text/plain&quot;&gt;
      &lt;![CDATA[SGVsbG8gd29ybGQh]]&gt;
    &lt;/greeting&gt;
  EOXML
&nbsp;
&gt;&gt; data = Hash.from_xml(greeting)
=&gt; {&quot;greeting&quot;=&gt;#&lt;StringIO:0x10617d9d0&gt;}
&gt;&gt; data['greeting'].content_type
=&gt; &quot;text/plain&quot;
&gt;&gt; data['greeting'].original_filename
=&gt; &quot;greeting.txt&quot;
&gt;&gt; data['greeting'].read
=&gt; &quot;Hello world!&quot;</pre></div></div>

<p>Now you can handle files using your XML API just like you would use file uploads in your HTML forms.</p>
]]></content:encoded>
			<wfw:commentRss>http://techblog.floorplanner.com/2010/02/15/restful-uploading-of-files-using-xml/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Two practical examples of Rack middleware in Rails</title>
		<link>http://techblog.floorplanner.com/2010/01/29/two-practical-examples-of-rack-middleware-in-rails/</link>
		<comments>http://techblog.floorplanner.com/2010/01/29/two-practical-examples-of-rack-middleware-in-rails/#comments</comments>
		<pubDate>Fri, 29 Jan 2010 08:38:30 +0000</pubDate>
		<dc:creator>Willem van Bergen</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[examples]]></category>
		<category><![CDATA[logging]]></category>
		<category><![CDATA[middleware]]></category>
		<category><![CDATA[rack]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[redirect]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://techblog.floorplanner.com/?p=824</guid>
		<description><![CDATA[After Rails moved to Rack as server interface, the ability the use Rack middleware was one of the most touted advantages. At first, it wasn&#8217;t very clear to me why this was such a big deal. However, I have applied Rack middleware in the last month on several occasions. I thought it might be interesting [...]]]></description>
			<content:encoded><![CDATA[<p>After Rails moved to Rack as server interface, the ability the use Rack middleware was one of the most touted advantages. At first, it wasn&#8217;t very clear to me why this was such a big deal. However, I have applied Rack middleware in the last month on several occasions. I thought it might be interesting for other Rails developers to see some practical examples of middleware, to see where they can be applied.</p>
<h3> Redirecting</h3>
<p>After a complete rewrite, we <a href="http://floorplanner.com/weblog/new-version-goes-live">released the new version of Floorplanner over a year ago</a>. We had to find a way to deal with the thousands of links that were in use by our customers and indexed by Google. </p>
<p>Some of these could simply redirect to their new location, e.g. <a rel="nofollow" href="http://www.floorplanner.com/tryit">http://www.floorplanner.com/tryit</a> can now be found on <a href="http://www.floorplanner.com/demo">http://www.floorplanner.com/demo</a>. These redirects can of course simply be implemented in either Rails or Apache. </p>
<p>However, we also had a long list of URLs on which floor plans were published using the old version. These plans cannot be migrated to the new version easily, so we had to keep an instance of the old Rails application running. Requests to these URLs should be redirected to this separate instance, now hosted on a different domain name. Moreover, it is important that it only tries to redirect to the old version if the URL cannot be resolved in the new version, because the plan may have been migrated to or recreated on the new version.</p>
<p>We previously implemented this by adding a catch all route to our application, and a custom handler for the <code>ActiveRecord::RecordNotFound</code> exception in our application. This was a rather ugly solution, and we rather not have a dependency on the old version&#8217;s database in the new version&#8217;s code. </p>
<p>I decided to rewrite this functionality with a piece of Rack middleware. It basically executes every request with our Rails application, and if it returns a 404 response, it checks if the URL exists on the old version. If so, it redirects the user to the old version, otherwise, it will respond with the 404 response that was generated by Rails.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family: Monaco,monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> RedirectToOldVersionMiddleware
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>app<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@app</span> = app
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> call<span style="color:#006600; font-weight:bold;">&#40;</span>env<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#008000; font-style:italic;"># execute the request using our Rails app</span>
   status, headers, body = <span style="color:#0066ff; font-weight:bold;">@app</span>.<span style="color:#9900CC;">call</span><span style="color:#006600; font-weight:bold;">&#40;</span>env<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">if</span> status == <span style="color:#006666;">404</span> <span style="color:#006600; font-weight:bold;">&amp;&amp;</span> url = find_redirect<span style="color:#006600; font-weight:bold;">&#40;</span>env<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'REQUEST_URI'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
      <span style="color:#008000; font-style:italic;"># Issue a &quot;Moved permanently&quot; response with the redirect location</span>
      <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">301</span>, <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#996600;">&quot;Location&quot;</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> url<span style="color:#006600; font-weight:bold;">&#125;</span>, <span style="color:#996600;">'Redirecting you to the new location...'</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    <span style="color:#9966CC; font-weight:bold;">else</span>
&nbsp;
      <span style="color:#008000; font-style:italic;"># Not a 404 or no redirect found, just send the response as is</span>
      <span style="color:#006600; font-weight:bold;">&#91;</span>status, headers, body<span style="color:#006600; font-weight:bold;">&#93;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  REDIRECT_HOST = <span style="color:#996600;">'http://old.floorplanner.version'</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> find_redirect<span style="color:#006600; font-weight:bold;">&#40;</span>path<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#008000; font-style:italic;"># See if we can find a valid URL on the old version.</span>
    Redirect.<span style="color:#9900CC;">exists</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>path<span style="color:#006600; font-weight:bold;">&#41;</span> ? <span style="color:#996600;">&quot;#{REDIRECT_HOST}#{path}&quot;</span> : <span style="color:#0000FF; font-weight:bold;">nil</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>This way, all the code that is related to redirecting users to the old version is contained in a single file, and doesn&#8217;t pollute our application anymore. Moreover, when all the old plans are migrated, or we decide we do not longer support these old plans, we can simply disable the middleware in our <code>environment.rb</code>, without having to alter any code.</p>
<h3> Logging complete requests for debugging purposes </h3>
<p>Another use for Rack middleware is logging: one of our customers notified us that an API call they were using was failing, but only some of the time. Unfortunately, we did not find any errors in our exception log, which made it very hard to debug this issue. After some time, we discovered that Rails was returning a 422 response, without the request ever &#8220;arriving&#8221; in our own code. Apparently, the request was stopped from processing somewhere in the Rails framework.</p>
<p>Unfortunately, we were not able to recreate this issue ourselves, and we started suspecting that the request was not well-formed, because Rails sends a 422 response in this case. To discover if this really was the issue, I wrote a piece of middleware that logs the complete request, before it is passed to the Rails framework for further processing.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family: Monaco,monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> RequestLoggerMiddleware
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>app<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@app</span> = app
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  REQUEST_LOG_DIR = Rails.<span style="color:#9900CC;">root</span>.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'log'</span>, <span style="color:#996600;">'api_calls'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> call<span style="color:#006600; font-weight:bold;">&#40;</span>env<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#008000; font-style:italic;"># log the request if it is a troublesome API call.</span>
    <span style="color:#9966CC; font-weight:bold;">if</span> env<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'REQUEST_URI'</span><span style="color:#006600; font-weight:bold;">&#93;</span> == <span style="color:#996600;">'/failing_api.xml'</span>
&nbsp;
      filename = <span style="color:#996600;">&quot;api_call_#{Time.now.strftime('%Y%m%d%H%M%S')}.log&quot;</span>
      <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span>REQUEST_LOG_DIR.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span>filename<span style="color:#006600; font-weight:bold;">&#41;</span>, <span style="color:#996600;">'wb'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>file<span style="color:#006600; font-weight:bold;">|</span>
        file.<span style="color:#CC0066; font-weight:bold;">puts</span><span style="color:#006600; font-weight:bold;">&#40;</span>env<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'rack.input'</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">read</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:#008000; font-style:italic;"># now, execute the request using our Rails app</span>
    response = <span style="color:#0066ff; font-weight:bold;">@app</span>.<span style="color:#9900CC;">call</span><span style="color:#006600; font-weight:bold;">&#40;</span>env<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>Using these request log files, API calls can be inspected exactly before any of the Rails magic happens. After resolving the issue, removing this logging was as simple as commenting a line in <code>environment.rb</code>.</p>
]]></content:encoded>
			<wfw:commentRss>http://techblog.floorplanner.com/2010/01/29/two-practical-examples-of-rack-middleware-in-rails/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Request-log-analyzer 1.6.0</title>
		<link>http://techblog.floorplanner.com/2010/01/08/request-log-analyzer-1-6-0/</link>
		<comments>http://techblog.floorplanner.com/2010/01/08/request-log-analyzer-1-6-0/#comments</comments>
		<pubDate>Fri, 08 Jan 2010 13:56:28 +0000</pubDate>
		<dc:creator>Willem van Bergen</dc:creator>
				<category><![CDATA[Databases]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[delayed::job]]></category>
		<category><![CDATA[log analysis]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[postgresql]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[release]]></category>
		<category><![CDATA[request-log-analyzer]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://techblog.floorplanner.com/?p=828</guid>
		<description><![CDATA[Bart &#38; I just released request-log-analyzer 1.6.0. New features since the 1.5.0 release:

PostgreSQL query log support;
Delayed::Job log support;
Small fixes in the Rails file format;
Various other small fixes and improvements.

As always, run the following command to install or upgrade to the latest version:

sudo gem install request-log-analyzer

]]></description>
			<content:encoded><![CDATA[<p>Bart &amp; I just released <a href="http://wiki.github.com/wvanbergen/request-log-analyzer">request-log-analyzer</a> 1.6.0. New features since the 1.5.0 release:</p>
<ul>
<li>PostgreSQL query log support;</li>
<li>Delayed::Job log support;</li>
<li>Small fixes in the Rails file format;</li>
<li>Various other small fixes and improvements.</li>
</ul>
<p>As always, run the following command to install or upgrade to the latest version:</p>

<div class="wp_syntax"><div class="code"><pre class="sh" style="font-family: Monaco,monospace;">sudo gem install request-log-analyzer</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://techblog.floorplanner.com/2010/01/08/request-log-analyzer-1-6-0/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Request-log-analyzer 1.5.0</title>
		<link>http://techblog.floorplanner.com/2009/11/18/request-log-analyzer-1-5-0/</link>
		<comments>http://techblog.floorplanner.com/2009/11/18/request-log-analyzer-1-5-0/#comments</comments>
		<pubDate>Wed, 18 Nov 2009 17:51:42 +0000</pubDate>
		<dc:creator>Willem van Bergen</dc:creator>
				<category><![CDATA[Databases]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[request-log-analyzer]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[slow query log]]></category>

		<guid isPermaLink="false">http://techblog.floorplanner.com/?p=801</guid>
		<description><![CDATA[Bart and I just released request-log-analyzer version 1.5.0. New features include:

MySQL slow query log format support to analyze what queries are slowing down your database.
Format autodetection: with all those supported file formats, remembering the right --format parameter gets tricky. With format autodetection, this usually is not needed anymore!

As always, run the following command to install [...]]]></description>
			<content:encoded><![CDATA[<p>Bart and I just released <a href="http://github.com/wvanbergen/request-log-analyzer">request-log-analyzer</a> version 1.5.0. New features include:</p>
<ul>
<li><a href="http://wiki.github.com/wvanbergen/request-log-analyzer/mysql-slow-query-log">MySQL slow query log</a> format support to analyze what queries are slowing down your database.</li>
<li><strong>Format autodetection:</strong> with all those supported file formats, remembering the right <code style="white-space: nowrap">--format</code> parameter gets tricky. With format autodetection, this usually is not needed anymore!</li>
</ul>
<p>As always, run the following command to install or upgrade to the latest version:</p>

<div class="wp_syntax"><div class="code"><pre class="sh" style="font-family: Monaco,monospace;">$ gem install request-log-analyzer</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://techblog.floorplanner.com/2009/11/18/request-log-analyzer-1-5-0/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Case-insensitive validates_uniqueness_of slowness</title>
		<link>http://techblog.floorplanner.com/2009/11/17/case-insensitive-validates_uniqueness_of-slowness/</link>
		<comments>http://techblog.floorplanner.com/2009/11/17/case-insensitive-validates_uniqueness_of-slowness/#comments</comments>
		<pubDate>Tue, 17 Nov 2009 14:01:02 +0000</pubDate>
		<dc:creator>Willem van Bergen</dc:creator>
				<category><![CDATA[Databases]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[case insensitive]]></category>
		<category><![CDATA[index]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[validates_uniqueness_of]]></category>

		<guid isPermaLink="false">http://techblog.floorplanner.com/?p=787</guid>
		<description><![CDATA[Watch out when using validates_uniqueness_of :field, :case_sensitive => false. Rails transforms this in a query that cannot be supported by an index, which will really slow validation down if the underlying table grows larger.
For example, we use validates_uniqueness_of to check for duplicate e-mail addresses. Because email addresses are case-insensitive, adding :case_sensitive => false seems like [...]]]></description>
			<content:encoded><![CDATA[<p>Watch out when using <code>validates_uniqueness_of :field, :case_sensitive => false</code>. Rails transforms this in a query that cannot be supported by an index, which will really slow validation down if the underlying table grows larger.</p>
<p>For example, we use <code>validates_uniqueness_of</code> to check for duplicate e-mail addresses. Because email addresses are case-insensitive, adding <code>:case_sensitive => false</code> seems like a natural choice. However, this results in the following queries:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family: Monaco,monospace;"><span style="color: #808080; font-style: italic;"># For a new User instance:</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> id <span style="color: #993333; font-weight: bold;">FROM</span> users 
 <span style="color: #993333; font-weight: bold;">WHERE</span> LOWER<span style="color: #66cc66;">&#40;</span>users<span style="color: #66cc66;">.</span>email<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">BINARY</span> <span style="color: #ff0000;">'user@example.com'</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># For an existing User instance:</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> id <span style="color: #993333; font-weight: bold;">FROM</span> users 
 <span style="color: #993333; font-weight: bold;">WHERE</span> LOWER<span style="color: #66cc66;">&#40;</span>users<span style="color: #66cc66;">.</span>email<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">BINARY</span> <span style="color: #ff0000;">'user@example.com'</span> 
   <span style="color: #993333; font-weight: bold;">AND</span> users<span style="color: #66cc66;">.</span>id <span style="color: #66cc66;">&lt;&gt;</span> <span style="color: #cc66cc;">42</span></pre></div></div>

<p>This query cannot be optimized by a (unique) index on the email field and thus has to scan the full table. As our users table grew larger, these queries started to show up in our slow query log. </p>
<p>However, MySQL uses case-insensitive comparison by default. (To be exact, case-sensitiveness depends on the current collation, which can vary. Rails generates the weird query to make sure the check works, regardless of the current collation.) The conversion to lowercase therefore is not necessary for a uniqueness check (as long as the field has a case-insensitive collation like <code>utf8_general_ci</code>). I decided to write my own validation method that issues a query that can be optimized by a query.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family: Monaco,monospace;">  <span style="color:#008000; font-style:italic;"># Alternative for validates_uniqueness_of :email, :case_sensitive =&gt; false</span>
  validate <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>user<span style="color:#006600; font-weight:bold;">|</span>
    conditions = <span style="color:#996600;">&quot;users.email = :email&quot;</span>
    conditions <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#996600;">&quot; AND users.id != :id&quot;</span> <span style="color:#9966CC; font-weight:bold;">unless</span> user.<span style="color:#9900CC;">new_record</span>?
    conditions = <span style="color:#006600; font-weight:bold;">&#91;</span>conditions, <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:email</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> user.<span style="color:#9900CC;">email</span>, <span style="color:#ff3333; font-weight:bold;">:id</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> user.<span style="color:#9900CC;">id</span> <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    <span style="color:#9966CC; font-weight:bold;">if</span> User.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:first</span>, <span style="color:#ff3333; font-weight:bold;">:select</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:id</span>, <span style="color:#ff3333; font-weight:bold;">:conditions</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> conditions<span style="color:#006600; font-weight:bold;">&#41;</span>
      user.<span style="color:#9900CC;">errors</span>.<span style="color:#9900CC;">add</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:email</span>, <span style="color:#996600;">'Already in use'</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>There is <a href="https://rails.lighthouseapp.com/projects/8994/tickets/2503-validates_uniqueness_of-is-horribly-inefficient-in-mysql">a ticket for this issue in Rails&#8217;s Lighthouse</a>, but as of yet this issue is unresolved. For now, this solution works to keep our slow query log nice and quiet!</p>
]]></content:encoded>
			<wfw:commentRss>http://techblog.floorplanner.com/2009/11/17/case-insensitive-validates_uniqueness_of-slowness/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Request-log-analyzer 1.4.0</title>
		<link>http://techblog.floorplanner.com/2009/09/30/request-log-analyzer-1-4-0/</link>
		<comments>http://techblog.floorplanner.com/2009/09/30/request-log-analyzer-1-4-0/#comments</comments>
		<pubDate>Wed, 30 Sep 2009 09:40:04 +0000</pubDate>
		<dc:creator>Willem van Bergen</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Amazon S3]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[log analysis]]></category>
		<category><![CDATA[merb]]></category>
		<category><![CDATA[profiling]]></category>
		<category><![CDATA[Rack CommonLogger]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[request-log-analyzer]]></category>

		<guid isPermaLink="false">http://techblog.floorplanner.com/?p=729</guid>
		<description><![CDATA[Bart and I have been working a lot on request-log-analyzer lately, our tool to produce performance reports for web applications based on their log files. Today, we released version 1.4.0, which boasts many new features since I last blogged about a release. The changelog contains all changes we have implemented recently with some additional information, [...]]]></description>
			<content:encoded><![CDATA[<p>Bart and I have been working a lot on <a href="http://github.com/wvanbergen/request-log-analyzer">request-log-analyzer</a> lately, our tool to produce performance reports for web applications based on their log files. Today, we released version 1.4.0, which boasts many new features since I last blogged about a release. The <a href="http://wiki.github.com/wvanbergen/request-log-analyzer/changelog">changelog</a> contains all changes we have implemented recently with some additional information, but these are the highlights:</p>
<ul>
<li><strong>New and improved log formats:</strong> r-l-a can now handle <a href="http://wiki.github.com/wvanbergen/request-log-analyzer/apache-access-log">Apache access logs</a>, <a href="http://wiki.github.com/wvanbergen/request-log-analyzer/apache-access-log#rack">Rack CommonLogger logs</a> and <a href="http://wiki.github.com/wvanbergen/request-log-analyzer/amazon-s3-access-log">Amazon S3 access logs</a>. Moreover, the Rails format has been restructured to offer <a href="http://wiki.github.com/wvanbergen/request-log-analyzer/rails-request-log">more flexibility</a>.</li>
<li><strong>Improved database support: </strong>the database supports other databases than SQLite3 as well, and r-l-a can append information to an existing database instead of overwriting it. Moreover, a console tool similar to Rails&#8217;s script/console is bundled to inspect the database and run queries on it easily.</li>
<li><strong>Added standard deviation to reports: </strong>the standard deviation measure has been added to duration and traffic reports to get some feel of the variation in values besides the mean.</li>
<li><strong>E-mailing reports:</strong> r-l-a can email the performance report to a given e-mail address. This can be useful when running r-l-a in a cron job.</li>
<li><strong>Compressed log support:</strong> r-l-a will decompress compressed logs automatically.</li>
<li><strong>Speed improvements:</strong> we have profiled <a href="http://techblog.floorplanner.com/2009/09/27/performance-tweaking-of-ruby-algorithms/">request-log-analyzer itself</a> and significantly improved its performance.</li>
<li><strong>API:</strong> we created a basic API so it is possible to use the r-l-a engine as a library as well.</li>
<li><strong>Monitoring integration:</strong> integrate performance information into your <a href="http://github.com/barttenbrinke/munin-plugins-rails/">Munin dashboard</a> or your <a href="http://wiki.github.com/wvanbergen/request-log-analyzer/scout">Scout account</a>.</li>
</ul>
<p>As always, use <code>sudo gem install request-log-analyzer</code> to install or upgrade.</p>
<h3>Ruby en Rails 2009 conference</h3>
<p>Bart and I will be presenting request-log-analyzer and performance tuning of Rails applications in general at the <a href="http://2009.rubyenrails.nl">Ruby en Rails</a> conference in Amsterdam, October 30-31 2009. We hope to see you there!</p>
]]></content:encoded>
			<wfw:commentRss>http://techblog.floorplanner.com/2009/09/30/request-log-analyzer-1-4-0/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Adyen payment services for Rails</title>
		<link>http://techblog.floorplanner.com/2009/09/27/adyen-payment-services-for-rails/</link>
		<comments>http://techblog.floorplanner.com/2009/09/27/adyen-payment-services-for-rails/#comments</comments>
		<pubDate>Sun, 27 Sep 2009 18:27:23 +0000</pubDate>
		<dc:creator>Willem van Bergen</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[adyen]]></category>
		<category><![CDATA[forms]]></category>
		<category><![CDATA[payments]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[rspec]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[signature calculation]]></category>
		<category><![CDATA[soap]]></category>

		<guid isPermaLink="false">http://techblog.floorplanner.com/?p=726</guid>
		<description><![CDATA[Michel and I have been playing around with integrating Adyen payment services in Rails applications. We have assembled some of the pieces of code we have written, combined them, written specs for them and released the result as a gem. The package is also included on the Adyen support site.
Currently, the gem provides the following:

Simple [...]]]></description>
			<content:encoded><![CDATA[<p>Michel and I have been playing around with integrating <a href="http://www.adyen.com/">Adyen payment services</a> in Rails applications. We have assembled some of the pieces of code we have written, combined them, written specs for them and <a href="http://github.com/wvanbergen/adyen">released the result as a gem</a>. The package is also included on the <a href="https://support.adyen.com/index.php?_m=downloads&amp;_a=viewdownload&amp;downloaditemid=43">Adyen support site</a>.</p>
<p>Currently, the gem provides the following:</p>
<ul>
<li>Simple configuration and setup.</li>
<li>Uses Adyen&#8217;s test or production environment based on your Rails environment.</li>
<li>Generating hidden form fields for redirecting to Adyen for a payment.</li>
<li>Calculating the signature to sign these redirects.</li>
<li>Checking Adyen&#8217;s signature when the user gets redirected back.</li>
<li>Matchers to easily test your payment forms using RSpec.</li>
<li>Receiving and storing notifications from Adyen.</li>
<li>Calling the Adyen SOAP services (requires the <a href="http://github.com/troelskn/handsoap">Handsoap gem</a>).</li>
</ul>
<p>Currently, not all SOAP services are implemented (because we didn&#8217;t need them all). It should be quite easy to implement them as well based on the other services that are implemented already. Don&#8217;t hesitate to submit patches!</p>
]]></content:encoded>
			<wfw:commentRss>http://techblog.floorplanner.com/2009/09/27/adyen-payment-services-for-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>New version of Scoped search</title>
		<link>http://techblog.floorplanner.com/2009/08/31/new-version-of-scoped-search/</link>
		<comments>http://techblog.floorplanner.com/2009/08/31/new-version-of-scoped-search/#comments</comments>
		<pubDate>Mon, 31 Aug 2009 09:37:22 +0000</pubDate>
		<dc:creator>Willem van Bergen</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[LIKE]]></category>
		<category><![CDATA[query language]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[scoped_search]]></category>
		<category><![CDATA[search]]></category>

		<guid isPermaLink="false">http://techblog.floorplanner.com/?p=698</guid>
		<description><![CDATA[After an almost complete rewrite, I am proud to present version 2.0 of scoped_search, the ActiveRecord plugin that makes it easy to find records using a simple query language. This new version support a new query language that supports more complex constructs, and can therefore be used to conduct more fine-grained queries on your models.
New [...]]]></description>
			<content:encoded><![CDATA[<p>After an almost complete rewrite, I am proud to present version 2.0 of <a href="http://github.com/wvanbergen/scoped_search">scoped_search</a>, the ActiveRecord plugin that <a href="http://techblog.floorplanner.com/2008/07/26/easy-search-with-activerecord/">makes it easy to find records using a simple query language</a>. This new version support a new query language that supports more complex constructs, and can therefore be used to conduct more fine-grained queries on your models.</p>
<h3>New query language</h3>
<ul>
<li>Logical operators: AND (&amp;, &amp;&amp;), OR (|, ||) and NOT (!, -) operators, and parentheses to structure the boolean logic: <code>police AND (car || uniform), -"village people"</code>. By default, the AND operator is used to combine different segments of your query.</li>
<li>Comparison operators: the most common comparison operators are supported, and to what you expect on integer and date field.</li>
<li>Explicit field support: only search in the specified field instead of all fields: <code>age &gt;= 21</code>, <code>created &lt; 2009-01-01</code>, <code>username != "root"</code>.</li>
<li>Check for NULL fields: <code>null? parent</code>, <code>set? error_message</code></li>
<li>Commas are supported to separate the different parts of the query.</li>
</ul>
<p>More information about <a href="http://wiki.github.com/wvanbergen/scoped_search/query-language">the query language</a> can be found in the project wiki on GitHub.</p>
<h3>New definition syntax</h3>
<p>The new version supports a new syntax to define what fields of your model can be searched and in what cases. An example:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family: Monaco,monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> User <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>   
  belongs_to <span style="color:#ff3333; font-weight:bold;">:account_type</span>
&nbsp;
  scoped_search <span style="color:#ff3333; font-weight:bold;">:on</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:first_name</span>, <span style="color:#ff3333; font-weight:bold;">:last_name</span><span style="color:#006600; font-weight:bold;">&#93;</span>
  scoped_search <span style="color:#ff3333; font-weight:bold;">:on</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:created_at</span>, <span style="color:#ff3333; font-weight:bold;">:alias</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:created</span>, <span style="color:#ff3333; font-weight:bold;">:only_explicit</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span>
  scoped_search <span style="color:#ff3333; font-weight:bold;">:in</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:account_type</span>, <span style="color:#ff3333; font-weight:bold;">:on</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:name</span>, <span style="color:#ff3333; font-weight:bold;">:description</span><span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>After the fields have been defined, the <code>search_for</code> method can be used to search your models using a named scope, just like it was before. The <a href="http://wiki.github.com/wvanbergen/scoped_search/search-definition">project wiki</a> has more information about this new syntax. The search syntax itself hasn&#8217;t changed:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family: Monaco,monospace;"><span style="color:#0066ff; font-weight:bold;">@users</span> = User.<span style="color:#9900CC;">search_for</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:q</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">paginate</span><span style="color:#006600; font-weight:bold;">&#40;</span>page <span style="color:#006600; font-weight:bold;">=&gt;</span> params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:page</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<h3>Installation or upgrade</h3>
<p>Include the gem in your <code>environment.rb</code> configuration and run <code>rake gems:install</code> to install it:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family: Monaco,monospace;">config.<span style="color:#9900CC;">gem</span> <span style="color:#996600;">'scoped_search'</span>, <span style="color:#ff3333; font-weight:bold;">:source</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'http://gemcutter.org'</span></pre></div></div>

<h3>Backwards compatibility</h3>
<p>The new version has a new syntax to define the fields that can be searched with a query. This new syntax gives you more fine-grained control over the queries that will be generated, so I urge you to adopt this new syntax. However, the old <code>searchable_on</code> syntax is still available for backwards compatibility.</p>
<p>Please contact me if you have any issues with the new version.</p>
<p><small>(Updated with new gemcutter installation instructions.)</small></p>
]]></content:encoded>
			<wfw:commentRss>http://techblog.floorplanner.com/2009/08/31/new-version-of-scoped-search/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Request-log-analyzer 1.0</title>
		<link>http://techblog.floorplanner.com/2009/01/12/request-log-analyzer-10/</link>
		<comments>http://techblog.floorplanner.com/2009/01/12/request-log-analyzer-10/#comments</comments>
		<pubDate>Mon, 12 Jan 2009 13:17:38 +0000</pubDate>
		<dc:creator>Willem van Bergen</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[merb]]></category>
		<category><![CDATA[production.log]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[rails-log-analyzer]]></category>
		<category><![CDATA[reporting]]></category>
		<category><![CDATA[request-log-analyzer]]></category>
		<category><![CDATA[requests]]></category>
		<category><![CDATA[statistics]]></category>

		<guid isPermaLink="false">http://techblog.floorplanner.com/?p=409</guid>
		<description><![CDATA[After a complete rewrite, Bart and I are proud to present request-log-analyzer version 1.0! Request-log-analyzer is an open-source command-line tool to analyze production log files from your Rails application to produce a performance report.
What&#8217;s new?

More robust log parser. It parses more lines and it now combines all lines that belong to the same request, which greatly [...]]]></description>
			<content:encoded><![CDATA[<p>After a complete rewrite, <a title="Bart's Moves on Rails website" href="http://www.movesonrails.com">Bart</a> and I are proud to present <strong>request-log-analyzer</strong> version <strong>1.0!</strong> Request-log-analyzer is an open-source command-line tool to analyze production log files from your Rails application to produce a performance report.</p>
<h3>What&#8217;s new?</h3>
<ul>
<li>More robust log parser. It parses more lines and it now combines all lines that belong to the same request, which greatly improves the amount of information available. </li>
<li>It produces more detailed and more beautiful <a title="Sample report" href="http://wiki.github.com/wvanbergen/request-log-analyzer/sample-output">reports</a>. </li>
<li>A <a href="http://wiki.github.com/wvanbergen/request-log-analyzer/creating-a-request-database">database builder</a> is included, which will create an SQLite 3 database with all parsed request information, so you can roll your own queries.</li>
<li>Request filtering options, so you can exclude irrelevant data. <a title="In-depth analysis example" href="http://wiki.github.com/wvanbergen/request-log-analyzer/in-depth-analysis">An example</a> on how this can be applied in practice can be found in the wiki. </li>
<li>Better, more modularized design under the hood. The parser is now fully log file format-agnostic. Developing extensions and modifications, or adding support for other log file formats should be much easier now. See the <a title="Request-log-analyzer development" href="http://wiki.github.com/wvanbergen/request-log-analyzer/development">development</a>-page for some pointers.</li>
<li>Documentation in the <a href="http://wiki.github.com/wvanbergen/request-log-analyzer">project&#8217;s wiki</a>. Hopefully, this helps people get up to speed with the new version and answers most questions about using the tool. If you still have questions, please contact us so we can keep improving it!</li>
</ul>
<h3>Installation</h3>
<p>Install or upgrade to the new version with the following command:</p>
<pre>$ sudo gem install wvanbergen-request-log-analyzer 
                --source http://gems.github.com</pre>
<p>To get the best results out of request-log-analyzer, it is important to configure logging correctly for your application. Some pointers on how to set things up correctly <a title="Configuring logging correctly" href="http://wiki.github.com/wvanbergen/request-log-analyzer/configure-logging">can be found in the wiki</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://techblog.floorplanner.com/2009/01/12/request-log-analyzer-10/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Rails and Merb merge!</title>
		<link>http://techblog.floorplanner.com/2008/12/24/rails-and-merb-merge/</link>
		<comments>http://techblog.floorplanner.com/2008/12/24/rails-and-merb-merge/#comments</comments>
		<pubDate>Wed, 24 Dec 2008 02:39:15 +0000</pubDate>
		<dc:creator>Willem van Bergen</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[awesomeness]]></category>
		<category><![CDATA[bloat]]></category>
		<category><![CDATA[community]]></category>
		<category><![CDATA[conflicts]]></category>
		<category><![CDATA[git]]></category>
		<category><![CDATA[merb]]></category>
		<category><![CDATA[merge]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://techblog.floorplanner.com/?p=398</guid>
		<description><![CDATA[Good luck to the merged Rails team and hopefully Rails 3 will kick ass! Let&#8217;s hope git will really deliver on this gig!  Try to refrain from using git blame too much when resolving merge conflicts.  

]]></description>
			<content:encoded><![CDATA[<p>Good luck to the merged Rails team and hopefully Rails 3 will kick ass! Let&#8217;s hope <strong>git</strong> will really deliver on this gig!  Try to refrain from using <code>git blame</code> too much when resolving merge conflicts. <img src='http://techblog.floorplanner.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p><a title="git checkout rails &amp;&amp; git merge merb" href="http://rubyonrails.org/merb"><img class="alignnone size-full wp-image-400" title="git checkout rails &amp;&amp; git merge merb" src="http://techblog.floorplanner.com/wp-content/uploads/2008/12/picture-22.png" alt="git checkout rails &amp;&amp; git merge merb" width="588" height="471" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://techblog.floorplanner.com/2008/12/24/rails-and-merb-merge/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
