<?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>Thinking Digitally &#187; Ruby on Rails</title>
	<atom:link href="http://thinkingdigitally.com/category/programming/ruby/ruby-on-rails/feed/" rel="self" type="application/rss+xml" />
	<link>http://thinkingdigitally.com</link>
	<description>programming for the fun of it</description>
	<lastBuildDate>Tue, 09 Feb 2010 04:55:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Rails-doc.org is my new Rails Reference</title>
		<link>http://thinkingdigitally.com/archive/rails-docorg-is-my-new-rails-reference/</link>
		<comments>http://thinkingdigitally.com/archive/rails-docorg-is-my-new-rails-reference/#comments</comments>
		<pubDate>Fri, 04 Jul 2008 18:32:27 +0000</pubDate>
		<dc:creator>Rob Olson</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[rails-doc]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://thinkingdigitally.com/archive/rails-docorg-is-my-new-rails-reference/</guid>
		<description><![CDATA[When working with a framework as large as Ruby on Rails its necessary to have a reference close by for&#8230; well just about everything. Until recently I was a big fan of gotAPI.com because I really appreciated the Ruby and Rails reference tied together. However, the Javascript autocomplete on their search box is broken in [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://thinkingdigitally.com/wp-content/uploads/2008/07/rails-doc-logo.png" class="display-inline-right" alt="Rails-doc.org" />
When working with a framework <a href="http://api.rubyonrails.org/">as large as Ruby on Rails</a> its necessary to have a reference close by for&#8230; well just about everything. Until recently I was a big fan of <a href="http://www.gotapi.com/rubyrails">gotAPI.com</a> because I really appreciated the Ruby and Rails reference tied together. However, the Javascript autocomplete on their search box is broken in <a href="http://www.spreadfirefox.com/en-US/worldrecord/firefox3">Firefox 3</a> so I decided to try the new Rails reference site, <a href="http://rails-doc.org">Rails-doc.org</a>.</p>

<p>Rails-doc.org is an fairly ambitious project to create a community driven Rails documentation site. Basically they let users sign up and contribute notes to the existing Rails documentation. This certainly has the potential to be very useful, especially for new Rails hackers because sometimes the people who have been around the framework for a while just take things for granted.</p>

<p>Take the documentation for <a href="http://www.ruby-doc.org/core-1.8.6/classes/Date.html#M000620">strftime in the Date class</a> for example. There is no documentation listed for that method. Despite the fact that you clearly need documentation of the strftime options in order to use that method. Instead you have to know to look under <a href="http://www.ruby-doc.org/core-1.8.6/classes/Time.html#M000297">strftime in the Time class</a> for the documentation. While this is an example specific to Ruby documentation, these are the kinds of obvious problems that a community documentation website can help solve.</p>

<p>Right now Rails-doc.org has only been live for a month the so amount of community documentation feels very low. In the mean time I&#8217;ll be using the site for the official documentation that is already in place. Plus its the best looking rendering of the Rails documentation site out there. The Nodeta guys did a good job with the design. They also have a nice looking <a href="http://blog.nodeta.fi/">blog</a>.</p>

<p>It will be interested to see how many of the notes contributed to Rails-doc get ported to the official Rails documentation. This certainly feels like the easiest and most straightforward way to contribute to Rails documentation and I can see it becoming a testing ground for future contributions to the official docs.</p>
]]></content:encoded>
			<wfw:commentRss>http://thinkingdigitally.com/archive/rails-docorg-is-my-new-rails-reference/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Eager Loading with Ultrasphinx</title>
		<link>http://thinkingdigitally.com/archive/eager-loading-with-ultrasphinx/</link>
		<comments>http://thinkingdigitally.com/archive/eager-loading-with-ultrasphinx/#comments</comments>
		<pubDate>Fri, 11 Apr 2008 08:44:17 +0000</pubDate>
		<dc:creator>Rob Olson</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[ultrasphinx]]></category>

		<guid isPermaLink="false">http://thinkingdigitally.com/archive/eager-loading-with-ultrasphinx/</guid>
		<description><![CDATA[Ultrasphinx is a great Rails plugin that wraps around the Sphinx full-text search engine. I am using Ultrasphinx to handle search queries on a personal project I&#8217;m working on and I ran into a situation where I wanted to eager load associated models for my search results. The method for doing this is not well [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://blog.evanweaver.com/files/doc/fauna/ultrasphinx/files/README.html">Ultrasphinx</a> is a great Rails plugin that wraps around the <a href="http://www.sphinxsearch.com/">Sphinx full-text search engine</a>. I am using Ultrasphinx to handle search queries on a personal project I&#8217;m working on and I ran into a situation where I wanted to eager load associated models for my search results. The method for doing this is not well documented so I&#8217;m going to step through how to add eager loading to your Ultrasphinx searches.</p>

<p>I am actually going to show two ways to do this. The first is the way that <a href="http://blog.evanweaver.com/">Evan Weaver</a>, the creator of Ultrasphinx, recommends and the second way is the create a simple plugin that extends the Ultrasphinx plugin. The second way is my favorite because it provides the cleanest integration in my opinion but I&#8217;m going to demonstrate both methods so you can choose for yourself.</p>

<p><span id="more-36"></span></p>

<h2>Method 1</h2>

<p>To begin we need to make a class method that performs the eager loading in the model. Something like this:</p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">## app/models/course.rb</span>
<span style="color:#9966CC; font-weight:bold;">class</span> Course
  ...
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">find_with_includes</span><span style="color:#006600; font-weight:bold;">&#40;</span>id<span style="color:#006600; font-weight:bold;">&#41;</span>
    find<span style="color:#006600; font-weight:bold;">&#40;</span>id, <span style="color:#ff3333; font-weight:bold;">:include</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:professors</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>


<p>Notice how I&#8217;m using the familiar ActiveRecord include option to perform the eager loading like we would normally. The next and last step is to add the method to <code>client_options[:finder_methods]</code> in your <code>environment.rb</code>.</p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">## config/environment.rb  </span>
<span style="color:#6666ff; font-weight:bold;">Ultrasphinx::Search</span>.<span style="color:#9900CC;">client_options</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:finder_methods</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">unshift</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:find_with_includes</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>


<p>That&#8217;s it. Now to the second method.</p>

<h2>Method 2</h2>

<p>As I mentioned before, in this method we are going to create a simple plugin that will add functionality to Ultrasphinx. The code for this plugin was written by <a href="http://rubyforge.org/users/malesca/">Henrik N</a>.</p>

<p>Create a new folder in <code>vendor/plugins/</code> called <code>ultrasphinx_customizations</code>. The name can be anything you like as long as it starts with &#8220;ultrasphinx&#8221;. This is so our customizations are loaded after the actual Ultrasphinx plugin. Inside the folder create <code>init.rb</code> and place this line in it.</p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">## vendor/plugins/ultrasphinx_customizations/init.rb</span>
<span style="color:#CC00FF; font-weight:bold;">Dir</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">dirname</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">__FILE__</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">'/lib/*.rb'</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>file<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#CC0066; font-weight:bold;">require</span> file <span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>


<p>Now for the part of the plugin that actually does the work. Create a <code>lib</code> folder in <code>ultrasphinx_customizations</code> and create a new file in it called <code>eager_loading.rb</code> with the following contents.</p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">## vendor/plugins/ultrasphinx_customizations/lib/eager_loading.rb</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Allows specifying eager loading options as e.g.</span>
<span style="color:#008000; font-style:italic;"># Ultrasphinx::Search.client_options[:include]['MyKlass'] =&gt; [:user]</span>
<span style="color:#008000; font-style:italic;"># or directly in is_indexed like</span>
<span style="color:#008000; font-style:italic;"># is_indexed ..., :eagerly_load =&gt; [:user]</span>
&nbsp;
<span style="color:#6666ff; font-weight:bold;">Ultrasphinx::Search</span>.<span style="color:#9900CC;">client_options</span>.<span style="color:#9900CC;">merge</span>!<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">HashWithIndifferentAccess</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#123;</span>
  <span style="color:#ff3333; font-weight:bold;">:finder_methods</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'find_all_by_id_with_eager_loading'</span><span style="color:#006600; font-weight:bold;">&#93;</span>,
  <span style="color:#ff3333; font-weight:bold;">:include</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">find_all_by_id_with_eager_loading</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>ids<span style="color:#006600; font-weight:bold;">&#41;</span>
    includes = <span style="color:#6666ff; font-weight:bold;">Ultrasphinx::Search</span>.<span style="color:#9900CC;">client_options</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'include'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">name</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    args = ids <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:include</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> includes<span style="color:#006600; font-weight:bold;">&#93;</span>
    find_all_by_id<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>args<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">is_indexed_with_include_option</span><span style="color:#006600; font-weight:bold;">&#40;</span>options<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#6666ff; font-weight:bold;">Ultrasphinx::Search</span>.<span style="color:#9900CC;">client_options</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'include'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">name</span><span style="color:#006600; font-weight:bold;">&#93;</span> = options.<span style="color:#9900CC;">delete</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:eagerly_load</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    is_indexed_without_include_option<span style="color:#006600; font-weight:bold;">&#40;</span>options<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#0000FF; font-weight:bold;">self</span>; alias_method_chain <span style="color:#ff3333; font-weight:bold;">:is_indexed</span>, <span style="color:#ff3333; font-weight:bold;">:include_option</span>; <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>


<p>Sweet we&#8217;re done with the plugin. Now for the fun part. Remember in the model where we have our Ultrasphinx <code>is_indexed :fields =&gt; [...]</code> declaration? Thanks to our new customization we can add an <code>:eagerly_loaded</code> key and it will behave like an ActiveRecord find(:include => &#8230;). Here is what mine looks like.</p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">## app/models/course.rb</span>
&nbsp;
is_indexed  <span style="color:#ff3333; font-weight:bold;">:fields</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'course_number'</span>, <span style="color:#996600;">'title'</span>, <span style="color:#996600;">'term'</span><span style="color:#006600; font-weight:bold;">&#93;</span>,
            <span style="color:#ff3333; font-weight:bold;">:eagerly_load</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:professors</span><span style="color:#006600; font-weight:bold;">&#93;</span></pre></div></div>


<p>That&#8217;s all you have to do! I really enjoy the second method because it provides a seamless integration with the <code>is_indexed</code> call which seems to help me sleep at night.</p>

<p>Huge thanks to Henrik N for writing the ultrasphinx_customization code and Evan Weaver for writing Ultrasphinx.</p>

<p><strong>Updated 04/17/2008</strong>: Fixed incorrect statement about plugin names.</p>
]]></content:encoded>
			<wfw:commentRss>http://thinkingdigitally.com/archive/eager-loading-with-ultrasphinx/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Rails has a low learning curve? Hardly. You need to know Ruby</title>
		<link>http://thinkingdigitally.com/archive/rails-has-a-low-learning-curve-hardly-you-need-to-know-ruby/</link>
		<comments>http://thinkingdigitally.com/archive/rails-has-a-low-learning-curve-hardly-you-need-to-know-ruby/#comments</comments>
		<pubDate>Mon, 04 Feb 2008 04:36:16 +0000</pubDate>
		<dc:creator>Rob Olson</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://thinkingdigitally.com/archive/rails-has-a-low-learning-curve-hardly-you-need-to-know-ruby/</guid>
		<description><![CDATA[Ruby on Rails is often (incorrectly) billed as the framework that makes web development easy. Unfortunately a lot of people take this to mean &#8220;Anyone can make a web site with Rails&#8221; or &#8220;You can get started with Rails in 15 minutes&#8220;. Unfortunately neither is the case but regardless hundreds of thousands of people1 are [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://thinkingdigitally.com/wp-content/uploads/2008/02/ruby-required.png" alt="Ruby Required" /></p>

<p><a href="http://www.rubyonrails.org/">Ruby on Rails</a> is often (incorrectly) billed as the framework that makes web development easy. Unfortunately a lot of people take this to mean &#8220;<em>Anyone</em> can make a web site with Rails&#8221; or &#8220;<em>You</em> can get started with Rails in <a href="http://www.rubyonrails.org/screencasts">15 minutes</a>&#8220;. Unfortunately neither is the case but regardless hundreds of thousands of people<sup id="fnref:1"><a href="#fn:1" rel="footnote">1</a></sup> are flocking to Rails to start making a web app for everything under the sun.</p>

<p>These people learn in hurry that Rails is actually quite a large beast<sup id="fnref:2"><a href="#fn:2" rel="footnote">2</a></sup> and jump right into working with Rails without taking time to learn the programming language that Rails uses&#8230; Ruby. Almost like Ruby doesn&#8217;t exist. If I had to guess I would say people coding Rails apps without actually knowing Ruby has inevitably lead to the Rails community&#8217;s pseudo status as a <a href="http://www.google.com/search?&amp;q=rails%20is%20a%20ghetto">ghetto</a>.</p>

<p>I must confess, I was one of the Rails coders who didn&#8217;t know Ruby when I first started with Rails. Very few people who come to Rails known Ruby due to Ruby&#8217;s limited popularity before Rails came along. For about the past 7 months I have been trudging along, learning more about Ruby every day<sup id="fnref:3"><a href="#fn:3" rel="footnote">3</a></sup> and the code I write for Rails projects now is much better. Ruby programming constructs like blocks, lamba, proc, etc are fairly advanced topics that I am probably only beginning to comprehend.</p>

<p>Most people might think it is obvious that you would need to know a framework&#8217;s programming language before you started using the framework. Certainly you would not attempt J2EE development without first knowing Java. For some reason this has not always been the case with Rails.</p>

<div class="footnotes">
<hr />
<ol>

<li id="fn:1">
<p>Instant Rails has been downloaded over 400,000 times.&#160;<a href="#fnref:1" rev="footnote">&#8617;</a></p>
</li>

<li id="fn:2">
<p><a href="http://www.amazon.com/Rails-Way-Addison-Wesley-Professional-Ruby/dp/0321445619">The Rails Way</a> which contains everything a Rails developer needs to know about Rails is 912 pages.&#160;<a href="#fnref:2" rev="footnote">&#8617;</a></p>
</li>

<li id="fn:3">
<p>Largely thanks to <a href="http://projecteuler.net">Project Euler</a>.&#160;<a href="#fnref:3" rev="footnote">&#8617;</a></p>
</li>

</ol>
</div>
]]></content:encoded>
			<wfw:commentRss>http://thinkingdigitally.com/archive/rails-has-a-low-learning-curve-hardly-you-need-to-know-ruby/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
