<?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>Winn &#187; Ruby on Rails</title>
	<atom:link href="http://winn.ws/archives/tag/ruby-on-rails/feed" rel="self" type="application/rss+xml" />
	<link>http://winn.ws</link>
	<description>Standards-based design &#38; development</description>
	<lastBuildDate>Wed, 01 Feb 2012 05:30:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Stock info with ystock</title>
		<link>http://winn.ws/archives/893</link>
		<comments>http://winn.ws/archives/893#comments</comments>
		<pubDate>Mon, 14 Feb 2011 05:10:07 +0000</pubDate>
		<dc:creator>Greg Winn</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[ystock]]></category>

		<guid isPermaLink="false">http://winn.ws/?p=893</guid>
		<description><![CDATA[A new side project I have been working on is in the financial industry. This project needs to pull stock information such as; price, change, and volume of a any stock. The source i will be getting this information from is Yahoo! Finance. Yahoo produces a cvs file with just the information i need, so it's good news! The next task is to build something i can use over and over, something portable i can take from this project and implement on the next.]]></description>
			<content:encoded><![CDATA[<p><img class="codeicon" src="http://d45jz936mo8n8.cloudfront.net/wp-content/uploads/2008/02/2ndstepimage.png" alt="Rails Ystock Gem" title="Ruby on Rails"  /></p>
<h3>Gem Ystock</h3>
<p>A new side project I have been working on is in the financial industry. This project needs to pull stock information such as; price, change, and volume of a any stock. The source i will be getting this information from is Yahoo! Finance. Yahoo produces a cvs file with just the information i need, so it&#8217;s good news! The next task is to build something i can use over and over, something portable i can take from this project and implement on the next. In rails 3 plugins seem to be old news so i decided on creating a gem named &#8216;ystock&#8217; for Yahoo Stock information. (NOTE: I have no affiliation with Yahoo, this gem simply uses their service)</p>
<p><span id="more-893"></span></p>
<p>The project is hosted with github: <a href="https://github.com/gregwinn/ystock">https://github.com/gregwinn/ystock</a>, and the gem is hosted at RubyGems.org: <a href="http://rubygems.org/gems/ystock">http://rubygems.org/gems/ystock</a>. Below is how to install and use my new gem!</p>
<h4>Install</h4>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> gem <span style="color: #c20cb9; font-weight: bold;">install</span> ystock</pre></div></div>

<p>After installation you can now include this into your Gemfile</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">gem <span style="color:#996600;">'ystock'</span></pre></div></div>

<h4>Usage</h4>
<p><a href="http://github.com/gregwinn/ystock/wiki/Usage">http://github.com/gregwinn/ystock/wiki/Usage</a></p>
<p>After you have installed the gem and included it into your Gemfile you can now start to use ystock.</p>
<p><strong>1.</strong> In a controller you can do the following: </p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> SomeController <span style="color:#006600; font-weight:bold;">&lt;</span> ApplicationController
   <span style="color:#9966CC; font-weight:bold;">def</span> index
      <span style="color:#0066ff; font-weight:bold;">@stock</span> = Ystock.<span style="color:#9900CC;">get_stock</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'appl'</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><strong>2.</strong> In your view you may display the results:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;p&gt;The price of Apple stock is: $&lt;%=@stock[:price]%&gt;&lt;/p&gt;</pre></div></div>

<h4>Available data</h4>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;%=@stock[:price]%&gt;
&lt;%=@stock[:change]%&gt;
&lt;%=@stock[:volume]%&gt;</pre></div></div>

<p>Thanks for reading, i hope this gem is of use to you in any future projects!</p>
]]></content:encoded>
			<wfw:commentRss>http://winn.ws/archives/893/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Array to a hash in Ruby</title>
		<link>http://winn.ws/archives/879</link>
		<comments>http://winn.ws/archives/879#comments</comments>
		<pubDate>Sat, 29 Jan 2011 05:53:19 +0000</pubDate>
		<dc:creator>Greg Winn</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://winn.ws/?p=879</guid>
		<description><![CDATA[A WordPress MU API on Rails I have been working a WordPress MU API for the company I work for and came across a use for converting an array into a hash. The API is in Ruby on Rails, and the client that is making the request is PHP. The php script is posting an [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://d45jz936mo8n8.cloudfront.net/wp-content/uploads/2008/03/2ndstepimage.png" alt="Rails" class="codeicon" /></p>
<h3>A WordPress MU API on Rails</h3>
<p>I have been working a WordPress MU API for the company I work for and came across a use for converting an array into a hash. The API is in Ruby on Rails, and the client that is making the request is PHP. The php script is posting an array to the API witch then needs to be turned into a hash. Below is how I accomplished this.</p>
<p><span id="more-879"></span></p>
<p>The sudo php client request.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'key'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'value'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$class</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Get_some_data</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'string'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// This is posted to the API...</span></pre></div></div>

<p>Now the Ruby</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">data = <span style="color:#CC00FF; font-weight:bold;">Hash</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">*</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:string</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">flatten</span><span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#008000; font-style:italic;"># I can then call</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> data.<span style="color:#9900CC;">key</span>
<span style="color:#008000; font-style:italic;">#=&gt; value</span></pre></div></div>

<h4>You can do this to any array.</h4>
<p>Have an array in ruby that needs to be a hash?</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">a = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'apple'</span>, <span style="color:#006666;">1</span>, <span style="color:#996600;">'banana'</span>, <span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#93;</span>
h = <span style="color:#CC00FF; font-weight:bold;">Hash</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">*</span>a.<span style="color:#9900CC;">flatten</span><span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;h: #{h1.inspect}&quot;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># -----</span>
&nbsp;
a = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'apple'</span>, <span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>, <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'banana'</span>, <span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
h = <span style="color:#CC00FF; font-weight:bold;">Hash</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">*</span>a.<span style="color:#9900CC;">flatten</span><span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;h: #{h.inspect}&quot;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://winn.ws/archives/879/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Ruby and Rails Delegate</title>
		<link>http://winn.ws/archives/821</link>
		<comments>http://winn.ws/archives/821#comments</comments>
		<pubDate>Tue, 11 Jan 2011 04:27:38 +0000</pubDate>
		<dc:creator>Greg Winn</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://winn.ws/?p=821</guid>
		<description><![CDATA[Delegation is a quite common practice in Ruby projects, if you consider proxies, mix ins and composition as the ingredient of the Delegation Pattern. The Delegation Design Pattern is a technique where an object exposes certain behavior but it actually delegates responsibility for implementing that behavior to an associated object.]]></description>
			<content:encoded><![CDATA[<p><img src="http://d45jz936mo8n8.cloudfront.net/wp-content/uploads/2008/03/2ndstepimage.png" alt="" title="2ndstepimage.png" width="50" height="64" style="float:left; margin:0 .5em .5em 0;" /></p>
<p>Delegation is a quite common practice in Ruby projects, if you consider proxies, mix ins and composition as the ingredient of the Delegation Pattern. The Delegation Design Pattern is a technique where an object exposes certain behavior but it actually delegates responsibility for implementing that behavior to an associated object.</p>
<p><span id="more-821"></span></p>
<p>A really common technique is to use method_missing to intercepts the calls to undefined method, then forward the call to the right handler. However, this isn’t always a good idea. There are better ways to implement the delegation pattern in Ruby.</p>
<p>The Ruby standard library contains a Delegate module that aims to provide support for the Delegation pattern. Sadly, I found it to be way more complex than the traditional approach and I never really used it.</p>
<h3>ActiveSupport Delegate module</h3>
<p>If your project includes ActiveSupport, and every Rails project does, you have a more clean and easy way to implement the delegation pattern: the Module#delegate extension. It provides a delegate module you can use in your class or in your modules to delegate a specific method to an associate object.</p>
<p>For instance, consider a standard Post model which belongs to an User.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Post
  belongs_to <span style="color:#ff3333; font-weight:bold;">:user</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> User
  has_many <span style="color:#ff3333; font-weight:bold;">:posts</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>You might want a call to post.author to return the name of the user associated to the given post. Normally, you would create a new author method as follows</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Post
  belongs_to <span style="color:#ff3333; font-weight:bold;">:user</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> author
    <span style="color:#008000; font-style:italic;"># let's use try to bypass nil-check</span>
    user.<span style="color:#9900CC;">try</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:name</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>The same code expressed using the delegate method.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Post
  belongs_to <span style="color:#ff3333; font-weight:bold;">:user</span>
  delegate <span style="color:#ff3333; font-weight:bold;">:author</span>, <span style="color:#ff3333; font-weight:bold;">:to</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> user, <span style="color:#ff3333; font-weight:bold;">:allow_nil</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>The delegate method can be used in any context, it’s not limited to ActiveRecord models. For example, your custom queue wrapper can delegate to the internal queue implementation some specific methods.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> QueueManager
&nbsp;
  attr_accessor <span style="color:#ff3333; font-weight:bold;">:queue</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># Delegates some methods to the internal queue</span>
  delegate <span style="color:#ff3333; font-weight:bold;">:size</span>, <span style="color:#ff3333; font-weight:bold;">:clear</span>, <span style="color:#ff3333; font-weight:bold;">:to</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:queue</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> intialize
    <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">queue</span> = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
m = QueueManager.<span style="color:#9900CC;">new</span>
m.<span style="color:#9900CC;">size</span>
<span style="color:#008000; font-style:italic;"># =&gt; 0</span>
m.<span style="color:#9900CC;">clear</span>
<span style="color:#008000; font-style:italic;"># =&gt; []</span></pre></div></div>

<p>Methods can be delegated to instance variables, class variables, or constants by providing them as a symbols. At least one method and the :to option are required.</p>
<h3>Options</h3>
<p>The delegate method understand some additional options, useful to customize the behavior.</p>
<p>This is my favorite option. The :prefix can be set to true to prefix the delegate method with the name of the object being delegated to. You can also provide a custom prefix.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Post
  belongs_to <span style="color:#ff3333; font-weight:bold;">:user</span>
&nbsp;
  delegate <span style="color:#ff3333; font-weight:bold;">:name</span>, <span style="color:#ff3333; font-weight:bold;">:to</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:user</span>, <span style="color:#ff3333; font-weight:bold;">:prefix</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span>
  <span style="color:#008000; font-style:italic;"># post.user_name</span>
&nbsp;
  delegate <span style="color:#ff3333; font-weight:bold;">:name</span>, <span style="color:#ff3333; font-weight:bold;">:to</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:user</span>, <span style="color:#ff3333; font-weight:bold;">:prefix</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;author&quot;</span>
  <span style="color:#008000; font-style:italic;"># post.author_name</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>The :allow_nil option allows the class to delegate the method to an object that might be nil. In this case, a call to the delegated method will return nil. The default behavior is to raise a NoMethodError.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Post
  belongs_to <span style="color:#ff3333; font-weight:bold;">:user</span>
  delegate <span style="color:#ff3333; font-weight:bold;">:name</span>, <span style="color:#ff3333; font-weight:bold;">:to</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:user</span>, <span style="color:#ff3333; font-weight:bold;">:prefix</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
Post.<span style="color:#9900CC;">new</span>.<span style="color:#9900CC;">user_name</span>
<span style="color:#008000; font-style:italic;"># raise NoMethodError</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> Post
  belongs_to <span style="color:#ff3333; font-weight:bold;">:user</span>
  delegate <span style="color:#ff3333; font-weight:bold;">:name</span>, <span style="color:#ff3333; font-weight:bold;">:to</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:user</span>, <span style="color:#ff3333; font-weight:bold;">:prefix</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span>, <span style="color:#ff3333; font-weight:bold;">:allow_nil</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
Post.<span style="color:#9900CC;">new</span>.<span style="color:#9900CC;">user_name</span>
<span style="color:#008000; font-style:italic;"># =&gt; nil</span></pre></div></div>

<p>The :to it a non-option, because it’s mandatory. <a href="https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/module/delegation.rb">Check out the documentation.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://winn.ws/archives/821/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>4 ways to use Symbols in Ruby</title>
		<link>http://winn.ws/archives/769</link>
		<comments>http://winn.ws/archives/769#comments</comments>
		<pubDate>Sat, 11 Dec 2010 00:00:38 +0000</pubDate>
		<dc:creator>Greg Winn</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://winn.ws/?p=769</guid>
		<description><![CDATA[A typical implementation of a Ruby interpreter maintains a symbol table in which it stores the names of all the classes, methods, and variables it knows about. This also allows such an interpreter to avoid most string comparisons: it refers to a method name by their position in this symbol table. This turns a relatively expensive string operation into a relatively cheap integer operation.]]></description>
			<content:encoded><![CDATA[<p><img src="http://d45jz936mo8n8.cloudfront.net/wp-content/uploads/2008/02/2ndstepimage.png" alt="" title="Ruby on Rails" width="50" height="64" style="float:left; margin:0 8px 8px 0;" />A typical implementation of a Ruby interpreter maintains a symbol table in which it stores the names of all the classes, methods, and variables it knows about. This also allows such an interpreter to avoid most string comparisons: it refers to a method name by their position in this symbol table. This turns a relatively expensive string operation into a relatively cheap integer operation.</p>
<p><span id="more-769"></span></p>
<p>These symbols are not purely internal to the interpreter; they can also be used by Ruby programs. A <em>Symbol</em> object refers to a symbol. A symbol literal is written by prefixing an identifier or string with a colon.</p>
<h3>One</h3>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#ff3333; font-weight:bold;">:symbol</span> <span style="color:#008000; font-style:italic;">#A Symbol literal</span></pre></div></div>

<h3>Two</h3>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">:<span style="color:#996600;">&quot;symbol&quot;</span> <span style="color:#008000; font-style:italic;">#The Same literal</span></pre></div></div>

<h3>Three</h3>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">:<span style="color:#996600;">'a very long symbol'</span> <span style="color:#008000; font-style:italic;">#Quotes are best used for symbols with spaces</span></pre></div></div>

<h3>Four</h3>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">s = <span style="color:#996600;">&quot;string&quot;</span>
:<span style="color:#996600;">&quot;#{s}&quot;</span> <span style="color:#008000; font-style:italic;"># The symbol :string</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://winn.ws/archives/769/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Simple DB</title>
		<link>http://winn.ws/archives/748</link>
		<comments>http://winn.ws/archives/748#comments</comments>
		<pubDate>Sun, 21 Nov 2010 16:44:21 +0000</pubDate>
		<dc:creator>Greg Winn</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[AWS]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://winn.ws/?p=748</guid>
		<description><![CDATA[Amazon SimpleDB is a web service for storing, maintaining, and querying structured data sets in real time. All data is stored in Amazon's web service cloud, making SimpleDB very reliable, scalable, and flexible. I am going to go over all you need to start using Simple DB with Ruby on Rails.]]></description>
			<content:encoded><![CDATA[<p><img style="float:left; margin:0 8px 8px 0;" src="http://d45jz936mo8n8.cloudfront.net/wp-content/uploads/2008/03/2ndstepimage.png" alt="" title="2ndstepimage.png" width="50" height="64" class="alignnone size-full wp-image-675" /></p>
<p>Amazon SimpleDB is a web service for storing, maintaining, and querying structured data sets in real time. All data is stored in Amazon&#8217;s web service cloud, making SimpleDB very reliable, scalable, and flexible. I am going to go over all you need to start using Simple DB with Ruby on Rails.</p>
<p><span id="more-748"></span></p>
<p>The Rails web application framework, apart from generally being a wonderful tool, offers out-of-the-box support for web service-based data stores with its ActiveResource sub-framework. Only a very thin adapter layer is necessary to bridge the ActiveResource API to SimpleDB. Rails gives you the unique opportunity to utilize SimpleDB just as any other RESTful resource provided by a Rails application.</p>
<h3>Getting Setup</h3>
<ol>
<li><a href="htp://aws.amazon.com/">Sign up for an Amazon AWS Account.</a></li>
<li>Install the aws-sdb Ruby Gem providing a basic Ruby API for SimpleDB:
<pre>sudo gem install aws-sdb</pre>
</li>
<li>Install the AWS SDB Proxy Plugin for Rails:
<pre>script/plugin install http://rug-b.rubyforge.org/svn/aws_sdb_proxy</pre>
</li>
<li>Enter your Amazon Web Service credentials in the config/aws_sdb_proxy.yml file (optionally configure server ports and an individual salt used to generate primary keys with a hash function). Do this at least for the development environment.</li>
<li>Either use an existing SimpleDB domain in your account (you can list your domains with rake aws_sdb:list_domains), or create a new one with rake aws_sdb:create_domain DOMAIN=my_new_domain</li>
<li>Start the AWS SDB Proxy server: rake aws_sdb:start_proxy_in_foreground proving debug output on stdout (once you are confident with the configuration you can use rake aws_sdb:start_proxy to start the server as a background daemon)</li>
</ol>
<p><strong>NOTE: </strong>You may have an error:
<pre>wrong number of arguments (3 for 1) </pre>
<p>. The AWS plugin needs a little help so lets edit one of the files. Locate &#8220;vendor/plugins/aws_sdb_proxy/lib/aws_sdb_proxy/server.rb&#8221; now look for:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">SDB_SERVICE = <span style="color:#6666ff; font-weight:bold;">AwsSdb::Service</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">Logger</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">nil</span><span style="color:#006600; font-weight:bold;">&#41;</span>,
                                           CONFIG<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'aws_access_key_id'</span><span style="color:#006600; font-weight:bold;">&#93;</span>,
                                           CONFIG<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'aws_secret_access_key'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p> and replace it with:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">SDB_SERVICE = <span style="color:#6666ff; font-weight:bold;">AwsSdb::Service</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>:access_key_id<span style="color:#006600; font-weight:bold;">=&gt;</span>CONFIG<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'aws_access_key_id'</span><span style="color:#006600; font-weight:bold;">&#93;</span>,
                  <span style="color:#ff3333; font-weight:bold;">:secret_access_key</span><span style="color:#006600; font-weight:bold;">=&gt;</span>CONFIG<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'aws_secret_access_key'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

</p>
<p>Once you fix this issue you can now start the server/create your domain without error.</p>
<h3>Using Simple DB</h3>
<p>To make a Rails model access SimpleDB, it must inherit from ActiveResource::Base. For the following examples we will use a Logging model, thus create the following models/logging.rb file:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Logging <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveResource::Base</span>
  <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">site</span>   = <span style="color:#996600;">&quot;http://localhost:8888&quot;</span> <span style="color:#008000; font-style:italic;"># Proxy host + port</span>
  <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">prefix</span> = <span style="color:#996600;">&quot;/my_new_domain/&quot;</span>       <span style="color:#008000; font-style:italic;"># SDB domain</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Now we can create new records by simply:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">Logging.<span style="color:#9900CC;">create</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:user_id</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0066ff; font-weight:bold;">@user</span>.<span style="color:#9900CC;">id</span>, <span style="color:#ff3333; font-weight:bold;">:action</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;Some action&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>Now query for the records</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">log = Logging.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:all</span>, <span style="color:#ff3333; font-weight:bold;">:attributes</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span>:user_id <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0066ff; font-weight:bold;">@user</span>.<span style="color:#9900CC;">id</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>Update the record</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">log = Logging.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span>log.<span style="color:#9900CC;">id</span><span style="color:#006600; font-weight:bold;">&#41;</span>
log.<span style="color:#9900CC;">action</span> = <span style="color:#996600;">&quot;Update this action&quot;</span>
log.<span style="color:#9900CC;">save</span></pre></div></div>

<p>You can read more about this on the Amazon site at <a href="http://aws.amazon.com/articles/1242">http://aws.amazon.com/articles/1242</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://winn.ws/archives/748/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Install Passenger</title>
		<link>http://winn.ws/archives/734</link>
		<comments>http://winn.ws/archives/734#comments</comments>
		<pubDate>Thu, 21 Oct 2010 19:06:57 +0000</pubDate>
		<dc:creator>Greg Winn</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Home Page]]></category>
		<category><![CDATA[Mac]]></category>
		<category><![CDATA[mod_rails]]></category>
		<category><![CDATA[Passenger]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://winn.ws/?p=734</guid>
		<description><![CDATA[Passenger makes deploying rails apps as easy as putting the file on the server and starting Apache. I have been using passenger for about 4 years and have used it for some very high scale projects.]]></description>
			<content:encoded><![CDATA[<p><img style="float:left; margin:0 8px 8px 0;" src="http://d45jz936mo8n8.cloudfront.net/wp-content/uploads/2010/10/mod_rails.jpg" alt="" title="mod_rails" width="196" height="63" class="alignnone size-full wp-image-737" /></p>
<p>Passenger makes deploying rails apps as easy as putting the file on the server and starting Apache. I have been using passenger for about 4 years and have used it for some very high scale projects. Passenger makes deployments very php like, what i mean by that is you can just drop the files and go. From my experience using Passenger the only time Apache need to be recycled is when adding a new controller or model. Adding actions inside an already existing controller Apache will not need to be restarted, you can simply start using that action.</p>
<p><span id="more-734"></span></p>
<p>The next big seller for me is the documentation on this product it’s been around for some time so there is a large community around “mod_rails / Passenger”. Passenger works on any POSIX-compliant operating system. In other words: practically any operating system on earth, except Microsoft Windows.</p>
<p>Passenger was super quick and easy to install and get a rails application running. Below i have detailed my installation on Mac OSX 10.6.4.</p>
<p><strong>1) First I opened a terminal window and ran the command:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">sudo gem install passenger</pre></div></div>

<p><em>(Important note just remember to sudo).</em></p>
<p><strong>2) Now lets install the apache2 module:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">sudo passenger-install-apache2-module</pre></div></div>

<p>After running the apache2-module command it will tell you what you can expect from the install process, just hit “Return”.</p>
<p><strong>3) Now lets setup your Apache, the install will provide you with three lines that need to go into your Apache config document:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">LoadModule passenger_module /Library/Ruby/Gems/1.8/gems/passenger-3.0.0/ext/apache2/mod_passenger.so
PassengerRoot /Library/Ruby/Gems/1.8/gems/passenger-3.0.0
PassengerRuby /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby</pre></div></div>

<p><strong>NOTE: Don&#8217;t copy and paste mine, be sure to copy the output from the module install in your terminal window.</strong></p>
<p>After restarting Apache you want to check and make sure AllowOverrides are set to all. Now create your rails project or use an existing one.</p>
<p><strong>4) Now we need to setup a VirtualHost in apache, open the httpd-vhosts.conf file and add this:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">&lt;VirtualHost *:80&gt;
	DocumentRoot &quot;/var/www/path/to/your/rails/app&quot;
	ServerName local.YourTestDomain.com
	RailsEnv development # &lt;- important to set this here.
&lt;/VirtualHost&gt;</pre></div></div>

<p>Now restart apache once again and then edit your hosts file adding the local domain, once this has been done you can visit your local domain in a browser and you should now see your rail application running. No need to visit “localhost:3000” any more. Any time you create a new controller or model just restart Apache and reload the page.</p>
<p style="text-align:center;"><img src="http://d45jz936mo8n8.cloudfront.net/wp-content/uploads/2010/10/address_bar_url.png" alt="" title="address_bar_url" width="277" height="39" class="alignnone size-full wp-image-740" /></p>
<p>I hope this has helped someone install this great product, i think you will find it very useful in the future for production rails app’s and just at home development. <a href="http://www.modrails.com/index.html">Read more about mod_rails / Passenger here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://winn.ws/archives/734/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Using remote_function</title>
		<link>http://winn.ws/archives/171</link>
		<comments>http://winn.ws/archives/171#comments</comments>
		<pubDate>Mon, 20 Oct 2008 16:00:37 +0000</pubDate>
		<dc:creator>Greg Winn</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://www.winn.ws/?p=171</guid>
		<description><![CDATA[This wonderful built-in function is a great way to make easy Ajax calls. In this example I will show you how to use remote_function with the onClick call, and also within a JavaScript function. First, lets make a checkbox called finish; this link will send the id of the item to the database to be [...]]]></description>
			<content:encoded><![CDATA[<p>This wonderful built-in function is a great way to make easy Ajax calls. In this example I will show you how to use remote_function with the onClick call, and also within a JavaScript function.</p>
<p>First, lets make a checkbox called finish; this link will send the id of the item to the database to be updated. The item will be a todo and we will be using the list.html.erb and the todo_controller.rb.</p>
<p>In the view named: list.html.erb</p>
<pre class="ruby">
<code>
remote_function(:url => {:action => 'finish', :controller => 'todo',
:id => todo.id})
</code>
</pre>
<p>Now once the checkbox is clicked it will them make the Ajax call to the todo_controller.rb with the method finish.</p>
<p>In the controller: todo_controller.rb</p>
<pre class="ruby">
<code>
def finish
	@todo = Todo.update(params[:id], :status => '1')
	if @todo.save
		render :update do |page|
		page.replace_html 'thedivID' + params[:id], 'Finished!'
		end
	end
end
</code>
</pre>
<p>The above code will first update the database setting the status from 0 to 1 (1 = finished and 0 = unfinished). Next we check if the update was successful, after that we render the update to the page. By using render :update, we can replace the html in side of the div or other element with the text ‘Finished’.</p>
<p><em>Note you can use remote_function in any way you see fit, ie: onclick, onload and others.</em></p>
<p>Passing another var with remote_function. lets say i want to pass the user id, list id and, the todo id. How would we do this with the remote function?</p>
<p>We know know the link should look like this: /todo/finish/17?todoid=72&#038;userid=100, but how do we do this with rails? Below is how to do just that;</p>
<pre class="ruby">
<code>
remote_function(:url => {:action => 'finish', :controller => 'todo',
:id => list.id, :todoid => todo.id, :userid => @user.id})
</code>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://winn.ws/archives/171/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Winn Scriptatype: 1.7</title>
		<link>http://winn.ws/archives/169</link>
		<comments>http://winn.ws/archives/169#comments</comments>
		<pubDate>Wed, 15 Oct 2008 02:17:58 +0000</pubDate>
		<dc:creator>Greg Winn</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[WinnScriptatype]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[prototype]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://www.winn.ws/?p=169</guid>
		<description><![CDATA[I wanted to re-cap the new adds in 1.7 for rails. The new method calls are as listed: # Added V1.7b textlink_do_fade textlink_do_puff textlink_do_blindup textlink_do_dropout You can build any of the above calls with: textlink_do_fade&#40;'Link Text','divYouWantToUpdate',&#123;***&#125;,&#123;****&#125;&#41; Visit the project for more info on how to use the latest methods and more. Winn Scriptatype project home [...]]]></description>
			<content:encoded><![CDATA[<p>I wanted to re-cap the new adds in 1.7 for rails. The new method calls are as listed:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># Added V1.7b</span>
textlink_do_fade
textlink_do_puff
textlink_do_blindup
textlink_do_dropout</pre></div></div>

<p>You can build any of the above calls with:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">textlink_do_fade<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'Link Text'</span>,<span style="color:#996600;">'divYouWantToUpdate'</span>,<span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">***</span><span style="color:#006600; font-weight:bold;">&#125;</span>,<span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">****</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>Visit the project for more info on how to use the latest methods and more. <a href="http://rubyonrailsrailway.com/winnscriptatype">Winn Scriptatype project home page.</a> at <a href="http://rubyonrailsrailway.com/">Ruby On Rails Railway</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://winn.ws/archives/169/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WinnScriptatype:Rails v1.7b</title>
		<link>http://winn.ws/archives/135</link>
		<comments>http://winn.ws/archives/135#comments</comments>
		<pubDate>Fri, 04 Apr 2008 03:18:55 +0000</pubDate>
		<dc:creator>Greg Winn</dc:creator>
				<category><![CDATA[WinnScriptatype]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[plug-in]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://www.winn.ws/archives/135</guid>
		<description><![CDATA[I just released WinnScriptatype version 1.7 beta on the RubyForge project page. I have also made some fixes to version 1.5 and it is now production ready. The latest release has added some new functionality and the ability to make calls to display text. The call still supports the effects and Ajax request url along [...]]]></description>
			<content:encoded><![CDATA[<p>I just released WinnScriptatype version 1.7 beta on the RubyForge project page. I have also made some fixes to version 1.5 and it is now production ready. The latest release has added some new functionality and the ability to make calls to display text. The call still supports the effects and Ajax request url along with url options.</p>
<p>[ruby]<br />
# New calls in 1.7b<br />
textlink_do_fade<br />
textlink_do_puff<br />
textlink_do_blindup<br />
textlink_do_dropout<br />
[/ruby]</p>
<p>I expect i will be making the same update to the PHP version soon so keep an eye out for that!</p>
<p>Take a look at the <a href="http://rubyforge.org/projects/winnscriptatype/">WinnScriptatype:Rails RubyForge project page</a> and download the latest!</p>
]]></content:encoded>
			<wfw:commentRss>http://winn.ws/archives/135/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rails Plugin: WinnScriptatype</title>
		<link>http://winn.ws/archives/129</link>
		<comments>http://winn.ws/archives/129#comments</comments>
		<pubDate>Thu, 27 Mar 2008 02:45:00 +0000</pubDate>
		<dc:creator>Greg Winn</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[WinnScriptatype]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[plug-in]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://www.winn.ws/archives/129</guid>
		<description><![CDATA[Making an Ajax checkbox just got easy. This very simple plugin was built for my own use. The idea was to make one call for my checkbox, update to the database, and an effect. This plugin is easy to use and install, i dont have it setup in an svn yet so you will need [...]]]></description>
			<content:encoded><![CDATA[<p>Making an Ajax checkbox just got easy. This very simple plugin was built for my own use. The idea was to make one call for my checkbox, update to the database, and an effect. This plugin is easy to use and install, i dont have it setup in an svn yet so you will need to download it and place it in RAILS_ROOT/vendor/plugins/HERE.</p>
<p>I had been working on this plugin for a few weeks in making a new application. I kept writing the &#8220;input, Ajax.Request and, Effect.Fade&#8221;, so i needed a way to make that fast and do what i need. And out came this handy plugin!</p>
<p>[ruby]<br />
# Lets use this new plugin<br />
checkbox_do_fade(&#8216;theDiv&#8217;, &#8216;CheckboxID&#8217;, {:controller => &#8216;todo&#8217;, :action => &#8216;finish&#8217;, :id => todo.id}, {:userid => user.id})</p>
<p>[/ruby]</p>
<h4><strong>Here is an over view of the options:</strong></h4>
<p>[ruby]<br />
checkbox_do_fade(&#8216;*&#8217;, &#8216;**&#8217;, {***}, {****})</p>
<p>[Element ID]* This is the name of the element you want to effect such as a div or table (tr,td)&#8230;</p>
<p>[Checkbox ID]** This is the id of the checkbox it self</p>
<p>[URL/path]*** this is the url for the update to take place NOTE: always use controller,<br />
              action, and id DO NOT place other variables in this block that is for the next block.</p>
<p>[URL/path options]**** This block is used for url options like so: (/path/to/1?userid=1) as you see<br />
                       userid is 1, this is done by using the url options block. You may send an unlimited number of<br />
                       url options with this.</p>
<p># Below are the ONLY supported calls<br />
checkbox_do_fade # This will fade the element<br />
checkbox_do_puff # This will puff the element<br />
checkbox_do_blindup # and so on&#8230;<br />
checkbox_do_dropout # again&#8230;<br />
[/ruby]</p>
<p>Ready to start using it? <a href="http://svn.winn.ws/winn_scriptatype.zip">Download WinnScriptatype</a></p>
<p>You will also need help installing this plugin so take a look at <a href="http://rubyonrailsrailway.com/winnscriptatype">Ruby on Rails Railway : WinnScriptatype</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://winn.ws/archives/129/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Database Caching 2/46 queries in 0.032 seconds using disk: basic
Object Caching 608/712 objects using disk: basic
Content Delivery Network via Amazon Web Services: CloudFront: d45jz936mo8n8.cloudfront.net

Served from: winn.ws @ 2012-02-05 06:03:32 -->
