<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="http://feeds.feedburner.com/~d/styles/rss2full.xsl" type="text/xsl" media="screen"?><?xml-stylesheet href="http://feeds.feedburner.com/~d/styles/itemcontent.css" type="text/css" media="screen"?><!-- generator="wordpress/2.1.2" --><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">

<channel>
	<title>Ruby Fleebie</title>
	<link>http://www.rubyfleebie.com</link>
	<description>Because programming should be fun</description>
	<pubDate>Fri, 07 Nov 2008 06:51:03 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.1.2</generator>
	<language>en</language>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/rubyfleebie" type="application/rss+xml" /><feedburner:emailServiceId xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">798653</feedburner:emailServiceId><feedburner:feedburnerHostname xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">http://www.feedburner.com</feedburner:feedburnerHostname><item>
		<title>simplyglobal : A simple globalization plugin for Rails</title>
		<link>http://www.rubyfleebie.com/simplyglobal-a-simple-globalization-plugin-for-rails/</link>
		<comments>http://www.rubyfleebie.com/simplyglobal-a-simple-globalization-plugin-for-rails/#comments</comments>
		<pubDate>Thu, 30 Oct 2008 18:38:58 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
		
		<category><![CDATA[Easy reading]]></category>

		<guid isPermaLink="false">http://www.rubyfleebie.com/simplyglobal-a-simple-globalization-plugin-for-rails/</guid>
		<description><![CDATA[   simplyglobal : A simple globalization plugin for Rails
The following is a guest post from Dan Simard
Sometimes, you have to reinvent the wheel. It&#8217;s really sad to say and you will probably hate me for saying that (and I know that you&#8217;ll do because I hate myself for it). I&#8217;ve written a new [...]]]></description>
			<content:encoded><![CDATA[<h1>   simplyglobal : A simple globalization plugin for Rails</h1>
<p><strong>The following is a guest post from Dan Simard</strong></p>
<p>Sometimes, you have to reinvent the wheel. It&#8217;s really sad to say and you will probably hate me for saying that (and I know that you&#8217;ll do because I hate myself for it). I&#8217;ve written <strong>a new globalization plugin for Rails</strong>.</p>
<h3>   Why did I reinvented the wheel?</h3>
<p>I searched and tried a lot of <a href="http://agilewebdevelopment.com/plugins/search?search=global" title="other globalisation plugins">other globalisation plugins</a>&#8230; and I really tried them. I spent hours with <a href="http://agilewebdevelopment.com/plugins/globalize" title="Globalize">Globalize</a>. It was just too much. I tried the <a href="http://rubyforge.org/projects/gettextlocalize/" title="gettext_Localize">gettext_Localize</a> that works with the <a href="http://www.gnu.org/software/gettext/" title="Gettext">good ol&#8217; gettext</a> command. Fuck it. Too complicated. It just didn&#8217;t fit my needs at all.</p>
<p>All I wanted was a wheel that you can put a wood-pole in the middle and then it could start spinning. I made one.</p>
<p>You can go on the <a href="http://code.google.com/p/simplyglobal/" title="simplyglobal project homepage">simplyglobal project homepage</a> to learn on to <a href="http://code.google.com/p/simplyglobal/wiki/Install" title="install it">install it</a> and <a href="http://code.google.com/p/simplyglobal/wiki/How" title="use it">use it</a>.</p>
<p>In fact, this is not really a globalization plugin because there&#8217;s no localization handling or anything that can look like it. The name should have been <strong>simplytranslated</strong> but I already created the project with the name <em>simplyglobal</em> and it was an hassle to change it.</p>
<h3>       How to install</h3>
<p><strong>1.</strong> Execute <em>./script/plugin install <a href="http://simplyglobal.googlecode.com/svn/trunk/simplyglobal" rel="nofollow">http://simplyglobal.googlecode.com/svn/trunk/simplyglobal</a></em><br />
<strong>2.</strong> Create a file named <em>simplyglobal.rb</em> in the <em>config/initializers</em> directory<br />
<strong>3.</strong> In <em>simplyglobal.rb</em>, create hashes of language<br />
Add the language hashes to the objectYou will end up with a file named <em>simplyglobal.rb</em> that looks like this :</p>
<div class="ch_code_container" style="font-family: monospace;">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;"><span style="color:#7b0303; font-style:normal;">#français</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">fr = <span style="color:#3a27c9; font-weight:bold;">&#123;</span>    <span style="color:#996600;">&quot;hi&quot;</span> =&gt; <span style="color:#996600;">&quot;bonjour&quot;</span>,    <span style="color:#996600;">&quot;welcome&quot;</span> =&gt; <span style="color:#996600;">&quot;bienvenue&quot;</span>  <span style="color:#3a27c9; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;"><span style="color:#7b0303; font-style:normal;"># espanol</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">es = <span style="color:#3a27c9; font-weight:bold;">&#123;</span>    <span style="color:#996600;">&quot;hi&quot;</span> =&gt; <span style="color:#996600;">&quot;hola&quot;</span>,    <span style="color:#996600;">&quot;welcome&quot;</span> =&gt; <span style="color:#996600;">&quot;bienvenida&quot;</span><span style="color:#3a27c9; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">SimplyGlobal.<span style="color:#3a27c9;">add_language_hash</span><span style="color:#3a27c9; font-weight:bold;">&#40;</span>:fr, fr<span style="color:#3a27c9; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">SimplyGlobal.<span style="color:#3a27c9;">add_language_hash</span><span style="color:#3a27c9; font-weight:bold;">&#40;</span>:es, es<span style="color:#3a27c9; font-weight:bold;">&#41;</span></div>
</li>
</ol>
</div>
<p>In <strong>development</strong>, this file will be loaded every request. In <strong>production</strong>, it is loaded once.</p>
<h3>       How to use it with strings</h3>
<p>After you <a href="http://code.google.com/p/simplyglobal/wiki/Install" title="installed it">installed it</a>, you can use it in these various ways.</p>
<p>SimplyGlobal adds a <strong>t()</strong> method to all string objects that will return the translated string. Example, if you have defined a language hash that looks like this (<strong>note</strong> : normally, the languages hash are defined in <em>config/initializers/simplyglobal.rb</em> but I put it inline for the sake of the example) :</p>
<div class="ch_code_container" style="font-family: monospace;">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">fr = <span style="color:#3a27c9; font-weight:bold;">&#123;</span><span style="color:#996600;">&quot;hi&quot;</span> =&gt; <span style="color:#996600;">&quot;bonjour&quot;</span><span style="color:#3a27c9; font-weight:bold;">&#125;</span> <span style="color:#7b0303; font-style:normal;"># Create the language hash</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">SimplyGlobal.<span style="color:#3a27c9;">add_language_hash</span><span style="color:#3a27c9; font-weight:bold;">&#40;</span>:fr, fr<span style="color:#3a27c9; font-weight:bold;">&#41;</span> <span style="color:#7b0303; font-style:normal;"># Add the language hash to simplyglobal</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">SimplyGlobal.<span style="color:#3a27c9;">locale</span> = :fr <span style="color:#7b0303; font-style:normal;"># Assigns the locale to use</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;"><span style="color:#996600;">&quot;hi&quot;</span>.<span style="color:#3a27c9;">t</span> <span style="color:#7b0303; font-style:normal;"># returns &quot;bonjour&quot;</span></div>
</li>
</ol>
</div>
<p>As simple as that!</p>
<p>You can use it like the <a href="http://www.ruby-doc.org/core/classes/String.html#M000785" title="% method">% method</a> of the String class.</p>
<div class="ch_code_container" style="font-family: monospace;">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">fr = <span style="color:#3a27c9; font-weight:bold;">&#123;</span><span style="color:#996600;">&quot;hi %s%d&quot;</span> =&gt; <span style="color:#996600;">&quot;bonjour %s%d&quot;</span><span style="color:#3a27c9; font-weight:bold;">&#125;</span> <span style="color:#7b0303; font-style:normal;"># Create the language hash</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">SimplyGlobal.<span style="color:#3a27c9;">add_language_hash</span><span style="color:#3a27c9; font-weight:bold;">&#40;</span>:fr, fr<span style="color:#3a27c9; font-weight:bold;">&#41;</span> <span style="color:#7b0303; font-style:normal;"># Add the language hash to simplyglobal</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">SimplyGlobal.<span style="color:#3a27c9;">locale</span> = :fr <span style="color:#7b0303; font-style:normal;"># Assigns the locale to use</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;"><span style="color:#996600;">&quot;hi&quot;</span>.<span style="color:#3a27c9;">t</span><span style="color:#3a27c9; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Johnny&quot;</span>, <span style="color:#3a27c9;">5</span><span style="color:#3a27c9; font-weight:bold;">&#41;</span> <span style="color:#7b0303; font-style:normal;"># returns &quot;bonjour Johnny5&quot;</span></div>
</li>
</ol>
</div>
<p>You can also return all translations for a word. That is a special feature developed only for Frank.</p>
<div class="ch_code_container" style="font-family: monospace;">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">SimplyGlobal.<span style="color:#3a27c9;">add_language_hash</span><span style="color:#3a27c9; font-weight:bold;">&#40;</span>:fr, <span style="color:#3a27c9; font-weight:bold;">&#123;</span><span style="color:#996600;">&quot;hi&quot;</span> =&gt; <span style="color:#996600;">&quot;bonjour&quot;</span><span style="color:#3a27c9; font-weight:bold;">&#125;</span><span style="color:#3a27c9; font-weight:bold;">&#41;</span> <span style="color:#7b0303; font-style:normal;"># Add the french language hash</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">SimplyGlobal.<span style="color:#3a27c9;">add_language_hash</span><span style="color:#3a27c9; font-weight:bold;">&#40;</span>:es, <span style="color:#3a27c9; font-weight:bold;">&#123;</span><span style="color:#996600;">&quot;hi&quot;</span> =&gt; <span style="color:#996600;">&quot;hola&quot;</span><span style="color:#3a27c9; font-weight:bold;">&#125;</span><span style="color:#3a27c9; font-weight:bold;">&#41;</span> <span style="color:#7b0303; font-style:normal;"># Add the spanish language hash</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;"><span style="color:#996600;">&quot;hi&quot;</span>.<span style="color:#3a27c9;">t</span><span style="color:#3a27c9; font-weight:bold;">&#40;</span>:all<span style="color:#3a27c9; font-weight:bold;">&#41;</span> <span style="color:#7b0303; font-style:normal;"># returns a hash : {:fr =&gt; &quot;bonjour&quot;, :es =&gt; &quot;hola&quot;}</span></div>
</li>
</ol>
</div>
<h3>       Using it with views</h3>
<p>Just create a view ending with <strong>_fr</strong> and simplyglobal will use it.</p>
<div class="ch_code_container" style="font-family: monospace;">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;"><span style="color:#3a27c9; font-weight:normal;">def</span> index</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">&nbsp; SimplyGlobal.<span style="color:#3a27c9;">locale</span> = :fr</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">&nbsp; <span style="color:#7b0303; font-style:normal;"># Will try to render index_fr.html.erb</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">&nbsp; <span style="color:#7b0303; font-style:normal;"># rather than index.html.erb</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;"><span style="color:#3a27c9; font-weight:normal;">end</span></div>
</li>
</ol>
</div>
<p>It also works for the partial.</p>
<div class="ch_code_container" style="font-family: monospace;">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">&lt;%= render :partial =&gt; &quot;info&quot; %&gt;</div>
</li>
</ol>
</div>
<p>will try to render <strong>_info_fr.html.erb</strong> rather than <strong>_info.html.erb</strong>.</p>

<p><a href="http://feeds.feedburner.com/~a/rubyfleebie?a=5Js39r"><img src="http://feeds.feedburner.com/~a/rubyfleebie?i=5Js39r" border="0"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~f/rubyfleebie?a=oxsPM"><img src="http://feeds.feedburner.com/~f/rubyfleebie?i=oxsPM" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/rubyfleebie?a=6tucm"><img src="http://feeds.feedburner.com/~f/rubyfleebie?i=6tucm" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/rubyfleebie?a=8jvIm"><img src="http://feeds.feedburner.com/~f/rubyfleebie?i=8jvIm" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/rubyfleebie?a=xgyOm"><img src="http://feeds.feedburner.com/~f/rubyfleebie?i=xgyOm" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/rubyfleebie?a=lcXTM"><img src="http://feeds.feedburner.com/~f/rubyfleebie?i=lcXTM" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/rubyfleebie?a=q5xQM"><img src="http://feeds.feedburner.com/~f/rubyfleebie?i=q5xQM" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://www.rubyfleebie.com/simplyglobal-a-simple-globalization-plugin-for-rails/feed/</wfw:commentRss>
		</item>
		<item>
		<title>How to display a collection grouped by an attribute value in Rails</title>
		<link>http://www.rubyfleebie.com/how-to-display-a-collection-grouped-by-an-attribute-value-in-rails/</link>
		<comments>http://www.rubyfleebie.com/how-to-display-a-collection-grouped-by-an-attribute-value-in-rails/#comments</comments>
		<pubDate>Mon, 13 Oct 2008 19:44:32 +0000</pubDate>
		<dc:creator>Frank</dc:creator>
		
		<category><![CDATA[In depth]]></category>

		<guid isPermaLink="false">http://www.rubyfleebie.com/how-to-display-a-collection-grouped-by-an-attribute-value-in-rails/</guid>
		<description><![CDATA[Flashback time. We are in 1999 and you are coding in ASP... yet people are not laughing at you.]]></description>
			<content:encoded><![CDATA[<p>Flashback time. We are in 1999 and you are coding in ASP thinking that this language is the future. You use ADODB recordsets to iterate over collections. You have a recordset containing some records from a &#8220;quotes&#8221; table. The &#8220;quotes&#8221; table contains a author column (varchar) and a body column (varchar). Now you want to display the results grouped by author name so it looks like this : </p>
<p><strong>Georges Brassens</strong><br />
- Les filles quand ça dit &#8220;je t&#8217;aime&#8221;, c&#8217;est comme un second baptême<br />
- Aucune idée sur terre n&#8217;est digne d&#8217;un trépas</p>
<p><strong>Billie Holiday</strong><br />
- Don&#8217;t threaten me with love, baby. Let&#8217;s just go walking in the rain.<br />
- I never hurt nobody but myself and that&#8217;s nobody&#8217;s business but my own.</p>
<p>Assuming your recordset is ordered by author, you do something like this (remember, we&#8217;re in 1999) : </p>
<div class="ch_code_container" style="font-family: monospace;">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">&lt;TABLE style=<span style="color: #ff0000;">&quot;color:fushia;font-style:MSONormal generated=frontpage&quot;</span>&gt;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">&lt;%<span style="color: #b1b100;">while</span> <span style="color: #b1b100;">not</span> objRS.<span style="color: #b1b100;">eof</span> %&gt;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">&lt;%<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>current_author &lt;&gt; objRS<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;author&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>%&gt;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">&lt;h1&gt;&lt;%=objRS<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;author&quot;</span><span style="color: #66cc66;">&#41;</span>%&gt;&lt;/h1&gt;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">&lt;%<span style="color: #b1b100;">end</span> if%&gt;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">&lt;Tr&gt;&lt;td&gt;&lt;%=objRS<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;body&quot;</span><span style="color: #66cc66;">&#41;</span>&lt;/td&gt;&lt;/TR&gt;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">&lt;%current_author = objRS<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;author&quot;</span><span style="color: #66cc66;">&#41;</span>%&gt;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">&lt;%objRS.<span style="color: #66cc66;">MoveNext</span>%&gt;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">&lt;%loop%&gt;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">&lt;%objRS.<span style="color: #66cc66;">close</span>%&gt;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">&lt;/TABLE&gt;</div>
</li>
</ol>
</div>
<p>Ok welcome back in 2008. ASP is dead. You are coding in rails and you want to do the same thing. How will you do it? Storing the author name in a buffer variable like in 1999? Not too sure about it.</p>
<p>This is a job for Enumerable#group_by (Enumerable is a <a href="http://www.rubyfleebie.com/an-introduction-to-modules-part-2">module</a> that is mixed in the Array class)</p>
<div class="ch_code_container" style="font-family: monospace;">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">all_quotes = Quote.<span style="color:#3a27c9;">find</span><span style="color:#3a27c9; font-weight:bold;">&#40;</span>:all<span style="color:#3a27c9; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">@authors = all_quotes.<span style="color:#3a27c9;">group_by</span><span style="color:#3a27c9; font-weight:bold;">&#40;</span>&amp;:author<span style="color:#3a27c9; font-weight:bold;">&#41;</span></div>
</li>
</ol>
</div>
<p>Enumerable#group_by will create different sets of quotes based on the &#8220;author&#8221; values. Why the &#8220;&#038;&#8221; sign? It&#8217;s because group_by expects a block. I could have done it this way  : @authors = all_quotes.group_by{|quote| quote.author}</p>
<p>So @authors is now a hash that will look like this:</p>
<p>{&#8221;George Brassens&#8221; => [#&lt;Quote id:131 &#8230;&gt;, #&lt;Quote id:331 &#8230;&gt;], &#8220;Billie Holiday&#8221; => [#&lt;Quote id:111 &#8230;&gt;, #&lt;Quote id:911 &#8230;&gt;] }</p>
<p>Now you can iterate over it like this :</p>
<div class="ch_code_container" style="font-family: monospace;">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">@authors.<span style="color:#3a27c9;">each_pair</span> <span style="color:#3a27c9; font-weight:normal;">do</span> |author_name, quotes|</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">&nbsp; &lt;h1&gt;&lt;%=author_name%&gt;&lt;/h1&gt;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">&nbsp; &lt;%quotes.<span style="color:#3a27c9;">each</span> <span style="color:#3a27c9; font-weight:normal;">do</span> |quote| %&gt;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">&nbsp; &nbsp; - &lt;%=quote.<span style="color:#3a27c9;">body</span>%&gt;&lt;br /&gt;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">&nbsp; &lt;%end%&gt;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">&lt;%end%&gt;</div>
</li>
</ol>
</div>
<p>One more thing to note : @authors is a hash, and hashes cannot be ordered. If you want to display quotes by sorted author name, you could do this :</p>
<div class="ch_code_container" style="font-family: monospace;">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">@authors.<span style="color:#3a27c9;">keys</span>.<span style="color:#3a27c9;">sort</span>.<span style="color:#3a27c9;">each</span> <span style="color:#3a27c9; font-weight:normal;">do</span> |author_name|</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">&nbsp; &lt;h1&gt;&lt;%=author_name%&gt;&lt;/h1&gt;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">&nbsp; &lt;%@authors<span style="color:#3a27c9; font-weight:bold;">&#91;</span>author_name<span style="color:#3a27c9; font-weight:bold;">&#93;</span>.<span style="color:#3a27c9;">each</span> <span style="color:#3a27c9; font-weight:normal;">do</span> |quote|%&gt;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">&nbsp; &nbsp; - &lt;%=quote.<span style="color:#3a27c9;">body</span>%&gt;&lt;br /&gt;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">&nbsp; &lt;%end%&gt;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">&lt;%end%&gt;</div>
</li>
</ol>
</div>
<p>Note : Enumerable#group_by does not exist in ruby 1.8, it only exists in Rails. The method will be in ruby 1.9 however.</p>

<p><a href="http://feeds.feedburner.com/~a/rubyfleebie?a=APcqDV"><img src="http://feeds.feedburner.com/~a/rubyfleebie?i=APcqDV" border="0"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~f/rubyfleebie?a=p4q5M"><img src="http://feeds.feedburner.com/~f/rubyfleebie?i=p4q5M" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/rubyfleebie?a=3FqMm"><img src="http://feeds.feedburner.com/~f/rubyfleebie?i=3FqMm" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/rubyfleebie?a=BYxFm"><img src="http://feeds.feedburner.com/~f/rubyfleebie?i=BYxFm" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/rubyfleebie?a=6vpLm"><img src="http://feeds.feedburner.com/~f/rubyfleebie?i=6vpLm" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/rubyfleebie?a=JcHeM"><img src="http://feeds.feedburner.com/~f/rubyfleebie?i=JcHeM" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/rubyfleebie?a=BY7YM"><img src="http://feeds.feedburner.com/~f/rubyfleebie?i=BY7YM" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://www.rubyfleebie.com/how-to-display-a-collection-grouped-by-an-attribute-value-in-rails/feed/</wfw:commentRss>
		</item>
		<item>
		<title>XMPP4r 0.4 has been released</title>
		<link>http://www.rubyfleebie.com/xmpp4r-04-has-been-released/</link>
		<comments>http://www.rubyfleebie.com/xmpp4r-04-has-been-released/#comments</comments>
		<pubDate>Tue, 07 Oct 2008 13:54:25 +0000</pubDate>
		<dc:creator>Frank</dc:creator>
		
		<category><![CDATA[Easy reading]]></category>

		<guid isPermaLink="false">http://www.rubyfleebie.com/xmpp4r-04-has-been-released/</guid>
		<description><![CDATA[It&#8217;s been a while since the latest release of XMPP4r. I was starting to think that the development for this great library had been stopped.
Fortunately, version 0.4 has been released on August 5th 2008. We&#8217;re going to try this new version internally and eventually use it for TimmyOnTime. I am personnally hoping for less memory [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s been a while since the latest release of <a href="http://home.gna.org/xmpp4r/">XMPP4r</a>. I was starting to think that the development for this great library had been stopped.</p>
<p>Fortunately, version 0.4 has been released on August 5th 2008. We&#8217;re going to try this new version internally and eventually use it for TimmyOnTime. I am personnally hoping for less memory consumption, more speed and more stability. This new version highlights are : </p>
<ol>
<li>The beginning of ruby 1.9 support</li>
<li>Refactoring of error classes (I&#8217;m really looking forward to this)</li>
</ol>
<p>Here is the full changelog :</p>
<p>XMPP4R 0.4 (05/08/2008)<br />
=======================<br />
* Initial support for Ruby 1.9 (see README_ruby19.txt)<br />
* Complete PubSub API Change - more logical and better for<br />
  childclasses, support for collection node creation<br />
* a Helper to assist with XEP-0115 Entity Capabilities<br />
* SASL anonymous support<br />
* File transfer fixes<br />
* MUC room configuration fixes<br />
* initial support for XEP-0118 User Tune<br />
* fix for an xmlrpc exception-during-serialisation bug, which would cause<br />
  a hang<br />
* Support auto-gem generation on GitHub with improved and DRY&#8217;er RakeFile and<br />
  gemspec.<br />
* Add support for the old SSL protocol (needed to connect to GTalk)<br />
* Changed API for Client, Component, Connection, Stream to remove<br />
  need for antiquated &#8216;threaded&#8217; param in the initializer.<br />
* Use a Logger instance instead of directly writing to stdout<br />
* Re-factored &#038; consolidated Error classes.  See xmpp4r/errors.rb for all<br />
  custom errors that can be caught.  All inherit from Jabber::Error which<br />
  itself inherits from Ruby&#8217;s StandardError. This is a first step in<br />
  re-factoring errors.  The next step will be to convert all &#8216;raise&#8217; calls to<br />
  raise a custom Jabber::Error or one of its children instead of anonymous<br />
  RuntimeErrors.  This allows much more granularity in catching and handling<br />
  errors later.<br />
  If you were catching Jabber::ErrorException before you should probably<br />
  change that in your code to now catch Jabber::Error if you want to<br />
  catch everything or one of the custom children of Jabber::Error defined in<br />
  &#8216;lib/xmpp4r/errors.rb&#8217;.  Additionally, the Error class which encapsulated<br />
  the xmpp error response, has been renamed to ErrorResponse to reflect its<br />
  real usage.  This free&#8217;s up &#8216;Jabber::Error&#8217; for use as our base Error class.</p>

<p><a href="http://feeds.feedburner.com/~a/rubyfleebie?a=T955SV"><img src="http://feeds.feedburner.com/~a/rubyfleebie?i=T955SV" border="0"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~f/rubyfleebie?a=fEcWM"><img src="http://feeds.feedburner.com/~f/rubyfleebie?i=fEcWM" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/rubyfleebie?a=F9i2m"><img src="http://feeds.feedburner.com/~f/rubyfleebie?i=F9i2m" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/rubyfleebie?a=kDy8m"><img src="http://feeds.feedburner.com/~f/rubyfleebie?i=kDy8m" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/rubyfleebie?a=S5iIm"><img src="http://feeds.feedburner.com/~f/rubyfleebie?i=S5iIm" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/rubyfleebie?a=NclUM"><img src="http://feeds.feedburner.com/~f/rubyfleebie?i=NclUM" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/rubyfleebie?a=ym4XM"><img src="http://feeds.feedburner.com/~f/rubyfleebie?i=ym4XM" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://www.rubyfleebie.com/xmpp4r-04-has-been-released/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Get the intersection of 2 arrays</title>
		<link>http://www.rubyfleebie.com/get-the-intersection-of-2-arrays/</link>
		<comments>http://www.rubyfleebie.com/get-the-intersection-of-2-arrays/#comments</comments>
		<pubDate>Fri, 03 Oct 2008 21:39:25 +0000</pubDate>
		<dc:creator>Frank</dc:creator>
		
		<category><![CDATA[short &amp; sweet]]></category>

		<guid isPermaLink="false">http://www.rubyfleebie.com/get-the-intersection-of-2-arrays/</guid>
		<description><![CDATA[Pretty easy stuff today, but this is not something you may need to do often so maybe you don&#8217;t know how to do it.
Say I have these 2 arrays :



colors1 = &#91;:blue, :red, :green, :orange, :purple&#93;


colors2 = &#91;:yellow, :cyan, :green, :blue, :purple&#93;



Now what if I want a new array that contains only the elements present [...]]]></description>
			<content:encoded><![CDATA[<p>Pretty easy stuff today, but this is not something you may need to do often so maybe you don&#8217;t know how to do it.</p>
<p>Say I have these 2 arrays :</p>
<div class="ch_code_container" style="font-family: monospace;">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">colors1 = <span style="color:#3a27c9; font-weight:bold;">&#91;</span>:blue, :red, :green, :orange, :purple<span style="color:#3a27c9; font-weight:bold;">&#93;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">colors2 = <span style="color:#3a27c9; font-weight:bold;">&#91;</span>:yellow, :cyan, :green, :blue, :purple<span style="color:#3a27c9; font-weight:bold;">&#93;</span></div>
</li>
</ol>
</div>
<p>Now what if I want a new array that contains only the elements present in both arrays?</p>
<div class="ch_code_container" style="font-family: monospace;">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">beautiful_colors = colors1 &amp; colors2</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;"><span style="color:#7b0303; font-style:normal;">#beautiful_colors contains [:blue, :green, :purple]</span></div>
</li>
</ol>
</div>
<p>Have a nice weekend!</p>

<p><a href="http://feeds.feedburner.com/~a/rubyfleebie?a=02f3p7"><img src="http://feeds.feedburner.com/~a/rubyfleebie?i=02f3p7" border="0"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~f/rubyfleebie?a=t7AlM"><img src="http://feeds.feedburner.com/~f/rubyfleebie?i=t7AlM" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/rubyfleebie?a=e4RNm"><img src="http://feeds.feedburner.com/~f/rubyfleebie?i=e4RNm" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/rubyfleebie?a=awBzm"><img src="http://feeds.feedburner.com/~f/rubyfleebie?i=awBzm" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/rubyfleebie?a=0wPqm"><img src="http://feeds.feedburner.com/~f/rubyfleebie?i=0wPqm" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/rubyfleebie?a=4zGzM"><img src="http://feeds.feedburner.com/~f/rubyfleebie?i=4zGzM" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/rubyfleebie?a=DrthM"><img src="http://feeds.feedburner.com/~f/rubyfleebie?i=DrthM" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://www.rubyfleebie.com/get-the-intersection-of-2-arrays/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Need support for has_many through with has_one in the “bridge table”</title>
		<link>http://www.rubyfleebie.com/need-support-for-has_many-through-with-has_one-in-the-bridge-table/</link>
		<comments>http://www.rubyfleebie.com/need-support-for-has_many-through-with-has_one-in-the-bridge-table/#comments</comments>
		<pubDate>Sat, 13 Sep 2008 18:02:51 +0000</pubDate>
		<dc:creator>Frank</dc:creator>
		
		<category><![CDATA[short &amp; sweet]]></category>

		<guid isPermaLink="false">http://www.rubyfleebie.com/need-support-for-has_many-through-with-has_one-in-the-bridge-table/</guid>
		<description><![CDATA[has_many :through limitations]]></description>
			<content:encoded><![CDATA[<p>Let me expose the problem with an example :</p>
<p>Cart model (as in : shopping cart)</p>
<div class="ch_code_container" style="font-family: monospace;">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;"><span style="color:#3a27c9; font-weight:normal;">class</span> Cart &lt; ActiveRecord::Base</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">&nbsp; has_one :bill</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">&nbsp; belongs_to :user</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;"><span style="color:#3a27c9; font-weight:normal;">end</span></div>
</li>
</ol>
</div>
<p>Bill model</p>
<div class="ch_code_container" style="font-family: monospace;">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;"><span style="color:#3a27c9; font-weight:normal;">class</span> Bill &lt; ActiveRecord::Base</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">&nbsp; belongs_to :cart&nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;"><span style="color:#3a27c9; font-weight:normal;">end</span></div>
</li>
</ol>
</div>
<p>User</p>
<div class="ch_code_container" style="font-family: monospace;">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;"><span style="color:#3a27c9; font-weight:normal;">class</span> User &lt; ActiveRecord::Base</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">&nbsp; has_many :carts</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">&nbsp; has_many :bills, :through =&gt; :carts <span style="color:#7b0303; font-style:normal;">#&lt;== Doesn&#8217;t work&#8230; no user.bills for you!</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;"><span style="color:#3a27c9; font-weight:normal;">end</span></div>
</li>
</ol>
</div>
<p>Basically, because the &#8220;through&#8221; table links the destination table with a has_one relationship, ActiveRecord complains with : <em>Invalid source reflection macro :has_one for has_many :bills, :through => :carts.  Use :source to specify the source reflection.</em></p>
<p>Someone on Rails Trac already created a <a href="http://dev.rubyonrails.org/ticket/4996">ticket</a> about this issue <strong>2 years ago</strong>. Can we expect this feature to be officially supported in the future? Let&#8217;s hope so.</p>

<p><a href="http://feeds.feedburner.com/~a/rubyfleebie?a=pKPEG8"><img src="http://feeds.feedburner.com/~a/rubyfleebie?i=pKPEG8" border="0"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~f/rubyfleebie?a=oCqTL"><img src="http://feeds.feedburner.com/~f/rubyfleebie?i=oCqTL" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/rubyfleebie?a=B96ul"><img src="http://feeds.feedburner.com/~f/rubyfleebie?i=B96ul" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/rubyfleebie?a=C29fl"><img src="http://feeds.feedburner.com/~f/rubyfleebie?i=C29fl" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/rubyfleebie?a=VD5ml"><img src="http://feeds.feedburner.com/~f/rubyfleebie?i=VD5ml" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/rubyfleebie?a=KSoFL"><img src="http://feeds.feedburner.com/~f/rubyfleebie?i=KSoFL" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/rubyfleebie?a=SXoSL"><img src="http://feeds.feedburner.com/~f/rubyfleebie?i=SXoSL" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://www.rubyfleebie.com/need-support-for-has_many-through-with-has_one-in-the-bridge-table/feed/</wfw:commentRss>
		</item>
		<item>
		<title>acts_as_state_machine and variable initial states</title>
		<link>http://www.rubyfleebie.com/acts_as_state_machine-and-variable-initial-states/</link>
		<comments>http://www.rubyfleebie.com/acts_as_state_machine-and-variable-initial-states/#comments</comments>
		<pubDate>Wed, 10 Sep 2008 15:54:16 +0000</pubDate>
		<dc:creator>Frank</dc:creator>
		
		<category><![CDATA[short &amp; sweet]]></category>

		<guid isPermaLink="false">http://www.rubyfleebie.com/acts_as_state_machine-and-variable-initial-states/</guid>
		<description><![CDATA[acts_as_state_machine is a great plugin really useful when you want to add constraints and behavior to your model objects.
Note : Those who don&#8217;t know what this plugin is all about should stop reading right there or risk being completely lost.
One thing that seems impossible to do with the plugin is to have variable &#8220;initial states&#8221; [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://agilewebdevelopment.com/plugins/acts_as_state_machine">acts_as_state_machine</a> is a great plugin really useful when you want to add constraints and behavior to your model objects.</p>
<p><strong>Note</strong> : Those who don&#8217;t know what this plugin is all about should stop reading right there or risk being completely lost.</p>
<p>One thing that seems impossible to do with the plugin is to have variable &#8220;initial states&#8221; for an object. Say for example that you have a User model that needs to act as a state machine. New users have to fill up a signup form to gain access to the application. These new users will be &#8220;pending&#8221; until an administrator approve them (enabled) or refuse them (disabled)</p>
<div class="ch_code_container" style="font-family: monospace;">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;"><span style="color:#3a27c9; font-weight:normal;">class</span> User &lt; ActiveRecord::Base</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">&nbsp; acts_as_state_machine :initial =&gt; :pending</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">&nbsp; state :pending</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">&nbsp; state :enabled</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">&nbsp; state :disabled</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">&nbsp; event :enable <span style="color:#3a27c9; font-weight:normal;">do</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">&nbsp; &nbsp; transitions :from =&gt; :pending, :to =&gt; :enabled</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">&nbsp; &nbsp; transitions :from =&gt; :disabled, :to =&gt; :enabled</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">&nbsp; <span style="color:#3a27c9; font-weight:normal;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">&nbsp; event :disable <span style="color:#3a27c9; font-weight:normal;">do</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">&nbsp; &nbsp; transitions :from =&gt; :pending, :to =&gt; :disabled</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">&nbsp; &nbsp; transitions :from =&gt; :enabled, :to =&gt; :disabled</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">&nbsp; <span style="color:#3a27c9; font-weight:normal;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;"><span style="color:#3a27c9; font-weight:normal;">end</span></div>
</li>
</ol>
</div>
<p>So far so good. But what if an administrator can also create a new user? In this case we would&#8217;nt want the user to be &#8220;pending&#8221;, we want him to be &#8220;enabled&#8221; right away. </p>
<p>I experimented a similar scenario, so I tried a few things to bypass the initial state :</p>
<div class="ch_code_container" style="font-family: monospace;">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;"><span style="color:#7b0303; font-style:normal;">#FAILED ATTEMPT #1</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;"><span style="color:#3a27c9; font-weight:normal;">def</span> create</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">&nbsp; user = User.<span style="color:#3a27c9;">new</span><span style="color:#3a27c9; font-weight:bold;">&#40;</span>params<span style="color:#3a27c9; font-weight:bold;">&#91;</span>:user<span style="color:#3a27c9; font-weight:bold;">&#93;</span><span style="color:#3a27c9; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">&nbsp; user.<span style="color:#3a27c9;">state</span> = :enabled</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">&nbsp; user.<span style="color:#3a27c9;">save</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;"><span style="color:#3a27c9; font-weight:normal;">end</span></div>
</li>
</ol>
</div>
<p>Too bad, so I tried this :</p>
<div class="ch_code_container" style="font-family: monospace;">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;"><span style="color:#7b0303; font-style:normal;">#FAILED ATTEMPT #2</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;"><span style="color:#3a27c9; font-weight:normal;">def</span> create</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">&nbsp; User.<span style="color:#3a27c9;">initial_state</span> = :enabled</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">&nbsp; user = User.<span style="color:#3a27c9;">new</span><span style="color:#3a27c9; font-weight:bold;">&#40;</span>params<span style="color:#3a27c9; font-weight:bold;">&#91;</span>:user<span style="color:#3a27c9; font-weight:bold;">&#93;</span><span style="color:#3a27c9; font-weight:bold;">&#41;</span>&nbsp; </div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">&nbsp; user.<span style="color:#3a27c9;">save</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;"><span style="color:#3a27c9; font-weight:normal;">end</span></div>
</li>
</ol>
</div>
<p>Of course, the following worked :</p>
<div class="ch_code_container" style="font-family: monospace;">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;"><span style="color:#7b0303; font-style:normal;">#SUCCESSFUL BUT SUCKY ATTEMPT</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;"><span style="color:#3a27c9; font-weight:normal;">def</span> create</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">&nbsp; user = User.<span style="color:#3a27c9;">new</span><span style="color:#3a27c9; font-weight:bold;">&#40;</span>params<span style="color:#3a27c9; font-weight:bold;">&#91;</span>:user<span style="color:#3a27c9; font-weight:bold;">&#93;</span><span style="color:#3a27c9; font-weight:bold;">&#41;</span>&nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">&nbsp; user.<span style="color:#3a27c9;">save</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">&nbsp; user.<span style="color:#3a27c9;">enable</span>!</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;"><span style="color:#3a27c9; font-weight:normal;">end</span></div>
</li>
</ol>
</div>
<p>Yuk&#8230; there had to be something better.</p>
<p>Finally, I learned that initial_state was an <em>inheritable attribute</em>. I didn&#8217;t know what those were all about so I did some googling. The only good explanation I found is <a href="http://gigavolt.net/blog/ruby-rails/inheritableAttributesInRails.writeback">this one</a>.</p>
<p>Anyway, I was able to override the initial state of my user object by doing this :</p>
<div class="ch_code_container" style="font-family: monospace;">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;"><span style="color:#7b0303; font-style:normal;">#SUCCESSFUL ATTEMPT!</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;"><span style="color:#3a27c9; font-weight:normal;">def</span> create</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">&nbsp; User.<span style="color:#3a27c9;">write_inheritable_attribute</span> :initial_state, :enabled</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">&nbsp; user = User.<span style="color:#3a27c9;">new</span><span style="color:#3a27c9; font-weight:bold;">&#40;</span>params<span style="color:#3a27c9; font-weight:bold;">&#91;</span>:user<span style="color:#3a27c9; font-weight:bold;">&#93;</span><span style="color:#3a27c9; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">&nbsp; user.<span style="color:#3a27c9;">save</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;"><span style="color:#3a27c9; font-weight:normal;">end</span></div>
</li>
</ol>
</div>
<p>It&#8217;s not pretty I know, but at least it gets the job done. I wonder if the fact that you cannot &#8220;easily&#8221; change the initial state programmatically is a missing feature that should be added in a future version&#8230; or if it&#8217;s only my understanding of a &#8220;state machine&#8221; that sucks. Any thoughts?</p>

<p><a href="http://feeds.feedburner.com/~a/rubyfleebie?a=c6iLvi"><img src="http://feeds.feedburner.com/~a/rubyfleebie?i=c6iLvi" border="0"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~f/rubyfleebie?a=As0fL"><img src="http://feeds.feedburner.com/~f/rubyfleebie?i=As0fL" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/rubyfleebie?a=1O1wl"><img src="http://feeds.feedburner.com/~f/rubyfleebie?i=1O1wl" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/rubyfleebie?a=AcgHl"><img src="http://feeds.feedburner.com/~f/rubyfleebie?i=AcgHl" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/rubyfleebie?a=Jv6Ll"><img src="http://feeds.feedburner.com/~f/rubyfleebie?i=Jv6Ll" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/rubyfleebie?a=KX7FL"><img src="http://feeds.feedburner.com/~f/rubyfleebie?i=KX7FL" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/rubyfleebie?a=Mt4SL"><img src="http://feeds.feedburner.com/~f/rubyfleebie?i=Mt4SL" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://www.rubyfleebie.com/acts_as_state_machine-and-variable-initial-states/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Use named_scope to add elegant finders to your models</title>
		<link>http://www.rubyfleebie.com/use-named_scope-to-add-elegant-finders-to-your-models/</link>
		<comments>http://www.rubyfleebie.com/use-named_scope-to-add-elegant-finders-to-your-models/#comments</comments>
		<pubDate>Wed, 20 Aug 2008 16:46:02 +0000</pubDate>
		<dc:creator>Frank</dc:creator>
		
		<category><![CDATA[short &amp; sweet]]></category>

		<guid isPermaLink="false">http://www.rubyfleebie.com/use-named_scope-to-add-elegant-finders-to-your-models/</guid>
		<description><![CDATA[I used to be a fan of the has_finder gem written by Nick Kallen.
Now with Rails 2.1, this functionnality is built-in so you don&#8217;t need to install the gem.
The named_scope method allows you to add finders to any model. A code snippet is worth 10000 words :



class Story &#60; ActiveRecord::Base


&#160; named_scope :hilarious, :conditions =&#62; &#91;&#34;type [...]]]></description>
			<content:encoded><![CDATA[<p>I used to be a fan of the <a href="http://pivots.pivotallabs.com/users/nick/blog/articles/284-hasfinder-it-s-now-easier-than-ever-to-create-complex-re-usable-sql-queries">has_finder gem</a> written by Nick Kallen.</p>
<p>Now with Rails 2.1, this functionnality is built-in so you don&#8217;t need to install the gem.</p>
<p>The named_scope method allows you to add finders to any model. A code snippet is worth 10000 words :</p>
<div class="ch_code_container" style="font-family: monospace;">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;"><span style="color:#3a27c9; font-weight:normal;">class</span> Story &lt; ActiveRecord::Base</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">&nbsp; named_scope :hilarious, :conditions =&gt; <span style="color:#3a27c9; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;type = ?&quot;</span>,<span style="color:#996600;">&quot;comedy&quot;</span><span style="color:#3a27c9; font-weight:bold;">&#93;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">&nbsp; named_scope :popular, :conditions =&gt; <span style="color:#3a27c9; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;popularity_level &gt; ?&quot;</span>, <span style="color:#3a27c9;">3</span><span style="color:#3a27c9; font-weight:bold;">&#93;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;"><span style="color:#3a27c9; font-weight:normal;">end</span></div>
</li>
</ol>
</div>
<p>Doing this you can chain expressions like this : </p>
<div class="ch_code_container" style="font-family: monospace;">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">funny_stories = @all_stories.<span style="color:#3a27c9;">hilarious</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">funny_and_popular = @all_stories.<span style="color:#3a27c9;">hilarious</span>.<span style="color:#3a27c9;">popular</span></div>
</li>
</ol>
</div>
<p>Maybe you knew it, maybe you didn&#8217;t. If you didn&#8217;t, now you know it. (I feel very smart about this last sentence. I think I am going to put this in my room somewhere)</p>

<p><a href="http://feeds.feedburner.com/~a/rubyfleebie?a=kqxYgS"><img src="http://feeds.feedburner.com/~a/rubyfleebie?i=kqxYgS" border="0"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~f/rubyfleebie?a=zPu8OK"><img src="http://feeds.feedburner.com/~f/rubyfleebie?i=zPu8OK" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/rubyfleebie?a=qoOj8k"><img src="http://feeds.feedburner.com/~f/rubyfleebie?i=qoOj8k" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/rubyfleebie?a=lMbtSk"><img src="http://feeds.feedburner.com/~f/rubyfleebie?i=lMbtSk" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/rubyfleebie?a=fkZf6k"><img src="http://feeds.feedburner.com/~f/rubyfleebie?i=fkZf6k" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/rubyfleebie?a=1r8DfK"><img src="http://feeds.feedburner.com/~f/rubyfleebie?i=1r8DfK" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/rubyfleebie?a=QJaKqK"><img src="http://feeds.feedburner.com/~f/rubyfleebie?i=QJaKqK" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://www.rubyfleebie.com/use-named_scope-to-add-elegant-finders-to-your-models/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Komodo Edit : A great editor for dynamic languages</title>
		<link>http://www.rubyfleebie.com/komodo-edit-a-great-editor-for-dynamic-languages/</link>
		<comments>http://www.rubyfleebie.com/komodo-edit-a-great-editor-for-dynamic-languages/#comments</comments>
		<pubDate>Mon, 26 May 2008 21:17:40 +0000</pubDate>
		<dc:creator>Frank</dc:creator>
		
		<category><![CDATA[Easy reading]]></category>

		<guid isPermaLink="false">http://www.rubyfleebie.com/komodo-edit-a-great-editor-for-dynamic-languages/</guid>
		<description><![CDATA[Being a Linux/Ubuntu user, i cannot use the praised Textmate editor to develop ruby applications. I tried a lot of editors, some of them being very good ones, but in the end I always end up using gedit.
gedit is great&#8230; but it is rather limited features-wise. Recently I have tried Komodo Edit and I can [...]]]></description>
			<content:encoded><![CDATA[<p>Being a Linux/Ubuntu user, i cannot use the praised <a href="http://macromates.com/">Textmate</a> editor to develop ruby applications. I tried a lot of editors, some of them being very good ones, but in the end I always end up using <a href="http://www.gnome.org/projects/gedit/">gedit</a>.</p>
<p>gedit is great&#8230; but it is rather limited features-wise. Recently I have tried <a href="http://www.activestate.com/Products/komodo_ide/komodo_edit.mhtml">Komodo Edit</a> and I can say that this is <strong>the best RubyOnRails editor available on Linux</strong>. It has a lot of features (autocomplete, tons of supported languages, macros, color schemes, etc) but somehow it is still light and minimalist.</p>
<p>Komodo Edit is also available on Mac and Windows</p>
<p><img src="http://www.rubyfleebie.com/wp-content/uploads/2008/05/komodo.png" width="600" alt="Komodo screenshot" /></p>

<p><a href="http://feeds.feedburner.com/~a/rubyfleebie?a=dPuhUg"><img src="http://feeds.feedburner.com/~a/rubyfleebie?i=dPuhUg" border="0"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~f/rubyfleebie?a=cGEjnH"><img src="http://feeds.feedburner.com/~f/rubyfleebie?i=cGEjnH" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/rubyfleebie?a=PBlAsh"><img src="http://feeds.feedburner.com/~f/rubyfleebie?i=PBlAsh" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/rubyfleebie?a=su0fdh"><img src="http://feeds.feedburner.com/~f/rubyfleebie?i=su0fdh" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/rubyfleebie?a=esehNh"><img src="http://feeds.feedburner.com/~f/rubyfleebie?i=esehNh" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/rubyfleebie?a=oYwDNH"><img src="http://feeds.feedburner.com/~f/rubyfleebie?i=oYwDNH" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/rubyfleebie?a=Qv6dwH"><img src="http://feeds.feedburner.com/~f/rubyfleebie?i=Qv6dwH" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://www.rubyfleebie.com/komodo-edit-a-great-editor-for-dynamic-languages/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Phusion Passenger, you’re my hero</title>
		<link>http://www.rubyfleebie.com/phusion-passenger-youre-my-hero/</link>
		<comments>http://www.rubyfleebie.com/phusion-passenger-youre-my-hero/#comments</comments>
		<pubDate>Fri, 23 May 2008 00:11:03 +0000</pubDate>
		<dc:creator>Frank</dc:creator>
		
		<category><![CDATA[Easy reading]]></category>

		<guid isPermaLink="false">http://www.rubyfleebie.com/phusion-passenger-youre-my-hero/</guid>
		<description><![CDATA[I feel so weird. Maybe it&#8217;s just the side-effects of eating that old, pinky-brown colored and talking piece of raw meat that was sitting in the corner of my kitchen since a month or two&#8230; but as strange as it may sound, I think it&#8217;s something else.
I think it is something related to Rails that [...]]]></description>
			<content:encoded><![CDATA[<p>I feel so weird. Maybe it&#8217;s just the side-effects of eating that old, pinky-brown colored and talking piece of raw meat that was sitting in the corner of my kitchen since a month or two&#8230; but as strange as it may sound, I think it&#8217;s something else.</p>
<p>I think it is something related to Rails that makes me so excited. I just tried deploying a rails application with <a href="http://modrails.com">Phusion Passenger</a> (aka mod_rails)&#8230; and I am still shocked. Rails deployment has finally becomes a breeze! At the time I am writing this, I still have this exaggerated dumb smile in my face expressing my satisfaction&#8230; and I am alone in my house.</p>
<p>#UPDATE : I removed the smiling pig image that was put here to &#8220;express my satisfaction&#8221;. I removed it because it was not funny and it made me look like someone who is not funny&#8230; which is something I am not comfortable with. Now that I have removed the image of the smiling pig, I expect to receive my first comment (and tons of others) for this blog post. For those who liked the smiling pig, you can find it <a href="http://www.rubyfleebie.com/wp-content/uploads/2008/05/Pig_Smile.jpg">here</a> and pretend it is funny.</p>
<p>Ok enough stupidities, <a href="http://www.modrails.com/">Phusion Passenger</a> is great. It is built to work with the Apache web server. No port configuration, no complicated vhost configuration, you upload your stuff and guess what? It works. Well, almost&#8230; to restart your rails application you just have to <em>touch</em> tmp/restart.txt. If you&#8217;re using capistrano, it means that you only have to do something like that :</p>
<div class="ch_code_container" style="font-family: monospace;">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">namespace :deploy <span style="color:#3a27c9; font-weight:normal;">do</span>&nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">&nbsp; task :restart, :roles =&gt; :app <span style="color:#3a27c9; font-weight:normal;">do</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">&nbsp; &nbsp; run <span style="color:#996600;">&quot;touch #{current_path}/tmp/restart.txt&quot;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">&nbsp; <span style="color:#3a27c9; font-weight:normal;">end</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;"><span style="color:#3a27c9; font-weight:normal;">end</span></div>
</li>
</ol>
</div>
<p><strong>And what about the vhost configuration?</strong><br />
<code><br />
&lt;VirtualHost *:80&gt;<br />
  ServerName yourdomain.com<br />
  ServerAlias www.yourdomain.com<br />
  DocumentRoot /home/you/apps/yourapp/current/public<br />
&lt;/VirtualHost&gt;<br />
</code><br />
I didn&#8217;t have time to play with all the settings and read the documentation&#8230; but I have the feeling that this thing is the light, try it!</p>
<p><code><br />
gem install passenger<br />
passenger-install-apache2-module<br />
</code></p>

<p><a href="http://feeds.feedburner.com/~a/rubyfleebie?a=tJOIDl"><img src="http://feeds.feedburner.com/~a/rubyfleebie?i=tJOIDl" border="0"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~f/rubyfleebie?a=ys0VMH"><img src="http://feeds.feedburner.com/~f/rubyfleebie?i=ys0VMH" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/rubyfleebie?a=Jd7crh"><img src="http://feeds.feedburner.com/~f/rubyfleebie?i=Jd7crh" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/rubyfleebie?a=6hrIBh"><img src="http://feeds.feedburner.com/~f/rubyfleebie?i=6hrIBh" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/rubyfleebie?a=PPtW0h"><img src="http://feeds.feedburner.com/~f/rubyfleebie?i=PPtW0h" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/rubyfleebie?a=0JF8TH"><img src="http://feeds.feedburner.com/~f/rubyfleebie?i=0JF8TH" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/rubyfleebie?a=knuheH"><img src="http://feeds.feedburner.com/~f/rubyfleebie?i=knuheH" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://www.rubyfleebie.com/phusion-passenger-youre-my-hero/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Railers Need To Stop Not Caring About The Database</title>
		<link>http://www.rubyfleebie.com/railers-need-to-stop-not-caring-about-the-database/</link>
		<comments>http://www.rubyfleebie.com/railers-need-to-stop-not-caring-about-the-database/#comments</comments>
		<pubDate>Thu, 24 Apr 2008 14:36:38 +0000</pubDate>
		<dc:creator>Frank</dc:creator>
		
		<category><![CDATA[Easy reading]]></category>

		<guid isPermaLink="false">http://www.rubyfleebie.com/railers-need-to-stop-not-caring-about-the-database/</guid>
		<description><![CDATA[For many of us, databases are no fun&#8230; surely not as fun as ActiveRecord or Rails in general. Every railers I know love the &#8220;dot notation&#8221; offered by ActiveRecord associations immensely&#8230;  while refering to the vast majority of raw SQL queries as &#8220;Some ugly non-sense gibberish that we would all prefer not know the [...]]]></description>
			<content:encoded><![CDATA[<p>For many of us, databases are no fun&#8230; surely not as fun as ActiveRecord or Rails in general. Every railers I know love the &#8220;dot notation&#8221; offered by ActiveRecord associations immensely&#8230;  while refering to the vast majority of raw SQL queries as &#8220;Some ugly non-sense gibberish that we would all prefer not know the existence&#8221;.</p>
<p>Our hate towards the SQL language and databases is not a valid excuse to not assume our responsabilities as application developers. Recently I was working on a Rails plugin when the sentiment of being incompetent struck my body exactly like the lightning strikes an adventurous golfer who happily plays golf during a thunderstorm.</p>
<p>It all happened when I had a query that was generating a lot of results&#8230; and was slow as hell to process&#8230; even though I was using the :include option for Eager Loading.</p>
<p>@stuff = ParentStuff.find(:all, :include => :child_stuff, :order => &#8220;created_at DESC&#8221;)</p>
<p>Then, in my view, I had something like :</p>
<div class="ch_code_container" style="font-family: monospace;">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">@stuff.<span style="color:#3a27c9;">each</span> <span style="color:#3a27c9; font-weight:normal;">do</span> |parent_stuff|</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">&nbsp; bla bla bla &lt;%=parent_stuff.<span style="color:#3a27c9;">name</span>%&gt;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">&nbsp; parent_stuff.<span style="color:#3a27c9;">child_stuff</span>.<span style="color:#3a27c9;">each</span> <span style="color:#3a27c9; font-weight:normal;">do</span> |stuff|</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">&nbsp; &nbsp; &nbsp;bla bla bla &lt;%=stuff.<span style="color:#3a27c9;">title</span>%&gt;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;">&nbsp; <span style="color:#3a27c9; font-weight:normal;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;">
<div style="font-family: 'Trebuchet MS', Arial, Verdana; font-weight: normal;"><span style="color:#3a27c9; font-weight:normal;">end</span></div>
</li>
</ol>
</div>
<p>It was awfully slow! I started looking at the logs and saw this typical monstruous sql query. Look at it&#8230; you  have to look at it. I know you don&#8217;t want to&#8230; but it won&#8217;t go away :</p>
<p>SELECT parent_stuff.`id` AS t0_r0, parent_stuff.`field1` AS t0_r1, parent_stuff.`field2` AS t0_r2, parent_stuff.`field3` AS t0_r3, parent_stuff.`field4` AS t0_r4, child_stuff.`id` AS t1_r0, child_stuff.`field5` AS t1_r1, child_stuff.`field6` AS t1_r2, child_stuff.`field7` AS t1_r3, child_stuff.`field8` AS t1_r4, child_stuff.`field9` AS t1_r5, child_stuff.`field10` AS t1_r6, child_stuff.`field11` AS t1_r7 FROM parent_stuff LEFT OUTER JOIN child_stuff ON child_stuff.the_foreign_key = parent_stuff.id WHERE (parent_stuff.created_at >= &#8216;2008-04-24 00:00:00&#8242; AND parent_stuff.created_at <= '2008-04-24 09:44:56') ORDER BY parent_stuff.created_at</p>
<p>What's the problem with this? It's just one query... cannot take that much time. I thought it was a "Rails problem" until I ran the query directly in a MySql web interface. It took the same amount of time, meaning that the problem was within the query itself. Oh sh**... it was a database problem! Like many Rails Developers, my database skills are, while not inexistant, inadequate. </p>
<p> I tried to figure out what could be the problem with this query. I replaced every LEFT OUTER JOIN with INNER JOIN and Boom! the query executed in less than half a second. </p>
<p>My urgent desire of leaving the MySql web interface made me go back to my Rails code. I then tried some stupid random stuff like : </p>
<p>@stuff = ParentStuff.find(:all, :include => :child_stuff, :joins => &#8220;INNER JOIN child_stuff ON child_stuff.parent_stuff_id=id&#8221; :order => &#8220;created_at DESC&#8221;) but the slow LEFT OUTER JOINs remain in the query instead of being replace by the faster INNER JOIN. Then, I learned that in Rails 2.0, you could do Eager Loading with INNER JOIN by passing an association name to the :joins option, like that :</p>
<p>@stuff = ParentStuff.find(:all, :joins => :child_stuff, :order => &#8220;created_at DESC&#8221;)</p>
<p>Problem is I was working on a plugin&#8230; and I&#8217;d like it to be compatible with older versions of Rails. So I did what I was scared of doing since the beginning : Going to some MySql forums to read the advice of some DBA&#8217;s &#8230;</p>
<p>Then I found the answer to my problem&#8230; and this answer proved me that I needed to stop playing blindly with ActiveRecord like if the backend database was none of my business&#8230;. because it is. The answer is so obvious&#8230; but given the fact that I never wanted to care about databases since Rails exists, I didn&#8217;t think about it :</p>
<p><strong>Answer : Add an index to the foreign key in the child table!</strong></p>
<p>This is just one case that shows the dangers of not being aware of what&#8217;s going on in the database. It&#8217;s sad, but you cannot blindly let ActiveRecord manage everything that is Database related for you. It&#8217;s only a question of being in control of the entire application&#8230; and the relational database is a part of it.</p>
<p><strong>UPDATE</strong><br />
This post has kind of <a href="http://www.railway.at/articles/2008/04/24/database-agnostic-database-ignorant">turned against me</a>. The only thing I wanted to say with this article is that ActiveRecord needs our supervision&#8230; not that I am a moron who can&#8217;t understand databases. I know what joins are, I know what indexes are&#8230; but in most situations I don&#8217;t have to &#8220;care&#8221; about them with ActiveRecord. Sorry but I felt the need to defend myself about this issue <img src='http://www.rubyfleebie.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /></p>

<p><a href="http://feeds.feedburner.com/~a/rubyfleebie?a=O4KkoU"><img src="http://feeds.feedburner.com/~a/rubyfleebie?i=O4KkoU" border="0"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~f/rubyfleebie?a=yK8XyCG"><img src="http://feeds.feedburner.com/~f/rubyfleebie?i=yK8XyCG" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/rubyfleebie?a=nlFt1yg"><img src="http://feeds.feedburner.com/~f/rubyfleebie?i=nlFt1yg" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/rubyfleebie?a=jD5xzcg"><img src="http://feeds.feedburner.com/~f/rubyfleebie?i=jD5xzcg" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/rubyfleebie?a=GCdyRTg"><img src="http://feeds.feedburner.com/~f/rubyfleebie?i=GCdyRTg" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/rubyfleebie?a=jijS3eG"><img src="http://feeds.feedburner.com/~f/rubyfleebie?i=jijS3eG" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/rubyfleebie?a=1F6D0nG"><img src="http://feeds.feedburner.com/~f/rubyfleebie?i=1F6D0nG" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://www.rubyfleebie.com/railers-need-to-stop-not-caring-about-the-database/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
