<?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>matthewcarriere.com &#187; matthew</title>
	<atom:link href="http://matthewcarriere.com/author/admin/feed/" rel="self" type="application/rss+xml" />
	<link>http://matthewcarriere.com</link>
	<description>a blog about startups, tech, and development.</description>
	<lastBuildDate>Sat, 12 May 2012 04:31:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>[FATAL] failed to allocate memory</title>
		<link>http://matthewcarriere.com/2012/02/13/fatal-failed-to-allocate-memory/</link>
		<comments>http://matthewcarriere.com/2012/02/13/fatal-failed-to-allocate-memory/#comments</comments>
		<pubDate>Tue, 14 Feb 2012 00:04:10 +0000</pubDate>
		<dc:creator>matthew</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://matthewcarriere.com/?p=133</guid>
		<description><![CDATA[I got this error today on my iMac. It was incredibly frustrating and I tried all of the following to fix it (the reason I list them is because I&#8217;m not entirely sure the final step I&#8217;ll mention at the end was the sole solution). 1. uninstall mysql2 gem (gem uninstall mysql2) 2. uninstall mysql [...]]]></description>
			<content:encoded><![CDATA[<p>I got this error today on my iMac. It was incredibly frustrating and I tried all of the following to fix it (the reason I list them is because I&#8217;m not entirely sure the final step I&#8217;ll mention at the end was the sole solution).</p>
<p>1. uninstall mysql2 gem (gem uninstall mysql2)<br />
2. uninstall mysql on homebrew (brew uninstall mysql)<br />
3. uninstall mysql-connector-c on homebrew (brew uninstall mysql-connector-c)<br />
4. reinstall mysql on homebrew (brew install mysql)<br />
5. reinstall mysql-connector-c on homebrew (brew install mysql-connector-c)<br />
6. reinstall mysql2 gem (gem install mysql2)</p>
<p>After all that I still got the error&#8230; at this point I was about to set my hair on fire, but then I noticed I had receieved a difference error message in my logs&#8230;</p>
<p>ActionView::Template::Error (dyld: Library not loaded: /opt/local/lib/libssl.1.0.0.dylib<br />
  Referenced from: /usr/local/bin/node<br />
  Reason: image not found</p>
<p>Do you see that reference to node? WTF!</p>
<p>So then I did the following:</p>
<p>1. uninstall node on homebrew (brew uninstall node)<br />
2. reinstall node on homebrew (brew install node)</p>
<p>WOOHOO! It worked again! My hair is safe and I can stand down from Defcon 1. Why did this happen? I have no idea. Hopefully this helps someone else and please let me know in the comments if you know why this happened.</p>
]]></content:encoded>
			<wfw:commentRss>http://matthewcarriere.com/2012/02/13/fatal-failed-to-allocate-memory/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>using text_field_with_auto_complete and form_authenticity_token</title>
		<link>http://matthewcarriere.com/2008/08/13/using-text_field_with_auto_complete-and-form_authenticity_token/</link>
		<comments>http://matthewcarriere.com/2008/08/13/using-text_field_with_auto_complete-and-form_authenticity_token/#comments</comments>
		<pubDate>Thu, 14 Aug 2008 06:14:39 +0000</pubDate>
		<dc:creator>matthew</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://matthewcarriere.com/?p=31</guid>
		<description><![CDATA[Rails 2.0 introduced protection for Cross-site request forgery (CSRF). This is turned on by default in your Rails applications. However, many developers are turning off this valuable protection rather than making the changes necessary to use it. This seems the same to me as leaving your door unlocked while your friend visits because cutting keys [...]]]></description>
			<content:encoded><![CDATA[<p>Rails 2.0 introduced protection for <a href="http://en.wikipedia.org/wiki/Cross-site_request_forgery">Cross-site request forgery (CSRF)</a>. This is turned on by default in your Rails applications. However, many developers are turning off this valuable protection rather than making the changes necessary to use it. This seems the same to me as leaving your door unlocked while your friend visits because cutting keys is hard. Case in point: text_field_with_auto_complete</p>
<p>You know you have run into the new CSRF protection when you see this error in your Rails application:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#6666ff; font-weight:bold;">ActionController::InvalidAuthenticityToken</span></pre></div></div>

<p>This is the point where you Google the error and receive one of the following pieces of advice:</p>
<h4>1. Turn off RequestForgeryProtection all together</h4>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># add the following line to your application.rb or one of your</span>
<span style="color:#008000; font-style:italic;"># environment.rb files. why not production? (sarcasm)</span>
config.<span style="color:#9900CC;">action_controller</span>.<span style="color:#9900CC;">allow_forgery_protection</span> = <span style="color:#0000FF; font-weight:bold;">false</span></pre></div></div>

<h4>2. Turn off RequestForgeryProtection for just one controller</h4>

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

<h4>3. Turn off RequestForgeryProtection for just one action.</h4>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">protect_from_forgery <span style="color:#ff3333; font-weight:bold;">:except</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#ff3333; font-weight:bold;">:attack_me</span></pre></div></div>

<p>So let&#8217;s see how easy it is to utilize this new feature!</p>
<p>I was working with the text_field_with_auto_complete in a pretty basic form:</p>
<p>For those of you who aren&#8217;t familiar with this there is a nice post on the <a href="http://wiki.rubyonrails.com/rails/pages/How+to+use+text_field_with_auto_complete">Rails Wiki</a></p>
<p>When I tried to perform my auto complete I received the <em>ActionController::InvalidAuthenticityToken</em> error.</p>
<p>Why? Because basically my Ajax request did not include a security token that RequestForgeryProtection uses to validate my request is coming from the right place. All you need to do is add this token to the parameters sent with your request. Rails makes this easy with the helper: form_authenticity_token. The token name RequestForgeryProtection will be looking for is: authenticity_token.</p>
<p>So let&#8217;s add this to our text_field_with_auto_complete:</p>
<p>Now when I performed my auto complete it worked flawlessly! Now to refactor that string&#8230; any suggestions?</p>
<h4>Update August 25, 2008</h4>
<p>Learned two things working with this&#8230;</p>
<p>1. It&#8217;s automatically doing a POST, so by changing the method to GET I can avoid having to deal with the authenticity_token.</p>
<p>2. And with that out of the way I don&#8217;t need to specify the parameters since by default it will send back the value of the text field.</p>
<p>That&#8217;s a lot cleaner!</p>
]]></content:encoded>
			<wfw:commentRss>http://matthewcarriere.com/2008/08/13/using-text_field_with_auto_complete-and-form_authenticity_token/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Learning Ruby on Rails with Heroku Episode 1</title>
		<link>http://matthewcarriere.com/2008/06/30/learning-ruby-on-rails-with-heroku-episode-1/</link>
		<comments>http://matthewcarriere.com/2008/06/30/learning-ruby-on-rails-with-heroku-episode-1/#comments</comments>
		<pubDate>Tue, 01 Jul 2008 06:10:32 +0000</pubDate>
		<dc:creator>matthew</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[heroku]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[screencast]]></category>

		<guid isPermaLink="false">http://matthewcarriere.com/?p=28</guid>
		<description><![CDATA[Ever since I saw Heroku I wanted to use it for a screencast on learning Ruby on Rails. This weekend I finished the very first episode! This episode is fairly light on Ruby on Rails content because I was so focused on screencasting and learning all about video and audio production for the web. So [...]]]></description>
			<content:encoded><![CDATA[<p>Ever since I saw <a href="http://heroku.com">Heroku</a> I wanted to use it for a screencast on learning Ruby on Rails. This weekend I finished the very first episode! This episode is fairly light on Ruby on Rails content because I was so focused on screencasting and learning all about video and audio production for the web.</p>
<p>So take a look and tell me what you think. I used <a href="http://vimeo.com">Vimeo</a> for hosting the video. I also produced the video in HD&#8230; however I didn&#8217;t realize that Vimeo only streams the HD content on their site, not the embedded video. So to view the best possible video you should view it on Vimeo. <a href="http://www.vimeo.com/1256684">here is the link</a>. You can make it full screen and enjoy the HD in all its glory.</p>
<p>Please feel free to leave some <strong>constructive</strong> criticism regarding the screencast. This way I can make some changes before I start pushing this out more regularly.</p>
<p>In this first episode of Learning Ruby on Rails with Heroku:</p>
<ol>
<li>Introduction</li>
<li>Goal of this screencast series</li>
<li>What is Heroku?</li>
<li>Getting an account</li>
<li>Creating your first Ruby on Rails application</li>
<li>Basic navigation of Heroku</li>
</ol>
<p><object width="640" height="360"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://www.vimeo.com/moogaloop.swf?clip_id=1256684&amp;server=www.vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=00ADEF&amp;fullscreen=1" /><embed src="http://www.vimeo.com/moogaloop.swf?clip_id=1256684&amp;server=www.vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=00ADEF&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="640" height="360"></embed></object><br /><a href="http://www.vimeo.com/1256684?pg=embed&#038;sec=1256684">Learning Ruby on Rails with Heroku Episode 1</a> from <a href="http://www.vimeo.com/carriere?pg=embed&#038;sec=1256684">Matthew Carriere</a> on <a href="http://vimeo.com?pg=embed&#038;sec=1256684">Vimeo</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://matthewcarriere.com/2008/06/30/learning-ruby-on-rails-with-heroku-episode-1/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Using select, reject, collect, inject and detect.</title>
		<link>http://matthewcarriere.com/2008/06/23/using-select-reject-collect-inject-and-detect/</link>
		<comments>http://matthewcarriere.com/2008/06/23/using-select-reject-collect-inject-and-detect/#comments</comments>
		<pubDate>Tue, 24 Jun 2008 06:08:16 +0000</pubDate>
		<dc:creator>matthew</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[iterator]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://matthewcarriere.com/?p=26</guid>
		<description><![CDATA[I spend a lot of time convincing my friends to switch to a Mac. Some of my friends are also software developers so naturally, just when they think the evangelism has come to an end, I convince them to get on the Rails. However, learning Rails usually means learning Ruby for the first time as [...]]]></description>
			<content:encoded><![CDATA[<p>I spend a lot of time convincing my friends to switch to a Mac. Some of my friends are also software developers so naturally, just when they think the evangelism has come to an end, I convince them to get on the Rails. However, learning Rails usually means learning Ruby for the first time as well. In this post I am going to address one of the issues I see for newcomers to Ruby. Looping.</p>
<p>Looping in Ruby seems to be a process of evolution for newcomers to the language. Newcomers will always find their way to the for loop almost immediately and when confronted with iterating an array, the first choice will generally be a for..in:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">a = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span>,<span style="color:#006666;">2</span>,<span style="color:#006666;">3</span>,<span style="color:#006666;">4</span><span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#9966CC; font-weight:bold;">for</span> n <span style="color:#9966CC; font-weight:bold;">in</span> a
  <span style="color:#CC0066; font-weight:bold;">puts</span> n
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>This works, but its not very&#8230; Ruby. The next stage of evolution will be using an iterator for the first time. So the for loop gets dropped all together and <em>each</em> is used. The Rubyist is born at this point:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">a.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>n<span style="color:#006600; font-weight:bold;">|</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> n
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>What I see next is a lot of conditional logic being used inside the each block. The logic is generally introduced to perform the following operations:</p>
<ol>
<li>Building a list of items from an array.</li>
<li>Total the items in an array.</li>
<li>Find an item in the array.</li>
</ol>
<p>So if this is you, then stop. Ruby has plenty more iterators where <em>each</em> came from. Which one you should be using depends on what operation you are trying to perform. So let&#8217;s take a look at our previous list and see if we can find a more Ruby way to get them done.</p>
<h4>Building a list of items from the array using select</h4>
<p>For this operation you should be using <em>select</em>. The way select works is simple, it basically iterates through all the elements in your array and performs your logic on each one. If the logic returns TRUE, then it adds the item to a new array which it returns when the iteration is complete. Here&#8217;s an example:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">a = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span>,<span style="color:#006666;">2</span>,<span style="color:#006666;">3</span>,<span style="color:#006666;">4</span><span style="color:#006600; font-weight:bold;">&#93;</span>
a.<span style="color:#CC0066; font-weight:bold;">select</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>n<span style="color:#006600; font-weight:bold;">|</span> n <span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#125;</span></pre></td></tr></table></div>

<p>This will return the last two elements in the array: 3 and 4. Why? Because 3 and 4 are both greater than 2, which was the logic we placed in the block. It&#8217;s worth noting that select has an evil step sister named <em>reject</em>. This will perform the opposite operation of select. Logic which returns FALSE adds the item to the array that is returned. Here&#8217;s the same examples as before except we will swap select, with reject:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">a = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span>,<span style="color:#006666;">2</span>,<span style="color:#006666;">3</span>,<span style="color:#006666;">4</span><span style="color:#006600; font-weight:bold;">&#93;</span>
a.<span style="color:#9900CC;">reject</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>n<span style="color:#006600; font-weight:bold;">|</span> n <span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#125;</span></pre></td></tr></table></div>

<p>In this example the return value is [1,2] because these elements return false when the condition is tested.</p>
<p>I also have to mention another close sibling to select and reject; collect, which returns an array of values that are the RESULT of logic in the block. Previously we returned the item based on the result of the CONDITION in the block. So perhaps we need square the values in our array:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">a = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span>,<span style="color:#006666;">2</span>,<span style="color:#006666;">3</span>,<span style="color:#006666;">4</span><span style="color:#006600; font-weight:bold;">&#93;</span>
a.<span style="color:#9900CC;">collect</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>n<span style="color:#006600; font-weight:bold;">|</span> n<span style="color:#006600; font-weight:bold;">*</span>n<span style="color:#006600; font-weight:bold;">&#125;</span></pre></td></tr></table></div>

<p>This returns a new array with each item in our array squared.</p>
<p>Finally, note that using select, reject, and collect returns an array. If you want to return something different, because you are concatenating or totaling values, then check out inject.</p>
<h4>Total the items in an array using inject</h4>
<p>When you think of accumulating, concatenating, or totaling values in an array, then think of inject. The main difference between select and inject is that inject gives you another variable for use in the block. This variable, referred to as the accumulator, is used to store the running total or concatenation of each iteration. The value added to the accumulator is the result of the logic you place in the block. At the end of each iteration, whatever that value is, can be added to the accumulator. For example, let&#8217;s sum all the numbers together in our array:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">a = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span>,<span style="color:#006666;">2</span>,<span style="color:#006666;">3</span>,<span style="color:#006666;">4</span><span style="color:#006600; font-weight:bold;">&#93;</span>
a.<span style="color:#9900CC;">inject</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>acc,n<span style="color:#006600; font-weight:bold;">|</span> acc <span style="color:#006600; font-weight:bold;">+</span> n<span style="color:#006600; font-weight:bold;">&#125;</span></pre></td></tr></table></div>

<p>This will return 10. The total value of all the elements in our array. The logic in our block is simple: add the current element to the accumulator. Remember, you must do <em>something</em> to the accumulator in each iteration. If we had simply placed <em>n</em> in the block the final value of the accumulator would have been 4. Why? Because its the last value in the array and since we did not add it to the accumulator explicitly the accumulator would be replaced in each iteration.</p>
<p>You can also use a parameter with the inject call to determine what the default value for the accumulator is:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">a = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span>,<span style="color:#006666;">2</span>,<span style="color:#006666;">3</span>,<span style="color:#006666;">4</span><span style="color:#006600; font-weight:bold;">&#93;</span>
a.<span style="color:#9900CC;">inject</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">10</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>acc,n<span style="color:#006600; font-weight:bold;">|</span> acc <span style="color:#006600; font-weight:bold;">+</span> n<span style="color:#006600; font-weight:bold;">&#125;</span></pre></td></tr></table></div>

<p>In this example the result is 20 because we assigned the accumulator an initial value of 10.</p>
<p>If you need to return a string or an array from inject, then you will need to treat the accumulator variable that way. You can use the default value parameter of inject to do this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">a = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span>,<span style="color:#006666;">2</span>,<span style="color:#006666;">3</span>,<span style="color:#006666;">4</span><span style="color:#006600; font-weight:bold;">&#93;</span>
a.<span style="color:#9900CC;">inject</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>acc,n<span style="color:#006600; font-weight:bold;">|</span> acc <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> n<span style="color:#006600; font-weight:bold;">+</span>n<span style="color:#006600; font-weight:bold;">&#125;</span></pre></td></tr></table></div>

<p>In this example I add n to itself and then append it to the accumulator variable. I initialized the accumulator as an empty array using the default value parameter.</p>
<h4>Find an item in the array using detect</h4>
<p>Our last example operation was to find an element in the array. Let&#8217;s just put it out there and say that other iterators could be used to <em>select</em> the correct value from the array, but I am going to show you how to use detect to round out our exploration of these iterators. </p>
<p>So let&#8217;s find the value 3 in our array using detect:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">a = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span>,<span style="color:#006666;">2</span>,<span style="color:#006666;">3</span>,<span style="color:#006666;">4</span><span style="color:#006600; font-weight:bold;">&#93;</span>
a.<span style="color:#9900CC;">detect</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>n<span style="color:#006600; font-weight:bold;">|</span> n == <span style="color:#006666;">3</span><span style="color:#006600; font-weight:bold;">&#125;</span></pre></td></tr></table></div>

<p>This returns 3. The value we were looking for. If the value had not been found, then the iterator returns <em>nil</em>.</p>
<p>So if your head is spinning at this point as to which iterator to use for when, then remember this:</p>
<ol>
<li>Use select or reject if you need to select or reject items based on a condition.</li>
<li>Use collect if you need to build an array of the results from logic in the block.</li>
<li>Use inject if you need to accumulate, total, or concatenate array values together.</li>
<li>Use detect if you need to find an item in an array.</li>
</ol>
<p>By using these iterators you will be one step closer to mastering&#8230; Ruby-Fu.</p>
]]></content:encoded>
			<wfw:commentRss>http://matthewcarriere.com/2008/06/23/using-select-reject-collect-inject-and-detect/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>Installing MySQL on Mac OS X Leopard using MacPorts</title>
		<link>http://matthewcarriere.com/2008/04/02/installing-mysql-on-mac-os-x-leopard-using-macports/</link>
		<comments>http://matthewcarriere.com/2008/04/02/installing-mysql-on-mac-os-x-leopard-using-macports/#comments</comments>
		<pubDate>Thu, 03 Apr 2008 06:05:49 +0000</pubDate>
		<dc:creator>matthew</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[macports]]></category>
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://matthewcarriere.com/?p=24</guid>
		<description><![CDATA[There comes a time in every Rails developers life that they need to work with MySQL. Often this means that your development comes to a screeching halt as you wade through the various options for getting MySQL installed on your Mac. This guide won&#8217;t help you install it anywhere else&#8230; because I am a zealot [...]]]></description>
			<content:encoded><![CDATA[<p>There comes a time in every Rails developers life that they need to work with MySQL. Often this means that your development comes to a screeching halt as you wade through the various options for getting MySQL installed on your Mac. </p>
<blockquote><p>This guide won&#8217;t help you install it anywhere else&#8230; because I am a zealot and I don&#8217;t care how to install it anywhere else.</p></blockquote>
<p>Install MySQL using macports, go get a coffee while this runs its course.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">sudo port install mysql5 +<span style="color: #000066;">server</span></pre></td></tr></table></div>

<p>Next up, create the initial databases used by MySQL.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">sudo <span style="color: #66cc66;">/</span>opt<span style="color: #66cc66;">/</span>local<span style="color: #66cc66;">/</span>lib<span style="color: #66cc66;">/</span>mysql5<span style="color: #66cc66;">/</span>bin<span style="color: #66cc66;">/</span>mysql_install_db --<span style="color: #000066;">user</span>=mysql</pre></td></tr></table></div>

<p>Configure MySQL to start at system start up.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">sudo launchctl load -w <span style="color: #66cc66;">/</span>Library<span style="color: #66cc66;">/</span>LaunchDaemons<span style="color: #66cc66;">/</span>org.macports.mysql5.plist</pre></td></tr></table></div>

<p>Start MySQL.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">sudo <span style="color: #66cc66;">/</span>opt<span style="color: #66cc66;">/</span>local<span style="color: #66cc66;">/</span>bin<span style="color: #66cc66;">/</span>mysqld_safe5 <span style="color: #66cc66;">&amp;</span></pre></td></tr></table></div>

<p>You can always stop the service manually as well:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">mysqladmin5 -u root -p shutdown</pre></td></tr></table></div>

<p>Confirm that that MySQL is running.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">mysqladmin5 -u root -p ping</pre></td></tr></table></div>

<p><em>Just hit enter at the password prompt, as it is blank. We are about to change that.</em></p>
<p>Secure your server.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">mysqladmin5 -u root password <span style="color: #66cc66;">&#91;</span>your password goes here<span style="color: #66cc66;">&#93;</span></pre></td></tr></table></div>

<p>At this point you should have your MySQL server up and running. However, your Rails project may complain about not being able to find the socket file. You can find that using the &#8216;status&#8217; command.</p>
<p>First log into MySQL.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">mysql5 -u root -p</pre></td></tr></table></div>

<p>At the prompt, run the command.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">status;</pre></td></tr></table></div>

<p>In the output you will see an entry listing the socket being used by MySQL. You can use this in the database.yml file.</p>
<p>And finally, I really don&#8217;t like typing that &#8217;5&#8242; after all the commands&#8230; I can&#8217;t explain it, it just upsets me. So I like to create a symbolic link for the mysql5 and mysqladmin5 commands.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">sudo ln -s <span style="color: #66cc66;">/</span>opt<span style="color: #66cc66;">/</span>local<span style="color: #66cc66;">/</span>bin<span style="color: #66cc66;">/</span>mysql5 <span style="color: #66cc66;">/</span>opt<span style="color: #66cc66;">/</span>local<span style="color: #66cc66;">/</span>bin<span style="color: #66cc66;">/</span>mysql
sudo ln -s <span style="color: #66cc66;">/</span>opt<span style="color: #66cc66;">/</span>local<span style="color: #66cc66;">/</span>bin<span style="color: #66cc66;">/</span>mysqladmin5 <span style="color: #66cc66;">/</span>opt<span style="color: #66cc66;">/</span>local<span style="color: #66cc66;">/</span>bin<span style="color: #66cc66;">/</span>mysqladmin</pre></td></tr></table></div>

<p>That should get you coding again against a MySQL database!</p>
]]></content:encoded>
			<wfw:commentRss>http://matthewcarriere.com/2008/04/02/installing-mysql-on-mac-os-x-leopard-using-macports/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Learn to use your new Mac like a pro</title>
		<link>http://matthewcarriere.com/2008/02/03/learn-to-use-your-new-mac-like-a-pro/</link>
		<comments>http://matthewcarriere.com/2008/02/03/learn-to-use-your-new-mac-like-a-pro/#comments</comments>
		<pubDate>Mon, 04 Feb 2008 06:03:46 +0000</pubDate>
		<dc:creator>matthew</dc:creator>
				<category><![CDATA[Mac]]></category>

		<guid isPermaLink="false">http://matthewcarriere.com/?p=21</guid>
		<description><![CDATA[Have you just switched to a Mac? Or have you switched sometime ago and still have those &#8220;how do I do this on a mac?&#8221; moments&#8230; well then this is for you! Apple has recently added a &#8220;Find out how&#8221; section to its website with videos on common computing tasks that should help your transition [...]]]></description>
			<content:encoded><![CDATA[<p>Have you just switched to a Mac? Or have you switched sometime ago and still have those &#8220;how do I do this on a mac?&#8221; moments&#8230; well then this is  for you! Apple has recently added a &#8220;Find out how&#8221; section to its website with videos on common computing tasks that should help your transition from the dark side.</p>
<p><a href="http://www.apple.com/findouthow/macosx">Find out how</a></p>
]]></content:encoded>
			<wfw:commentRss>http://matthewcarriere.com/2008/02/03/learn-to-use-your-new-mac-like-a-pro/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Getting Started with SQLite</title>
		<link>http://matthewcarriere.com/2008/01/30/getting-started-with-sqlite/</link>
		<comments>http://matthewcarriere.com/2008/01/30/getting-started-with-sqlite/#comments</comments>
		<pubDate>Thu, 31 Jan 2008 05:57:45 +0000</pubDate>
		<dc:creator>matthew</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[sqlite]]></category>

		<guid isPermaLink="false">http://matthewcarriere.com/?p=17</guid>
		<description><![CDATA[SQLite is an extremely lightweight (dare I say agile) database. It uses files and doesn&#8217;t require you to start and stop services or spend a great deal of time configuring it. SQLite will be a good choice if you have a load under 100,000 requests and few concurrent writes. If you have a simple site [...]]]></description>
			<content:encoded><![CDATA[<p>SQLite is an extremely lightweight (dare I say agile) database. It uses files and doesn&#8217;t require you to start and stop services or spend a great deal of time configuring it. SQLite will be a good choice if you have a load under 100,000 requests and few concurrent writes. If you have a simple site that does nothing but serve content, then SQLite may just fit the bill.</p>
<p>Now that Ruby on Rails is using SQLite as its default database here are some tips for working with it.</p>
<p>Right off, you will need a Ruby SQLite adapter if you intend to work with SQLite at all, so let&#8217;s get that installed first:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">sudo gem install sqlite3-ruby</pre></td></tr></table></div>

<p>Next up we need a database, and with SQLite all we need to do is pass a filename when we fire up SQLite:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="io" style="font-family:monospace;"><span style="color: #000066;">sqlite</span> test.db</pre></td></tr></table></div>

<p>At this point the database will operate similar to MySQL. You are sitting at a command prompt and can enter various SQL commands to create tables, perform crud operations etc.</p>
<p>When you are done with the database quit using the following command:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">.quit</pre></td></tr></table></div>

<p>Another handy command to take a look at is:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">.help</pre></td></tr></table></div>

<p>This will produce a list of commands you can use in SQLite. For example, trying to find the equivalent of <em>show databases</em> in SQLite?</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">.databases</pre></td></tr></table></div>

<p>If you are not at all interested in working with SQLite or you know ahead of time it won&#8217;t fit the bill then let Rails know when you create your application:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">rails -d mysql my_app</pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://matthewcarriere.com/2008/01/30/getting-started-with-sqlite/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>Take it to the next level with the Ruby on Rails console</title>
		<link>http://matthewcarriere.com/2008/01/27/take-it-to-the-next-level-with-the-ruby-on-rails-console/</link>
		<comments>http://matthewcarriere.com/2008/01/27/take-it-to-the-next-level-with-the-ruby-on-rails-console/#comments</comments>
		<pubDate>Mon, 28 Jan 2008 05:53:19 +0000</pubDate>
		<dc:creator>matthew</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[console]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://matthewcarriere.com/?p=14</guid>
		<description><![CDATA[If you are not using the Rails console while your developing then now is the time to start! I learn best by example, so that&#8217;s how I am going to structure this. Here&#8217;s the scenario: You are writing code against a class and need to determine which attributes you have available to you. In Rails, [...]]]></description>
			<content:encoded><![CDATA[<p>If you are not using the Rails console while your developing then now is the time to start! I learn best by example, so that&#8217;s how I am going to structure this.</p>
<p>Here&#8217;s the scenario:</p>
<p>You are writing code against a class and need to determine which attributes you have available to you. In Rails, your attributes are largely driven by the database schema, so you will not see them in the class definition. At this point most developers will take a look at the table in the database and find the column they need, then determine the type of data stored there. There is a better way&#8230;</p>
<p>Let&#8217;s use the command line.</p>
<p>We start by firing up our Rails application in console mode:<br />
<em>You must be in your Rails application directory to issue this command.</em></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">script<span style="color: #66cc66;">/</span>console</pre></td></tr></table></div>

<p>So now what? Well now you have command line access to your entire Rails application! For example, let&#8217;s say you have a class named Person (which would map to a people table in the database). You want a new instance of this class? Then issue the following:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">p</span> = Person.<span style="color:#9900CC;">new</span></pre></td></tr></table></div>

<p>Perhaps you want to view a Person from the database:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">p</span> = Person.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></td></tr></table></div>

<p>By default the results are displayed below each command and they are not very friendly, so let&#8217;s output them in YAML as it will be much more readable:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">y <span style="color:#CC0066; font-weight:bold;">p</span></pre></td></tr></table></div>

<p>&#8216;y&#8217; is like calling &#8216;puts p.to_yaml&#8217; on the resulting value that follows it. There is also a &#8216;p&#8217; shortcut that is the equivalent of issuing &#8216;puts&#8217; on the value.</p>
<p>So how does this help with determining attributes? At first glance you may think you have to look at the list of values returned in your YAML command and find what your looking for&#8230; no&#8230; it gets better. Remember that all Ruby objects inherit certain methods, watch this:</p>
<p>You want to get the methods associated with your object:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">y <span style="color:#CC0066; font-weight:bold;">p</span>.<span style="color:#9900CC;">methods</span></pre></td></tr></table></div>

<p>You can also get the private, public, and protected methods using the following commands:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">y <span style="color:#CC0066; font-weight:bold;">p</span>.<span style="color:#9900CC;">public_methods</span>
y <span style="color:#CC0066; font-weight:bold;">p</span>.<span style="color:#9900CC;">private_methods</span>
y <span style="color:#CC0066; font-weight:bold;">p</span>.<span style="color:#9900CC;">protected_methods</span></pre></td></tr></table></div>

<p>So this class could return a huge YAML result which may not seem as efficient as scanning the schema in the database, and you&#8217;re right. So let&#8217;s get to what we are really looking for using &#8216;grep&#8217;:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">y <span style="color:#CC0066; font-weight:bold;">p</span>.<span style="color:#9900CC;">methods</span>.<span style="color:#9900CC;">grep</span> <span style="color:#006600; font-weight:bold;">/</span>method_name<span style="color:#006600; font-weight:bold;">/</span></pre></td></tr></table></div>

<p>This will return any method which contains <em>method_name</em>. You can write pass any regular expression you like into this command and really drill into your class methods, and for help putting those regular expressions together I really like using <a href="http://www.rubular.com/">Rubular</a> a web-based Ruby regular expression editor.</p>
<p>And what about the type of an object?</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">y <span style="color:#CC0066; font-weight:bold;">p</span>.<span style="color:#9966CC; font-weight:bold;">class</span></pre></td></tr></table></div>

<p>This is just a taste of what you can do with the console&#8230; I use the console everyday as I develop. I will post some more console tips in future posts!</p>
]]></content:encoded>
			<wfw:commentRss>http://matthewcarriere.com/2008/01/27/take-it-to-the-next-level-with-the-ruby-on-rails-console/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Running Ruby on Rails with Mongrel Clustering and Apache Proxy</title>
		<link>http://matthewcarriere.com/2008/01/18/running-ruby-on-rails-with-mongrel-clustering-and-apache-proxy/</link>
		<comments>http://matthewcarriere.com/2008/01/18/running-ruby-on-rails-with-mongrel-clustering-and-apache-proxy/#comments</comments>
		<pubDate>Sat, 19 Jan 2008 05:42:52 +0000</pubDate>
		<dc:creator>matthew</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[mongrel]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[rubygems]]></category>

		<guid isPermaLink="false">http://matthewcarriere.com/?p=6</guid>
		<description><![CDATA[Being a Rails coder is easy, deploying a Rails application can be hard. Through a great deal of effort, we&#8217;ll call it blood, sweat and tears, I have put together the following guidance on how to get your Ruby on Rails application up and running behind a Mongrel cluster using Apache Proxy. Here&#8217;s the deal, [...]]]></description>
			<content:encoded><![CDATA[<p>Being a Rails coder is easy, deploying a Rails application can be hard. Through a great deal of effort, we&#8217;ll call it blood, sweat and tears, I have put together the following guidance on how to get your Ruby on Rails application up and running behind a Mongrel cluster using Apache Proxy.</p>
<p>Here&#8217;s the deal, I am going to install all the necessary applications to get this up and running, so if you have some of these installed on your server already you may just skip ahead, however, if you have any issues with this you may want to read over those sections you skipped and understand why I installed a specific version or what pitfalls I encountered that may be affecting your setup. Also, I am running Ubuntu, so if you are running a different distribution, then the paths to specific configuration files on your system may be different. Let&#8217;s get started!</p>
<h4>Installing MySQL</h4>
<p>Install MySQL using the package manager in Ubuntu:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">sudo apt-get install mysql-<span style="color: #000066;">server</span></pre></td></tr></table></div>

<p>After the installation you should be able to run the command <em>mysql</em> or <em>mysqladmin</em>. If you cannot run these then you may have to create symbolic links to these commands or add them to your path.</p>
<p>By default, MySQL only listens on localhost, in order to change that you need to comment out the line which reads: bind-address = 127.0.0.1</p>
<p>Change this by editing the following file on your system:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">sudo vi <span style="color: #66cc66;">/</span>etc<span style="color: #66cc66;">/</span>mysql<span style="color: #66cc66;">/</span>my.cnf</pre></td></tr></table></div>

<p>You can search for text in <em>vi</em> using a forward slash.</p>
<p>You should restart MySQL after making a change to the config file:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="io" style="font-family:monospace;"><span style="color: #66cc66;">/</span>etc<span style="color: #66cc66;">/</span>init.d<span style="color: #66cc66;">/</span>mysql restart</pre></td></tr></table></div>

<p>Now verify that MySQL is listening to the network. The output of the following command should show an entry for MySQL:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">netstat -tap</pre></td></tr></table></div>

<p>You need to set a password for the root account of MySQL, otherwise anyone can access it!</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">mysqladmin -u root password your_password
mysqladmin -h server_name -u root password your_password</pre></td></tr></table></div>

<p>In case it is not clear, <em>your_password</em> should be replaced with a strong password of your choosing.</p>
<h4>Installing Ruby</h4>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">apt-get install ruby ri irb</pre></td></tr></table></div>

<h4>Install Ruby Gems</h4>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">cd <span style="color: #66cc66;">/</span>tmp
wget http:<span style="color: #808080; font-style: italic;">//rubyforge.org/frs/download.php/17190/rubygems-0.9.2.tgz</span>
tar -zxvf rubygems-0.9.2.tgz
cd <span style="color: #66cc66;">/</span>rubygems-0.9.2.tgz
ruby setup.rb</pre></td></tr></table></div>

<p>Update rubygems</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">gem update --<span style="color: #000000; font-weight: bold;">system</span>
gem update
gem cleanup</pre></td></tr></table></div>

<p>Why the two calls to gem update? &#8211;system will update the RubyGems itself and without that parameter you will only be updating the gems.</p>
<h4>Install Rails</h4>
<p>Install Rails using Rubygems:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">gem install rails</pre></td></tr></table></div>

<p>Now let&#8217;s test the rails installation:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">rails <span style="color: #66cc66;">/</span>tmp<span style="color: #66cc66;">/</span>railstest
cd <span style="color: #66cc66;">/</span>tmp<span style="color: #66cc66;">/</span>railstest
.<span style="color: #66cc66;">/</span>script<span style="color: #66cc66;">/</span><span style="color: #000066;">server</span></pre></td></tr></table></div>

<p>This will start the default WeBrick web server, so open a web browser to http://localhost:3000 If all is well you will see the Ruby on Rails welcome screen. Once you are finished staring at your success, press CTRL + d to kill the web server.</p>
<h4>Install Mongrel</h4>
<p>Install the build tools:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">apt-get install build-essential ruby1.8-dev</pre></td></tr></table></div>

<p>Install Mongrel:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">gem install mongrel mongrel_cluster --include-dependencies</pre></td></tr></table></div>

<p>Now let&#8217;s test the Mongrel installation:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">cd <span style="color: #66cc66;">/</span>tmp<span style="color: #66cc66;">/</span>railstest
mongrel_rails start</pre></td></tr></table></div>

<p>This will start the Mongrel web server (now default), so open a web browser to http://localhost:3000 If all is well you will see the Ruby on Rails welcome screen&#8230; again. Now kill the web server.</p>
<h4>Configure Mongrel Rails cluster</h4>
<p>Add a user for our mongrel cluster:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">useradd mongrel</pre></td></tr></table></div>

<p>Add the group for our mongrel user:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">groupadd mongrel</pre></td></tr></table></div>

<p>Configure the directory security for your application:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">chown -R mongrel:mongrel <span style="color: #66cc66;">/</span>tmp<span style="color: #66cc66;">/</span>railstest</pre></td></tr></table></div>

<p>Now we need to configure the Rails cluster for the test application. This command will write a YAML file to the config directory in your Rails application so you will only have to enter all these parameters once. After that, simply firing the command <em>mongrel_rails cluster::start</em> will use this configuration:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">cd <span style="color: #66cc66;">/</span>tmp<span style="color: #66cc66;">/</span>railstest
mongrel_rails cluster::configure -e production \
-p <span style="color: #cc66cc;">8000</span> -N <span style="color: #cc66cc;">8</span> --<span style="color: #000066;">user</span> mongrel --group mongrel</pre></td></tr></table></div>

<p>You can reconfigure by running this command and using different parameters, you can also safely delete the YAML file from your config directory to blow these settings away.</p>
<p>Time to start the cluster:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">cd <span style="color: #66cc66;">/</span>tmp<span style="color: #66cc66;">/</span>railstest
mongrel_rails cluster::start</pre></td></tr></table></div>

<p>You should be able to test your application by going to http://localhost:8000. You could also hit any of the 8 ports you have configured.</p>
<h4>Install Apache</h4>
<p>We have come to the last part of this lengthy installation. Apache. Now, it must be understood that you MUST install Apache 2.2 or higher. The reason for this is the Apache Proxy Balance Module. This module is required to glue all this together and is ONLY available in Apache 2.2 or higher. I am going to build from source on this one because the current version available to me in the apt-get library is not the sufficient version required.</p>
<p>Download Apache, unpack it, and then change to the directory which contains the unpacked files.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">wget http:<span style="color: #808080; font-style: italic;">//archive.apache.org/dist/httpd/httpd-2.2.3.tar.gz</span>
tar xvfz httpd-2.2.3.tar.gz
cd httpd-2.2.3</pre></td></tr></table></div>

<p>Configure the source tree for compiling Apache on your system:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">.<span style="color: #66cc66;">/</span>configure --prefix=<span style="color: #66cc66;">/</span>usr<span style="color: #66cc66;">/</span>local<span style="color: #66cc66;">/</span>apache2 \
--enable-mods-shared=all \
--disable-deflate \
--enable-proxy \
--enable-proxy-balancer \
--enable-proxy-http</pre></td></tr></table></div>

<p>Build and install using the following commands:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">make
sudo make install</pre></td></tr></table></div>

<p>Start Apache and navigate to your server through a browser. You should see the default Apache homepage.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">sudo <span style="color: #66cc66;">/</span>usr<span style="color: #66cc66;">/</span>local<span style="color: #66cc66;">/</span>apache2<span style="color: #66cc66;">/</span>bin<span style="color: #66cc66;">/</span>apachectl start</pre></td></tr></table></div>

<p>There are several options for configuring and securing Apache that should be considered before taking this setup into production.</p>
<p>In order to configure Apache to serve your Mongrel cluster you will need to add the following to the Apache conf file:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
</pre></td><td class="code"><pre class="io" style="font-family:monospace;"><span style="color: #66cc66;">&lt;</span>Proxy balancer:<span style="color: #808080; font-style: italic;">//mongrel_cluster&gt;</span>
BalancerMember http:<span style="color: #808080; font-style: italic;">//127.0.0.1:8000</span>
BalancerMember http:<span style="color: #808080; font-style: italic;">//127.0.0.1:8001</span>
BalancerMember http:<span style="color: #808080; font-style: italic;">//127.0.0.1:8002</span>
BalancerMember http:<span style="color: #808080; font-style: italic;">//127.0.0.1:8003</span>
BalancerMember http:<span style="color: #808080; font-style: italic;">//127.0.0.1:8004</span>
BalancerMember http:<span style="color: #808080; font-style: italic;">//127.0.0.1:8005</span>
BalancerMember http:<span style="color: #808080; font-style: italic;">//127.0.0.1:8006</span>
BalancerMember http:<span style="color: #808080; font-style: italic;">//127.0.0.1:8007</span>
<span style="color: #66cc66;">&lt;/</span>Proxy<span style="color: #66cc66;">&gt;</span>
&nbsp;
<span style="color: #66cc66;">&lt;</span>VirtualHost yourdomain.com:<span style="color: #cc66cc;">80</span><span style="color: #66cc66;">&gt;</span>
ServerAdmin yourname<span style="color: #66cc66;">@</span>yourdomain.com
ServerName  www.yourdomain.com
ServerAlias yourdomain.com
&nbsp;
<span style="color: #808080; font-style: italic;"># Indexes + Directory Root.</span>
DirectoryIndex test.html
DocumentRoot <span style="color: #66cc66;">/</span>tmp<span style="color: #66cc66;">/</span>railstest<span style="color: #66cc66;">/</span>public<span style="color: #66cc66;">/</span>
&nbsp;
<span style="color: #66cc66;">&lt;</span>Directory <span style="color: #ff0000;">&quot;/tmp/railstest/public&quot;</span><span style="color: #66cc66;">&gt;</span>
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
<span style="color: #66cc66;">&lt;/</span>Directory<span style="color: #66cc66;">&gt;</span>
&nbsp;
RewriteEngine On
RewriteCond <span style="color: #66cc66;">%</span><span style="color: #66cc66;">&#123;</span>HTTP_HOST<span style="color: #66cc66;">&#125;</span> ^yourdomain\.com$ <span style="color: #66cc66;">&#91;</span>NC<span style="color: #66cc66;">&#93;</span>
RewriteRule ^<span style="color: #66cc66;">&#40;</span>.<span style="color: #66cc66;">*</span><span style="color: #66cc66;">&#41;</span>$ http:<span style="color: #808080; font-style: italic;">//www. yourdomain.com$1 [R=301,L]</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Check for maintenance file and redirect all requests</span>
RewriteCond <span style="color: #66cc66;">%</span><span style="color: #66cc66;">&#123;</span>DOCUMENT_ROOT<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">system</span><span style="color: #66cc66;">/</span>maintenance.html -f
RewriteCond <span style="color: #66cc66;">%</span><span style="color: #66cc66;">&#123;</span>SCRIPT_FILENAME<span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">!</span>maintenance.html
RewriteRule ^.<span style="color: #66cc66;">*</span>$ <span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">system</span><span style="color: #66cc66;">/</span>maintenance.html <span style="color: #66cc66;">&#91;</span>L<span style="color: #66cc66;">&#93;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Rewrite index to check for static</span>
RewriteRule ^<span style="color: #66cc66;">/</span>$ <span style="color: #66cc66;">/</span>index.html <span style="color: #66cc66;">&#91;</span>QSA<span style="color: #66cc66;">&#93;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Rewrite to check for Rails cached page</span>
RewriteRule ^<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span>^.<span style="color: #66cc66;">&#93;</span>+<span style="color: #66cc66;">&#41;</span>$ $1.html <span style="color: #66cc66;">&#91;</span>QSA<span style="color: #66cc66;">&#93;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Redirect all non-static requests to cluster</span>
RewriteCond <span style="color: #66cc66;">%</span><span style="color: #66cc66;">&#123;</span>DOCUMENT_ROOT<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">/%</span><span style="color: #66cc66;">&#123;</span>REQUEST_FILENAME<span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">!</span>-f
RewriteRule ^<span style="color: #66cc66;">/</span><span style="color: #66cc66;">&#40;</span>.<span style="color: #66cc66;">*</span><span style="color: #66cc66;">&#41;</span>$ balancer:<span style="color: #808080; font-style: italic;">//mongrel_cluster%{REQUEST_URI} [P,QSA,L]</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Logfiles</span>
ErrorLog  <span style="color: #66cc66;">/</span>tmp<span style="color: #66cc66;">/</span>railstest<span style="color: #66cc66;">/</span>log<span style="color: #66cc66;">/</span><span style="color: #000066;">error</span>.log
CustomLog <span style="color: #66cc66;">/</span>tmp<span style="color: #66cc66;">/</span>railstest<span style="color: #66cc66;">/</span>access.log combined
<span style="color: #66cc66;">&lt;/</span>VirtualHost<span style="color: #66cc66;">&gt;</span>
&nbsp;
NameVirtualHost yourdomain.com:<span style="color: #cc66cc;">80</span></pre></td></tr></table></div>

<p>Now, there are 3 pieces to take note of here. First, you need to create a Proxy group for the mongrel cluster we created. Since we created 8 nodes, there are 8 BalancerMembers. Second, you will need a VirtualHost entry for your Rails site (in this case the railstest site we created earlier). And Finally, you have to add the NameVirtualHost entry for your VirtualHost (I can&#8217;t tell you how many times I forget that).</p>
<p>That should get you going with a Mongrel Cluster behind Apache Proxy. If you want to setup more than one site, then just create 3 more entries in your Apache conf file. You may want to think about separating the declarations from the main httpd.conf file, but for this example I just wanted to get you going. Have Fun!</p>
]]></content:encoded>
			<wfw:commentRss>http://matthewcarriere.com/2008/01/18/running-ruby-on-rails-with-mongrel-clustering-and-apache-proxy/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>

