<?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>Jan Varwig &#187; rails</title>
	<atom:link href="http://jan.varwig.org/archive/tag/rails/feed" rel="self" type="application/rss+xml" />
	<link>http://jan.varwig.org</link>
	<description>Somewhere between Hello World and HAL9000</description>
	<lastBuildDate>Wed, 17 Mar 2010 11:12:04 +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>REST in Place now with Multiline Editor</title>
		<link>http://jan.varwig.org/archive/rest-in-place-now-with-multiline-editor</link>
		<comments>http://jan.varwig.org/archive/rest-in-place-now-with-multiline-editor#comments</comments>
		<pubDate>Fri, 05 Feb 2010 18:00:36 +0000</pubDate>
		<dc:creator>Jan</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[rest]]></category>
		<category><![CDATA[rest in place]]></category>
		<category><![CDATA[rubyonrails]]></category>

		<guid isPermaLink="false">http://jan.varwig.org/?p=247</guid>
		<description><![CDATA[Yesterday I pushed the changes introducing the Multiline editor to REST in Place I announced last week.

The definition of forms for editing inline-content is now separated from the Code that deals with Ajax and editing state.
This makes it very easy to extend REST in Place with your own Editors.

I also decided to keep the jQuery [...]]]></description>
			<content:encoded><![CDATA[<p>Yesterday I pushed the changes introducing the Multiline editor to REST in Place I announced last week.</p>

<p>The definition of forms for editing inline-content is now separated from the Code that deals with Ajax and editing state.
This makes it very easy to extend REST in Place with your own Editors.</p>

<p>I also decided to keep the jQuery backwards compatibility for a bit longer.</p>

<p>Check out the changes at <a href="http://github.com/janv/rest_in_place.git">http://github.com/janv/rest_in_place.git</a></p>
]]></content:encoded>
			<wfw:commentRss>http://jan.varwig.org/archive/rest-in-place-now-with-multiline-editor/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rails 3 and Mongrel</title>
		<link>http://jan.varwig.org/archive/rails-3-and-mongrel</link>
		<comments>http://jan.varwig.org/archive/rails-3-and-mongrel#comments</comments>
		<pubDate>Fri, 05 Feb 2010 16:12:18 +0000</pubDate>
		<dc:creator>Jan</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[bundler]]></category>
		<category><![CDATA[Gemfile]]></category>
		<category><![CDATA[mongrel]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[rails3]]></category>

		<guid isPermaLink="false">http://jan.varwig.org/?p=240</guid>
		<description><![CDATA[Today, the Rails team finally released the first beta of Rails 3.
Trying to play around with it, I experienced a very annoying problem with mongrel:

Per default, Rails seems to only work with the gems listed in its Gemfile, which means that it starts using webrick.
I wanted it to use mongrel, like Rails 2 does and [...]]]></description>
			<content:encoded><![CDATA[<p>Today, the Rails team finally released the first beta of <a href="http://weblog.rubyonrails.org/2010/2/5/rails-3-0-beta-release">Rails 3</a>.
Trying to play around with it, I experienced a very annoying problem with mongrel:</p>

<p>Per default, Rails seems to only work with the gems listed in its Gemfile, which means that it starts using webrick.
I wanted it to use mongrel, like Rails 2 does and added <code>gem "mongrel"</code> to my gemfile which resulted in <code>rails server</code> just freezing.
The cuplrit was some of the weirdest piece of code I&#8217;ve ever seen, mongrel&#8217;s <code>Mongrel::Gems.require</code> function:</p>

<div class="dean_ch" style="white-space: wrap;"><br />
<span class="kw1">def</span> <span class="kw3">require</span><span class="br0">&#40;</span>library, version = <span class="kw2">nil</span><span class="br0">&#41;</span><br />
&nbsp; <span class="kw1">begin</span><br />
&nbsp; &nbsp; <span class="kw4">Kernel</span>.<span class="kw3">require</span> library<br />
&nbsp; <span class="kw1">rescue</span> <span class="kw4">LoadError</span>, <span class="kw4">RuntimeError</span> =&gt; e<br />
&nbsp; &nbsp; <span class="kw1">begin</span> <br />
&nbsp; &nbsp; &nbsp; <span class="co1"># ActiveSupport breaks &#8216;require&#8217; by making it always return a true value</span><br />
&nbsp; &nbsp; &nbsp; <span class="kw4">Kernel</span>.<span class="kw3">require</span> <span class="st0">&#8216;rubygems&#8217;</span><br />
&nbsp; &nbsp; &nbsp; version ? gem<span class="br0">&#40;</span>library, version<span class="br0">&#41;</span> : gem<span class="br0">&#40;</span>library<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span class="kw1">retry</span><br />
&nbsp; &nbsp; <span class="kw1">rescue</span> <span class="re2">Gem::LoadError</span>, <span class="kw4">LoadError</span>, <span class="kw4">RuntimeError</span><br />
&nbsp; &nbsp; &nbsp; <span class="co1"># puts &quot;** #{library.inspect} could not be loaded&quot; unless library == &quot;mongrel_experimental&quot;</span><br />
&nbsp; &nbsp; <span class="kw1">end</span><br />
&nbsp; <span class="kw1">end</span> &nbsp;<br />
<span class="kw1">end</span><br />
&nbsp;</div>

<p>An infinite loop with the only way out being an exception that never got thrown!</p>

<p>This madness was called from mongrel.rb:</p>

<div class="dean_ch" style="white-space: wrap;"><br />
<span class="re0">$LOAD_PATH</span>.<span class="me1">unshift</span> <span class="st0">&#8216;projects/mongrel_experimental/lib/&#8217;</span><br />
<span class="re2">Mongrel::Gems</span>.<span class="kw3">require</span> <span class="st0">&#8216;mongrel_experimental&#8217;</span>, <span class="st0">&quot;&gt;=#{Mongrel::Const::MONGREL_VERSION}&quot;</span><br />
&nbsp;</div>

<p>Removing these two lines solved the issue. That would have meant patching my gems however, and that smells like something you should not do. The final solution was simple and posted in the comments of the Rails 3 Beta announcement by  Juanma Cervera. I just needed to add 3 more lines to my Gemfile:</p>

<div class="dean_ch" style="white-space: wrap;"><br />
gem <span class="st0">&quot;mongrel&quot;</span><br />
gem <span class="st0">&quot;cgi_multipart_eof_fix&quot;</span><br />
gem <span class="st0">&quot;fastthread&quot;</span><br />
gem <span class="st0">&quot;mongrel_experimental&quot;</span><br />
&nbsp;</div>
]]></content:encoded>
			<wfw:commentRss>http://jan.varwig.org/archive/rails-3-and-mongrel/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>REST in Place: Major update ahead</title>
		<link>http://jan.varwig.org/archive/rest-in-place-major-update-ahead</link>
		<comments>http://jan.varwig.org/archive/rest-in-place-major-update-ahead#comments</comments>
		<pubDate>Mon, 25 Jan 2010 00:08:44 +0000</pubDate>
		<dc:creator>Jan</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[inplace editor]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[rest]]></category>
		<category><![CDATA[rest in place]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[rubyonrails]]></category>

		<guid isPermaLink="false">http://jan.varwig.org/?p=237</guid>
		<description><![CDATA[This week, I&#8217;ll roll out a major update to REST in Place. I have completely restructured the code and made the Plugin much more object-oriented, modular and maintainable. Existing installations should be able to upgrade without a hitch. The most important change for users will be the jQuery 1.4 support and support for different editors [...]]]></description>
			<content:encoded><![CDATA[<p>This week, I&#8217;ll roll out a major update to <a href="http://github.com/janv/rest_in_place/">REST in Place</a>. I have completely restructured the code and made the Plugin much more object-oriented, modular and maintainable. Existing installations should be able to upgrade without a hitch. The most important change for users will be the jQuery 1.4 support and support for different editors (such as textarea, checkbox, etc.).</p>

<p>In November I got some pull requests for REST in Place which I didn&#8217;t get to really have a look at until last week. I haven&#8217;t really touched REST in Place in ages and wasn&#8217;t really aware that it was that popular. It has 72 followers and 11 forks on github and who knows how many other people are just silently using it! So, I merged in the contributions and dealt with some issues in the tracker and thought about how to go on.</p>

<p>REST in Place originally was a mere proof of concept and one of my first projects in JavaScript and jQuery. I have since written a lot more complicated applications and even a diploma thesis on JavaScript and the old code just isn&#8217;t up to my standards anymore. Additionally I have received several pull requests for textarea support which all couldn&#8217;t be merged because they were just copypasted and modified a little from the input-tag version. The only solution was a complete rewrite and that&#8217;s what I did. While the plugin still works the same way, the code is properly split up and more modular now. This enables easy extension and maintenance from now on and hopefully quicker development from me and better patches from all users.</p>

<p>This new development has some drawbacks too, unfortunately. This massive improvement will only go to the jQuery version of REST in Place. Since I don&#8217;t know much about Prototype or mootools, I can&#8217;t support both versions anymore. If someone is willing to work with me on this, I&#8217;ll happily accept their contributions but I can&#8217;t do this on my own. If anyone is willing to do so, please reply in the comments, so we can work out a plan. I want to keep all three versions as similar as possible, this means the Prototype and mootools versions should follow the same object-oriented structure as the jQuery version.</p>

<p>One last thing: I will support jQuery &lt; 1.4 until 1.4.1 comes out. 1.4 introduced some very nice changes which I&#8217;d like to make use of but since 1.4 seems a little buggy still, I&#8217;ll continue to support the older versions for a little while.</p>
]]></content:encoded>
			<wfw:commentRss>http://jan.varwig.org/archive/rest-in-place-major-update-ahead/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Dbserialize update</title>
		<link>http://jan.varwig.org/archive/dbserialize-update</link>
		<comments>http://jan.varwig.org/archive/dbserialize-update#comments</comments>
		<pubDate>Wed, 21 Jan 2009 20:03:52 +0000</pubDate>
		<dc:creator>Jan</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[dbserialize]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://jan.varwig.org/?p=100</guid>
		<description><![CDATA[I&#8217;ve given Sebastian Deutsch write access to my dbserialize repo on github and a few days ago he already made some minor changes, mainly improving the documentation and making the interface more consistent.

Dbserialize provides two rake tasks to store your current environments database tables to Yaml fixtures in db/fixtures and to fill the database with [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve given <a href="http://blog.9elements.com/">Sebastian Deutsch</a> write access to my dbserialize repo on github and a few days ago he already made some minor changes, mainly improving the documentation and making the interface more consistent.</p>

<p>Dbserialize provides two rake tasks to store your current environments database tables to Yaml fixtures in db/fixtures and to fill the database with these fixtures.</p>

<p>That way it’s possible to store sample data consistently in the repository and for all developers on a project to use common sample data instead of manually generating something on every developer machine or sending database dumps around via email.</p>

<p>Check out the <a href="http://blog.9elements.com/?p=89">announcement here</a>, the <a href="http://blog.9elements.com/?page_id=76">project page here</a>, and the <a href="http://github.com/janv/dbserialize/tree/master">repository on github</a></p>
]]></content:encoded>
			<wfw:commentRss>http://jan.varwig.org/archive/dbserialize-update/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>REST in Place now on Github</title>
		<link>http://jan.varwig.org/archive/rest-in-place-now-on-github</link>
		<comments>http://jan.varwig.org/archive/rest-in-place-now-on-github#comments</comments>
		<pubDate>Sat, 20 Sep 2008 21:34:10 +0000</pubDate>
		<dc:creator>Jan</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[on Rails]]></category>
		<category><![CDATA[git]]></category>
		<category><![CDATA[github]]></category>
		<category><![CDATA[mercurial]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[rest in place]]></category>

		<guid isPermaLink="false">http://jan.varwig.org/?p=90</guid>
		<description><![CDATA[After using Mercurial for 7 months we at 9elements have finally given in to the internet peer pressure und switched to git (Well, to be honest, several shortcomings in Mercurial played an important role too). Since then I&#8217;ve become accustomed to git and today ported over REST in Place from Subversion to Github.
The Github project [...]]]></description>
			<content:encoded><![CDATA[<p>After using <a href="http://selenic.com/mercurial/">Mercurial</a> for 7 months we at 9elements have finally given in to the internet peer pressure und switched to <a href="http://git.or.cz/">git</a> (Well, to be honest, several shortcomings in Mercurial played an important role too). Since then I&#8217;ve become accustomed to git and today ported over <a href="http://jan.varwig.org/projects/rest-in-place">REST in Place</a> from Subversion to Github.<br />
The Github project page ist located at <a href="http://github.com/janv/rest_in_place/">http://github.com/janv/rest_in_place/</a>, the repository can be found at <code>git://github.com/janv/rest_in_place.git</code>.</p>

<p>I&#8217;ve updated the README and the <a href="http://jan.varwig.org/projects/rest-in-place">project page</a> with the new information.</p>

<p>I&#8217;ve also published my <a href="http://github.com/janv/dbserialize/">dbserialize plugin at Github</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://jan.varwig.org/archive/rest-in-place-now-on-github/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>REST in Place now Rails 2.0 compatible</title>
		<link>http://jan.varwig.org/archive/rest-in-place-now-rails-20-compatible</link>
		<comments>http://jan.varwig.org/archive/rest-in-place-now-rails-20-compatible#comments</comments>
		<pubDate>Fri, 27 Jun 2008 17:41:27 +0000</pubDate>
		<dc:creator>Jan</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[on Rails]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[rest]]></category>
		<category><![CDATA[rest in place]]></category>
		<category><![CDATA[rubyonrails]]></category>

		<guid isPermaLink="false">http://jan.varwig.org/?p=86</guid>
		<description><![CDATA[Ever since Rails 2.0 came out, my REST in Place Plugins stopped working because of the request forgery protecting introduced into ActiveController. The AJAX request didn&#8217;t submit the rails authenticity token and the requests weren&#8217;t answered.

I fixed that problem yesterday in both the jQuery and the Prototype version (both included in the plugin.)

Check it out [...]]]></description>
			<content:encoded><![CDATA[<p>Ever since Rails 2.0 came out, my REST in Place Plugins stopped working because of the request forgery protecting introduced into ActiveController. The AJAX request didn&#8217;t submit the rails authenticity token and the requests weren&#8217;t answered.</p>

<p>I fixed that problem yesterday in both the <a href="http://www.jquery.com/">jQuery</a> and the <a href="http://www.prototypejs.org/">Prototype</a> version (both included in the plugin.)</p>

<p>Check it out on the <a href="/projects/rest-in-place">REST in Place</a> project page.</p>
]]></content:encoded>
			<wfw:commentRss>http://jan.varwig.org/archive/rest-in-place-now-rails-20-compatible/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Why bloggers suck</title>
		<link>http://jan.varwig.org/archive/why-bloggers-suck</link>
		<comments>http://jan.varwig.org/archive/why-bloggers-suck#comments</comments>
		<pubDate>Mon, 09 Jun 2008 19:10:04 +0000</pubDate>
		<dc:creator>Jan</dc:creator>
				<category><![CDATA[Software]]></category>
		<category><![CDATA[activerecord]]></category>
		<category><![CDATA[blogging]]></category>
		<category><![CDATA[orm]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ror]]></category>
		<category><![CDATA[rubyonrails]]></category>

		<guid isPermaLink="false">http://jan.varwig.org/?p=84</guid>
		<description><![CDATA[Today a post by Reg Braithwaite brought to my attention several blogposts about ActiveRecords alledged shortcomings.
Most of their writers are posting rants rants about some ideas they have about ActiveRecord without ever having really used it, it seems. The main issues seem to be


Domain-code in your controllers instead of your models
Rails choice of inheritance (AR [...]]]></description>
			<content:encoded><![CDATA[<p>Today a <a href="http://weblog.raganwald.com/2008/06/bitchiness-also-sucks.html">post by Reg Braithwaite</a> brought to my attention <a href="http://nothinghappens.net/?p=324">several</a> <a href="http://kore-nordmann.de/blog/why_active_record_sucks.html">blogposts</a> <a href="http://blog.mikeseth.com/index.php?/archives/4-ActiveRecord-sucks,-but-Kore-Nordmann-is-wrong.html">about ActiveRecords</a> <a href="http://karwin.blogspot.com/2008/05/activerecord-does-not-suck.html">alledged shortcomings</a>.
Most of their writers are posting rants rants about some ideas they have about ActiveRecord without ever having really used it, it seems. The main issues seem to be</p>

<ul>
<li>Domain-code in your controllers instead of your models</li>
<li>Rails choice of inheritance (AR as a base class for models)  for implementing AR</li>
</ul>

<p>Putting domain code inside your controllers is a programmer problem, not a framework problem.
I won&#8217;t even elaborate this any further.</p>

<p>The other issue is trickier. Not because of technical issues but because there&#8217;s a problem with the perception of Rails ActiveRecord. I first didn&#8217;t even want to waste my time to write this rant but then I started a comment on one of the postings that turned longer and longer until I decided to make a post of it.</p>

<p>Bill Karwin wrote some observations that I can mostly agree with.
There&#8217;s one exception though: I don&#8217;t see any problem with AR being implemented as a base class for your models.</p>

<p>One perceived problem is a lack of separation of concerns, the idea that you should separate model logic and database logic and that ActiveRecords fails here.</p>

<p>But one must look beyond the <em>separation-of-concerns</em> dogma here and ask what motivates this principle. Mixed concerns lead to confusion, confusions leads to errors and redundance (and thus to more errors). So, the motivation is to maintain a clear view over your methods and to reduce redundance.</p>

<p>Both goals are maintained by Rails although the models contain a mix of model- and database logic. Because most things can be expressed <em>very</em> concisely in Ruby, one line or even no code at all (using the powers of reflection) is enough to achieve what you want. If all your model-functionality and ORM-functionality  fit within one page on your screen, the benefit of splitting both into separate modules and/or files becomes too low to justify the cost.</p>

<p>The second problem with inheriting your models from AR::Base is supposedly the tight coupling between your model and your DB-access layer, requiring you to have a database ready for unit testing. If you&#8217;ve ever written tests for Rails, you know this is bullshit. Testing is a non-issue and the support for the testing database so seamless that I can&#8217;t imagine it being any more simple. One rake task sets up your testing database after a migration, the rest is handled automatically.</p>

<p>Pretending that relying on AR so much for your models is still bad somehow because you might want to change your record-store a some point in the lifetime of your application is nothing but a bunch of bullshit. NO solution EVER will enable you to do THAT in a way that&#8217;s so seamless that you&#8217;d want to. We&#8217;re talking about software here, not lightbulbs that you just screw in. Abstractions are always leaky and talking as if they were not is foolish.</p>

<p>The whole point is that Rails set out to to one thing well, and that is to write web apps in a certain way that has proven to be quite productive and covering the needs of most projects pretty well. You get a lot of comfort following that way but you pay a price if you don&#8217;t, namely losing that comfort. People are demanding stuff that Rails never promised to deliver. About their motivations I can only speculate. Maybe they&#8217;re just clueless, maybe they just want to make some noise. In any way, they&#8217;re hell of a reason to <a href="http://gilesbowkett.blogspot.com/2008/05/summon-monsters-open-door-heal-or-die.html">stay away from Digg and Reddit</a> and stick to some bloggers you trust with not wasting your time.</p>
]]></content:encoded>
			<wfw:commentRss>http://jan.varwig.org/archive/why-bloggers-suck/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rails: Determine the association collection size through joins</title>
		<link>http://jan.varwig.org/archive/rails-association-collection-size-though-joins</link>
		<comments>http://jan.varwig.org/archive/rails-association-collection-size-though-joins#comments</comments>
		<pubDate>Sun, 01 Jun 2008 15:25:21 +0000</pubDate>
		<dc:creator>Jan</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[on Rails]]></category>
		<category><![CDATA[activerecord]]></category>
		<category><![CDATA[associations]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[joins]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ror]]></category>
		<category><![CDATA[rubyonrails]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://jan.varwig.org/?p=82</guid>
		<description><![CDATA[Rails association collections know two ways to determine their size:


Through a dumb count as soon as you query the collection for its size.
This doesn&#8217;t require any additional work but puts quite a load on the database as soon as you have a large list of objects and want to know the size of an associated [...]]]></description>
			<content:encoded><![CDATA[<p>Rails association collections know two ways to determine their size:</p>

<ol>
<li><strong>Through a dumb count as soon as you query the collection for its size.</strong><br />
This doesn&#8217;t require any additional work but puts quite a load on the database as soon as you have a large list of objects and want to know the size of an associated collection for each object. For 100 posts with comments, this approach would query the database 100 times with <code>SELECT count(*) AS count_all FROM</code>comments<code>WHERE (comments.post_id = 1234)</code>. This might be okay for single objects which are queried from time to time but not for collections that need to be displayed frequently.</li>
<li><strong>Through counter caches that are a bit of a pain in the ass to set up correctly and gave me the fishy impression that they are easily corrupted.</strong>
Now, I admit that I didn&#8217;t spend too long investigating the implementation of the counter cache but after fiddling around with it for an hour before I finally found out how to properly initialize the cache in my migration and after discovering that the cache can only be changed relative to its current content, I left with a bad feeling.<br />
All this hassle is absolutely unavoidable if you need maximum performance. In this article I&#8217;ll use blogposts and comments as an example, though only because this is familiar to many people. A real blog with a bunch of posts on its index page, receiving any considerable amount of hits per day is a bad candidate for the trick I describe here.</li>
</ol>

<p>In raw SQL, if you want to find out the amount of associated rows in a different table, you use a <code>JOIN</code>, combined with <code>COUNT()</code> and <code>GROUP BY</code>, like this:</p>

<div class="dean_ch" style="white-space: wrap;"><span class="kw1">SELECT</span> posts.*, COUNT<span class="br0">&#40;</span>comments.id<span class="br0">&#41;</span> <span class="kw1">AS</span> comments_count<br />
<span class="kw1">FROM</span> posts<br />
<span class="kw1">LEFT</span> <span class="kw1">OUTER</span> <span class="kw1">JOIN</span> comments <span class="kw1">ON</span> posts.id = comments.post_id<br />
<span class="kw1">GROUP</span> <span class="kw1">BY</span> posts.id</div>

<p>Admittedly, this is slower than a counter cache but is not as difficult to set up, doesn&#8217;t risk errors due to a corrupt cache and if you chose your database keys wisely it is a pretty nice compromise.
When you ask an association collection for its <code>size()</code>, rails checks for the presence of a counter cache attribute in the current ActiveRecord object. If it finds one, it uses its content to return the collection size, otherwise the database is queried.</p>

<p>The catch is now, that in the above example, we inserted a perfectly valid counter cache column into our posts without specifying caching in the association declaration in the class. ActiveRecord inserts that columns content into a <code>comments_count</code> attribute in our Posts but since it doesn&#8217;t know exactly what to do with it, it doesn&#8217;t cast it into an integer but leaves it in there as a String. That makes <code>size()</code>, or to be more precise, the <code>count_records()</code> method trip with a &#8220;String can&#8217;t be coerced into Fixnum&#8221; error.</p>

<p>To fix this, I wrote an extension for the has_many Association:</p>

<div class="dean_ch" style="white-space: wrap;"><span class="kw1">module</span> OptionalJoinCounter<br />
&nbsp; <span class="kw1">def</span> count_records<br />
&nbsp; &nbsp; count = <span class="kw1">if</span> has_cached_counter?<br />
&nbsp; &nbsp; &nbsp; <span class="re1">@owner</span>.<span class="me1">send</span><span class="br0">&#40;</span><span class="re3">:read_attribute</span>, cached_counter_attribute_name<span class="br0">&#41;</span>.<span class="me1">to_i</span><br />
&nbsp; &nbsp; <span class="kw1">elsif</span> <span class="re1">@reflection</span>.<span class="me1">options</span><span class="br0">&#91;</span><span class="re3">:counter_sql</span><span class="br0">&#93;</span><br />
&nbsp; &nbsp; &nbsp; <span class="re1">@reflection</span>.<span class="me1">klass</span>.<span class="me1">count_by_sql</span><span class="br0">&#40;</span>@counter_sql<span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="kw1">else</span><br />
&nbsp; &nbsp; &nbsp; <span class="re1">@reflection</span>.<span class="me1">klass</span>.<span class="me1">count</span><span class="br0">&#40;</span><span class="re3">:conditions</span> =&gt; <span class="re1">@counter_sql</span>, <span class="re3">:include</span> =&gt; <span class="re1">@reflection</span>.<span class="me1">options</span><span class="br0">&#91;</span><span class="re3">:include</span><span class="br0">&#93;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="kw1">end</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="re1">@target</span> = <span class="br0">&#91;</span><span class="br0">&#93;</span> <span class="kw1">and</span> loaded <span class="kw1">if</span> count == <span class="nu0">0</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="kw1">if</span> <span class="re1">@reflection</span>.<span class="me1">options</span><span class="br0">&#91;</span><span class="re3">:limit</span><span class="br0">&#93;</span><br />
&nbsp; &nbsp; &nbsp; count = <span class="br0">&#91;</span> <span class="re1">@reflection</span>.<span class="me1">options</span><span class="br0">&#91;</span><span class="re3">:limit</span><span class="br0">&#93;</span>, count <span class="br0">&#93;</span>.<span class="me1">min</span><br />
&nbsp; &nbsp; <span class="kw1">end</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="kw2">return</span> count<br />
&nbsp; <span class="kw1">end</span><br />
<span class="kw1">end</span></div>

<p>Uh, well, yeah, the only change here is the <code>.to_i</code> at the end of line 4 but hey, what did you expect?</p>

<p>Save that in <code>lib/optional_join_counter.rb</code> and extend your association with <code>has_many :whatevers, :extend =&gt; OptionalJoinCounter</code></p>

<p>Now, to get back to the example, imagine we want to use this trick to count our comments.
The above SQL can either be written by hand, or with Rails finder options:</p>

<div class="dean_ch" style="white-space: wrap;">Post.<span class="me1">find</span> <span class="re3">:all</span>,<br />
&nbsp; <span class="re3">:select</span> =&gt; <span class="st0">&#8216;posts.*, count(comments.id) as comments_count&#8217;</span>,<br />
&nbsp; <span class="re3">:joins</span> =&gt; <span class="st0">&#8216;LEFT JOIN comments ON posts.id = comments.post_id&#8217;</span>,<br />
&nbsp; <span class="re3">:group</span> =&gt; <span class="st0">&#8216;posts.id&#8217;</span></div>
]]></content:encoded>
			<wfw:commentRss>http://jan.varwig.org/archive/rails-association-collection-size-though-joins/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>REST in Place</title>
		<link>http://jan.varwig.org/archive/rest-in-place</link>
		<comments>http://jan.varwig.org/archive/rest-in-place#comments</comments>
		<pubDate>Sun, 23 Dec 2007 13:57:01 +0000</pubDate>
		<dc:creator>Jan</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[on Rails]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[inplace editor]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[prototype]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[rest]]></category>
		<category><![CDATA[ror]]></category>
		<category><![CDATA[rubyonrails]]></category>
		<category><![CDATA[web-2.0]]></category>

		<guid isPermaLink="false">http://jan.varwig.org/archiv/rest-in-place</guid>
		<description><![CDATA[In Ajax vs. REST I wrote about my ideas for a RESTful inplace editor.

Well, after a day playing around with jQuery, I present REST in Place
]]></description>
			<content:encoded><![CDATA[<p>In <a href="/archiv/ajax-vs-rest">Ajax vs. REST</a> I wrote about my ideas for a RESTful inplace editor.</p>

<p>Well, after a day playing around with jQuery, I present <a href="/projects/rest-in-place">REST in Place</a></p>
]]></content:encoded>
			<wfw:commentRss>http://jan.varwig.org/archive/rest-in-place/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>AJAX vs. REST</title>
		<link>http://jan.varwig.org/archive/ajax-vs-rest</link>
		<comments>http://jan.varwig.org/archive/ajax-vs-rest#comments</comments>
		<pubDate>Fri, 14 Dec 2007 23:30:09 +0000</pubDate>
		<dc:creator>Jan</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[on Rails]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[inplace editor]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[rest]]></category>

		<guid isPermaLink="false">http://jan.varwig.org/archiv/ajax-vs-rest</guid>
		<description><![CDATA[A small shop I&#8217;ve been writing for for fathers pharmacy was a welcome playground for modeling my domain RESTful.
During my ventures, two big issues had me thinking quite hard for a while and there&#8217;s still no proper solution available. One of them was a DRY implementation of controllers for nested resources which I&#8217;ll describe later, [...]]]></description>
			<content:encoded><![CDATA[<p>A small shop I&#8217;ve been writing for for fathers pharmacy was a welcome playground for modeling my domain RESTful.
During my ventures, two big issues had me thinking quite hard for a while and there&#8217;s still no proper solution available. One of them was a DRY implementation of controllers for nested resources which I&#8217;ll describe later, the other one was the outdated <a href="http://svn.rubyonrails.org/rails/plugins/in_place_editing/">inplace editor plugin</a> in Rails.</p>

<p><span id="more-70"></span>
<h3>In-Place editing RESTful resources</h3></p>

<p>Writing applications in <a href="http://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm">REST</a>-style encourages an even stricter separation of view and controller logic than the usual Rails&#8217; stack.
A key principle is that the client alone should provide the user means to manipulate and PUT/POST data to the server. This is easier said than done in current browsers and with the HTML format (which just wasn&#8217;t made for building GUIs, but that&#8217;s a topic for another post). AJAX is the closest we can come to this ideal, enabling a seamless transition from a plain view of a resource to a editable form without going through too much pain.</p>

<p>A widely used way to achieve these transitions are inplace editors, made popular by Flickr and supported through nice helper methods in rails. The probem now is that these helper methods were written around, or even before Rails 1.0 with not the slightest hint of REST in them. Planting <code>in_place_edit_for :object, :attribute</code> in your controller creates a <code>set_object_attribute</code>  action that finds an object by id, updates only <code>attribute</code> and renders <code>attribute</code>s new value as a plain, short string to the client. The client, in this case the XMLHttpRequest, takes that response and puts it in the appropriate place in the HTML document.</p>

<p>There are several problems with this approach:</p>

<ol>
<li>The generated actions are extremely simple and devoid of any form of sophisticated flow control (authorization, exception handling)</li>
<li>The generated actions perform a task that can be done in the good old <code>update</code> action and are thusly redundant.</li>
<li>Those new actions aren&#8217;t at all RESTful.</li>
<li>Ideally, no special server side code should be necessary.</li>
</ol>

<p>While problem 1 can be easily countered by implementing your own version of <code>in_place_edit_for</code>, the real point are the other issues. The <code>update</code> action already performs the task of updating one or more attributes and is equipped with whatever authorization checks are necessary (does object belong to the logged in user?). The <code>update</code> action corresponds to the REST-verb <code>POST</code> and doesn&#8217;t violate REST principles.</p>

<p>A solution that solves all these problems would have to look like this (client-side Javascript only):</p>

<ul>
<li>The inplace editor posts to the objects url (something like <code>/object/123</code>), passing the attribute to be updated as a HTTP parameter.</li>
<li>The server sends a response when the update is completed successfully or unsuccessfully.</li>
<li>In case of an unsuccessful update some form of status could be transferred in the response</li>
<li>In case of a successful update, the server doesn&#8217;t send the updated attributes value, instead the client should GET
the object requesting a <em>text/json</em> content-type, extract the desired attribute from the json and update the HTML with its value.</li>
</ul>

<p>I already tried building my own inplace editor Javascript helper but since much of the functionality of the rails inplace editor depends on the the editor defined in script.aculo.us I gave up. On the one hand the problem wasn&#8217;t so pressing that I could justify spending a weekend digging through the source, on the other hand I&#8217;d really like to see a good jQuery integration in rails and think maybe I could hit two birds with one stone if I try to go this direction one day.</p>
]]></content:encoded>
			<wfw:commentRss>http://jan.varwig.org/archive/ajax-vs-rest/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
