<?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/"
	xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>All About Ruby</title>
	<atom:link href="http://allaboutruby.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://allaboutruby.wordpress.com</link>
	<description>Ruby newbie view on the web development using Ruby on Rails</description>
	<lastBuildDate>Sat, 31 Oct 2009 12:03:26 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='allaboutruby.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/c0a74344adcab0ac66574e63cc56e2b3?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>All About Ruby</title>
		<link>http://allaboutruby.wordpress.com</link>
	</image>
			<item>
		<title>Rails caching &#8211; resources</title>
		<link>http://allaboutruby.wordpress.com/2009/10/31/rails-caching-resources/</link>
		<comments>http://allaboutruby.wordpress.com/2009/10/31/rails-caching-resources/#comments</comments>
		<pubDate>Sat, 31 Oct 2009 12:02:01 +0000</pubDate>
		<dc:creator>allaboutruby</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[rails caching]]></category>
		<category><![CDATA[rails performance]]></category>
		<category><![CDATA[ror caching]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[ruby on rails performance]]></category>

		<guid isPermaLink="false">http://allaboutruby.wordpress.com/?p=199</guid>
		<description><![CDATA[When you create anything serious on Ruby on Rails, you will most definitely need to start thinking about caching. As Ruby is a high-level language, a lot of crunching goes on to make the magic work. So in order for all this magic to work you should ensure that the heavy-lifting is done only once [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=allaboutruby.wordpress.com&blog=56213&post=199&subd=allaboutruby&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>When you create anything serious on Ruby on Rails, you will most definitely need to start thinking about caching. As Ruby is a high-level language, a lot of crunching goes on to make the magic work. So in order for all this magic to work you should ensure that the heavy-lifting is done only once and after that you just use the results.</p>
<p>Good news is that caching in RoR is very powerful standard feature. </p>
<p>When should you start thinking about caching? It definitely pays off to know the proper ways to cache in RoR before you start coding anything serious. However, the optimization could be done at some later point, analyzing which queries, screens and views give you the most trouble.</p>
<p>Here&#8217;s how you should start with caching:</p>
<p>1 &#8211; <a href="http://railscasts.com/episodes?page=1&amp;search=caching">View all the railscasts.com caching tutorials first</a>. Although Ryan Bates does not go into details on the theory behind the caching, the ability to see a real-life example and get an easy explanation is priceless.</p>
<p>2 &#8211; After that read the <a href="http://guides.rubyonrails.org/caching_with_rails.html">&#8220;official&#8221; introduction to caching</a> on the RoR site. You might want to skip some parts at the first reading. </p>
<p>3 &#8211; Finally, you should invest your time in the <a href="http://railslab.newrelic.com/scaling-rails">advanced video casts on Rails scaling</a> by true experts NewRelic. These courses are made free for all to use &#8211; perfect!</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/allaboutruby.wordpress.com/199/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/allaboutruby.wordpress.com/199/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/allaboutruby.wordpress.com/199/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/allaboutruby.wordpress.com/199/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/allaboutruby.wordpress.com/199/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/allaboutruby.wordpress.com/199/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/allaboutruby.wordpress.com/199/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/allaboutruby.wordpress.com/199/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/allaboutruby.wordpress.com/199/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/allaboutruby.wordpress.com/199/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=allaboutruby.wordpress.com&blog=56213&post=199&subd=allaboutruby&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://allaboutruby.wordpress.com/2009/10/31/rails-caching-resources/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6a3304c2fbc5e2e67e8c6b0a1d9715d2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">allaboutruby</media:title>
		</media:content>
	</item>
		<item>
		<title>Ruby variables</title>
		<link>http://allaboutruby.wordpress.com/2009/09/24/ruby-variables/</link>
		<comments>http://allaboutruby.wordpress.com/2009/09/24/ruby-variables/#comments</comments>
		<pubDate>Thu, 24 Sep 2009 19:17:19 +0000</pubDate>
		<dc:creator>allaboutruby</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[object cloning]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[variable reference to object]]></category>

		<guid isPermaLink="false">http://allaboutruby.wordpress.com/?p=194</guid>
		<description><![CDATA[Something that you might have to watch out for when working with Ruby is how it works with variables. Ruby variables are only references (pointers) to objects. Therefore, you might experience a subtle behavior that you probably don&#8217;t expect. Try this out in your irb:

irb(main):001:0&#62; a = &#34;some text&#34;
=&#62; &#34;some text&#34;
irb(main):002:0&#62; b = a
=&#62; &#34;some [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=allaboutruby.wordpress.com&blog=56213&post=194&subd=allaboutruby&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Something that you might have to watch out for when working with Ruby is how it works with variables. Ruby variables are only references (pointers) to objects. Therefore, you might experience a subtle behavior that you probably don&#8217;t expect. Try this out in your irb:</p>
<pre class="brush: plain;">
irb(main):001:0&gt; a = &quot;some text&quot;
=&gt; &quot;some text&quot;
irb(main):002:0&gt; b = a
=&gt; &quot;some text&quot;
irb(main):003:0&gt; a.object_id
=&gt; 23109650
irb(main):004:0&gt; b.object_id
=&gt; 23109650
irb(main):005:0&gt; a.gsub!(/ text/, &quot;thing different&quot;)
=&gt; &quot;something different&quot;
irb(main):006:0&gt; b
=&gt; &quot;something different&quot;
irb(main):007:0&gt;
</pre>
<p>First, we assign a String object &#8220;some text&#8221;. We could have written it fully as a = String.new(&#8220;some text&#8221;).</p>
<p>Second, we say b = a, and for Ruby this means that b will now point to the same object as a, and you can verify it by running object_id on both variables. But now comes the catch &#8211; if you modify a, b will be automatically modified. So, by running gsub! on a, we also modify b. </p>
<p>However, if you assign a new value to one of those linked variables, the link breaks down, as now one of the variables points to another object. So, if you type <strong>a = &#8220;some new text&#8221;</strong>, b will not change, and actually a and b will now point to 2 different objects.</p>
<p>If you want to create a copy of an object and point a variable to it, instead of <strong>b = a</strong>, you could have cloned the object: <strong>b = a.clone</strong>.    </p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/allaboutruby.wordpress.com/194/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/allaboutruby.wordpress.com/194/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/allaboutruby.wordpress.com/194/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/allaboutruby.wordpress.com/194/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/allaboutruby.wordpress.com/194/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/allaboutruby.wordpress.com/194/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/allaboutruby.wordpress.com/194/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/allaboutruby.wordpress.com/194/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/allaboutruby.wordpress.com/194/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/allaboutruby.wordpress.com/194/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=allaboutruby.wordpress.com&blog=56213&post=194&subd=allaboutruby&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://allaboutruby.wordpress.com/2009/09/24/ruby-variables/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6a3304c2fbc5e2e67e8c6b0a1d9715d2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">allaboutruby</media:title>
		</media:content>
	</item>
		<item>
		<title>Timing attack? What&#8217;s that about?</title>
		<link>http://allaboutruby.wordpress.com/2009/09/08/timing-attack-whats-that-about/</link>
		<comments>http://allaboutruby.wordpress.com/2009/09/08/timing-attack-whats-that-about/#comments</comments>
		<pubDate>Tue, 08 Sep 2009 19:01:37 +0000</pubDate>
		<dc:creator>allaboutruby</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Rails 2.3.4]]></category>
		<category><![CDATA[Timing attack]]></category>

		<guid isPermaLink="false">http://allaboutruby.wordpress.com/?p=191</guid>
		<description><![CDATA[If you are wondering, like me, what was the &#8220;timing attack&#8221; fix update in the latest 2.3.4 for, you can read this post here. Basically, if a comparison is made between two strings, it&#8217;s done one character at a time, and it stops when the first differing character is encountered. Therefore a potential hacker could [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=allaboutruby.wordpress.com&blog=56213&post=191&subd=allaboutruby&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>If you are wondering, like me, what was the &#8220;timing attack&#8221; fix update in the latest 2.3.4 for, you can read <a href="http://codahale.com/a-lesson-in-timing-attacks/">this post here</a>. Basically, if a comparison is made between two strings, it&#8217;s done one character at a time, and it stops when the first differing character is encountered. Therefore a potential hacker could measure how much time it takes to check a cookie hash to see if it&#8217;s correct or not. </p>
<p>It all seems far fetched, given that surely there must be the overhead in terms of routing from attacker&#8217;s PC to your server and Internet Protocol actually works that way that the packets are sent using different nodes, but there are guys who think it&#8217;s practical. It&#8217;s claimed that most Java programs are exposed under this theoretical vulnerability, so it&#8217;s actually not a problem particular to Rails.</p>
<p>However, the bad news about this 2.3.4 patch is that it doesn&#8217;t work under Ruby 1.9.1.</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/allaboutruby.wordpress.com/191/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/allaboutruby.wordpress.com/191/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/allaboutruby.wordpress.com/191/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/allaboutruby.wordpress.com/191/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/allaboutruby.wordpress.com/191/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/allaboutruby.wordpress.com/191/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/allaboutruby.wordpress.com/191/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/allaboutruby.wordpress.com/191/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/allaboutruby.wordpress.com/191/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/allaboutruby.wordpress.com/191/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=allaboutruby.wordpress.com&blog=56213&post=191&subd=allaboutruby&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://allaboutruby.wordpress.com/2009/09/08/timing-attack-whats-that-about/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6a3304c2fbc5e2e67e8c6b0a1d9715d2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">allaboutruby</media:title>
		</media:content>
	</item>
		<item>
		<title>Defining methods in Ruby</title>
		<link>http://allaboutruby.wordpress.com/2009/09/06/defining-methods-in-ruby/</link>
		<comments>http://allaboutruby.wordpress.com/2009/09/06/defining-methods-in-ruby/#comments</comments>
		<pubDate>Sun, 06 Sep 2009 16:50:09 +0000</pubDate>
		<dc:creator>allaboutruby</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[defining methods]]></category>
		<category><![CDATA[metaprogramming]]></category>
		<category><![CDATA[ruby on rails]]></category>

		<guid isPermaLink="false">http://allaboutruby.wordpress.com/?p=189</guid>
		<description><![CDATA[1 &#8211; Hashes in method calls
You must have seen this syntax in Rails a lot, for example when using this Paperclip plugin, you would put this in your model:

has_attached_file :photo, :url =&#62; &#34;/:class/:attachment/:id/:style/:basename.:extension&#34;

While it looks pretty natural, you might have harder time understanding that has_attached_file is really a method, and there&#8217;s a considerable amount of [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=allaboutruby.wordpress.com&blog=56213&post=189&subd=allaboutruby&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p><strong>1 &#8211; Hashes in method calls</strong></p>
<p>You must have seen this syntax in Rails a lot, for example when using this Paperclip plugin, you would put this in your model:</p>
<pre class="brush: ruby;">
has_attached_file :photo, :url =&gt; &quot;/:class/:attachment/:id/:style/:basename.:extension&quot;
</pre>
<p>While it looks pretty natural, you might have harder time understanding that <strong>has_attached_file</strong> is really a method, and there&#8217;s a considerable amount of trickery involved to make this method call much easier on your eyes. This post will not only help you read the Ruby code better, but actually create the methods with flexible parameters yourself.</p>
<p>Consider this equivalent call:</p>
<pre class="brush: ruby;">
has_attached_file(:photo, {:url =&gt; &quot;/:class/:attachment/:id/:style/:basename.:extension&quot;})
</pre>
<p>Now, it&#8217;s more clear that has_attached_file method passes 2 values, where a second value is a hash. Allowing hashes in method calls gives a great freedom for both the developer and user of the method &#8211; you do not need to remember what&#8217;s the correct sequence of the parameters for the method and it&#8217;s possible to omit some parameters altogether. So, instead of s<strong>ome_method(first, second, third)</strong>, you can use a more flexible <strong>some_method :second =&gt; second, :first =&gt; first, :third =&gt; third</strong>. </p>
<p>Do note, that if you omit the round brackets, you should also omit the curly brackets, so that the hash is not confused for a block, which also uses the curly brackets.</p>
<p>Actually, if you look at how the method was defined in paperclip plugin, you will see this:</p>
<pre class="brush: ruby;">
def has_attached_file name, options = {}
  ...
end
</pre>
<p>This method definition (def) uses the fact that Ruby makes brackets optional even for method definitions, so it&#8217;s equivalent to this definition:</p>
<pre class="brush: ruby;">
def has_attached_file(name, options = {})
  ...
end
</pre>
<p>And what it means is that has_attached_file method expects 2 parameters. The one called <strong>options</strong> is a hash and it defaults to an empty hash. Actually defaults are quite powerful in Ruby &#8211; you can pass any arbitrary Ruby expression to them and call any variables in scope and link to any other parameters. You could have said, for example &#8211; <strong>def has_attached_file name, options = name.size</strong>.</p>
<p>So, how would you access those values passed in the hash? This is very easy, you would do something like this:</p>
<pre class="brush: ruby;">
def has_attached_file(name, options = {})
  size = options[:size] || 0
  file = options[:file] || &quot;undefined&quot;
end
</pre>
<p>So, you would check for a hash element with a key <strong>:file</strong> and if it exists, assign it to the file local variable. If it does not exist, you will (by using ||) set a default.</p>
<p><strong>2 &#8211; How many parameters do you want?</strong></p>
<p>Another common way to define a method is like this:</p>
<pre class="brush: ruby;">
def some_method(*args)
  ...
end
</pre>
<p>This means that however many arguments can be provided and they will all be moved into array called args, so that you can access them as a normal array. You can also do something like:</p>
<pre class="brush: ruby;">
def some_method(first, *args)
  ...
end
</pre>
<p>This will move the first parameter to first local var, all others (if any) will go into the args array.</p>
<p><strong>3. Define your own method on fly?</strong></p>
<p>Another thing you will notice a lot in Rails, is the possibility to define the methods on the fly. And great thing about it is that you can create the method name using any variables and Ruby expressions. Here&#8217;s another example from the Paperclip:</p>
<pre class="brush: ruby;">
define_method &quot;#{name}=&quot; do
  ...
end
</pre>
<p>This will create a method using the name variable, so that you can have access to this method based on the name of your choice. For more info you can see <a href="http://www.ruby-doc.org/core/classes/Module.html#M001654">Ruby docs</a>.</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/allaboutruby.wordpress.com/189/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/allaboutruby.wordpress.com/189/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/allaboutruby.wordpress.com/189/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/allaboutruby.wordpress.com/189/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/allaboutruby.wordpress.com/189/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/allaboutruby.wordpress.com/189/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/allaboutruby.wordpress.com/189/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/allaboutruby.wordpress.com/189/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/allaboutruby.wordpress.com/189/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/allaboutruby.wordpress.com/189/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=allaboutruby.wordpress.com&blog=56213&post=189&subd=allaboutruby&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://allaboutruby.wordpress.com/2009/09/06/defining-methods-in-ruby/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6a3304c2fbc5e2e67e8c6b0a1d9715d2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">allaboutruby</media:title>
		</media:content>
	</item>
		<item>
		<title>Rails 2.3.4 is out, security vulnerabilities fixed</title>
		<link>http://allaboutruby.wordpress.com/2009/09/05/rails-2-3-4-is-out-security-vulnerabilities-fixed/</link>
		<comments>http://allaboutruby.wordpress.com/2009/09/05/rails-2-3-4-is-out-security-vulnerabilities-fixed/#comments</comments>
		<pubDate>Sat, 05 Sep 2009 20:12:33 +0000</pubDate>
		<dc:creator>allaboutruby</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Rails 2.3.4]]></category>

		<guid isPermaLink="false">http://allaboutruby.wordpress.com/?p=186</guid>
		<description><![CDATA[Rails 2.3.4 is out, just a few weeks after the 2.3.3. There are no new features, the reason for this quick release are the 2 vulnerabilities found in the Rails code (one is a possible XSS epxloit and the other is a cookie hack. The release notes state that over 100 small bugs were fixed [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=allaboutruby.wordpress.com&blog=56213&post=186&subd=allaboutruby&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p><a href="http://weblog.rubyonrails.org/2009/9/4/ruby-on-rails-2-3-4">Rails 2.3.4 is out</a>, just a few weeks after the 2.3.3. There are no new features, the reason for this quick release are the 2 vulnerabilities found in the Rails code (one is a possible <a href="http://weblog.rubyonrails.org/2009/9/4/xss-vulnerability-in-ruby-on-rails">XSS epxloit</a> and the other is a cookie hack. The release notes state that over 100 small bugs were fixed as well.</p>
<p>However, given the amount of problems some users have (see some <a href="http://weblog.rubyonrails.org/2009/9/4/ruby-on-rails-2-3-4">comments here</a>) &#8211; mostly as it seems with Ruby 1.9, you might want to just patch the vulnerabilities for now, or at least test before you upgrade on production <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> .</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/allaboutruby.wordpress.com/186/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/allaboutruby.wordpress.com/186/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/allaboutruby.wordpress.com/186/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/allaboutruby.wordpress.com/186/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/allaboutruby.wordpress.com/186/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/allaboutruby.wordpress.com/186/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/allaboutruby.wordpress.com/186/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/allaboutruby.wordpress.com/186/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/allaboutruby.wordpress.com/186/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/allaboutruby.wordpress.com/186/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=allaboutruby.wordpress.com&blog=56213&post=186&subd=allaboutruby&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://allaboutruby.wordpress.com/2009/09/05/rails-2-3-4-is-out-security-vulnerabilities-fixed/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6a3304c2fbc5e2e67e8c6b0a1d9715d2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">allaboutruby</media:title>
		</media:content>
	</item>
		<item>
		<title>Updating Ruby / Rails</title>
		<link>http://allaboutruby.wordpress.com/2009/08/30/updating-ruby-rails/</link>
		<comments>http://allaboutruby.wordpress.com/2009/08/30/updating-ruby-rails/#comments</comments>
		<pubDate>Sun, 30 Aug 2009 13:45:05 +0000</pubDate>
		<dc:creator>allaboutruby</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[gems]]></category>
		<category><![CDATA[updating rails]]></category>
		<category><![CDATA[updating ruby]]></category>

		<guid isPermaLink="false">http://allaboutruby.wordpress.com/?p=180</guid>
		<description><![CDATA[Updating Ruby, Rails and other installed gems is pretty easy.
1 &#8211; Run the command &#8220;gem outdated&#8221;, to see which of your installed gems need updating.
2 &#8211; Update the gems itself by running

gem update --system

3 &#8211; Then if you want to update all the gems, simply run the command &#8220;gem update&#8221;. If you&#8217;re running the command [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=allaboutruby.wordpress.com&blog=56213&post=180&subd=allaboutruby&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Updating Ruby, Rails and other installed gems is pretty easy.</p>
<p>1 &#8211; Run the command &#8220;gem outdated&#8221;, to see which of your installed gems need updating.</p>
<p>2 &#8211; Update the gems itself by running</p>
<pre class="brush: ruby;">
gem update --system
</pre>
<p>3 &#8211; Then if you want to update all the gems, simply run the command &#8220;gem update&#8221;. If you&#8217;re running the command in Windows, you should probably run &#8220;gem update &gt; result.txt&#8221;, which will output the results of the command in the text file, so that you can take a look if any error were reported in the process.</p>
<p>4 &#8211; Finally, if you made an update of Rails, you will need to manually change the version your project uses by going into config/environment.rb and finding and changing this line:</p>
<pre class="brush: ruby;">
RAILS_GEM_VERSION = '2.3.3' unless defined? RAILS_GEM_VERSION
</pre>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/allaboutruby.wordpress.com/180/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/allaboutruby.wordpress.com/180/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/allaboutruby.wordpress.com/180/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/allaboutruby.wordpress.com/180/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/allaboutruby.wordpress.com/180/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/allaboutruby.wordpress.com/180/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/allaboutruby.wordpress.com/180/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/allaboutruby.wordpress.com/180/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/allaboutruby.wordpress.com/180/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/allaboutruby.wordpress.com/180/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=allaboutruby.wordpress.com&blog=56213&post=180&subd=allaboutruby&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://allaboutruby.wordpress.com/2009/08/30/updating-ruby-rails/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6a3304c2fbc5e2e67e8c6b0a1d9715d2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">allaboutruby</media:title>
		</media:content>
	</item>
		<item>
		<title>Code refactoring: generic partials</title>
		<link>http://allaboutruby.wordpress.com/2009/08/30/code-refactoring-generic-partials/</link>
		<comments>http://allaboutruby.wordpress.com/2009/08/30/code-refactoring-generic-partials/#comments</comments>
		<pubDate>Sun, 30 Aug 2009 13:10:16 +0000</pubDate>
		<dc:creator>allaboutruby</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[code refactoring]]></category>
		<category><![CDATA[generic partials]]></category>
		<category><![CDATA[polymorphic routes]]></category>
		<category><![CDATA[ruby on rails]]></category>

		<guid isPermaLink="false">http://allaboutruby.wordpress.com/?p=178</guid>
		<description><![CDATA[When making project of any size you will most probably find yourself repeating code from one model in the other, be it similarities in models, controllers or views. This time I would like to explain how you can take out any repeated code in your views by using partials.
In order to call a partial from [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=allaboutruby.wordpress.com&blog=56213&post=178&subd=allaboutruby&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>When making project of any size you will most probably find yourself repeating code from one model in the other, be it similarities in models, controllers or views. This time I would like to explain how you can take out any repeated code in your views by using partials.</p>
<p>In order to call a partial from your view, you will need to use a render(:partial) method, like this:</p>
<pre class="brush: ruby;">
&lt;%= render :partial =&gt; &quot;common/tags&quot;, :locals =&gt; { :thing =&gt; @artist } %&gt;
</pre>
<p>1 &#8211; So, &#8220;<strong>common/tags</strong>&#8221; tells Rails to look for your partial called <strong>_tags.html.erb</strong> (remember the underscore in front &#8211; this is an easy visual way to say that the view template is a partial) in directory <strong>app/views/common</strong>. </p>
<p>2 &#8211; The <strong>:locals</strong> hash sends all needed variables to the partial. In our case we are sending <strong>@artist</strong> variable as a local variable called <strong>thing</strong>. You can send as many local variables as you need.</p>
<p>Now, let&#8217;s look at the partial:</p>
<p>_tags.html.erb:</p>
<pre class="brush: ruby;">
	 &lt;b&gt;Tags:&lt;/b&gt;
	 &lt;% thing.tags.uniq.each do |tag|%&gt;
	 &lt;%= link_to tag.name.capitalize, tag_path(tag.name) %&gt;
	 &lt;% end %&gt;
	 &lt;%= link_to &quot;Add more tags&quot;, edit_polymorphic_path(thing) %&gt;
     &lt;% end %&gt;
</pre>
<p>1 &#8211; You see that the partial works with whatever the view passed to it in the local <strong>thing</strong> variable. As it&#8217;s a local variable there is no @ in front.<br />
2 &#8211; As the partial does not really know what <strong>thing</strong> really is (and doesn&#8217;t need to in our case, apart from the fact that the thing should have tags method), if you want to include links in the partials, there are some easy to use methods for you:</p>
<pre class="brush: ruby;">
link_to &quot;Show this thing to me&quot;, thing
link_to &quot;Edit this thing&quot;, edit_polymorphic_path(thing)
</pre>
<p>1 &#8211; The first notation is very useful short form of link_to &#8211; you can pass whatever object in place of the link and Rails will generate the right link to this object&#8217;s show method (it works with RESTful resources).</p>
<p>2 &#8211; The second is the same but for editing any object. Just use <strong>edit_polymorphic_path(thing)</strong>. It will create an edit path for your object automatically. You can read <a href="http://api.rubyonrails.org/classes/ActionController/PolymorphicRoutes.html">more about polymorphic routes here</a>.</p>
<p>So, this way you can make your partials generic and not dependent on the type of object being passed to it. If you will need to know what the thing being passed really is an object of, you can always check the <strong>thing.class.name</strong> property, which will return a string with the name of its class. But if you need to branch your code based on class of the object passed, you might not need to move it to a partial in the first place.</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/allaboutruby.wordpress.com/178/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/allaboutruby.wordpress.com/178/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/allaboutruby.wordpress.com/178/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/allaboutruby.wordpress.com/178/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/allaboutruby.wordpress.com/178/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/allaboutruby.wordpress.com/178/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/allaboutruby.wordpress.com/178/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/allaboutruby.wordpress.com/178/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/allaboutruby.wordpress.com/178/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/allaboutruby.wordpress.com/178/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=allaboutruby.wordpress.com&blog=56213&post=178&subd=allaboutruby&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://allaboutruby.wordpress.com/2009/08/30/code-refactoring-generic-partials/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6a3304c2fbc5e2e67e8c6b0a1d9715d2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">allaboutruby</media:title>
		</media:content>
	</item>
		<item>
		<title>Best Resources for Rails</title>
		<link>http://allaboutruby.wordpress.com/2009/08/21/best-resources-for-rails/</link>
		<comments>http://allaboutruby.wordpress.com/2009/08/21/best-resources-for-rails/#comments</comments>
		<pubDate>Fri, 21 Aug 2009 20:41:23 +0000</pubDate>
		<dc:creator>allaboutruby</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[learning rruby on rails]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[screencasts]]></category>

		<guid isPermaLink="false">http://allaboutruby.wordpress.com/?p=173</guid>
		<description><![CDATA[These sites are extremely helpful for anybody starting out with Ruby on Rails:
1 &#8211; Rails API. A must for getting any information about Rails methods, etc. Can be a bit overwhelming for a total newbie, but becomes vital for any intermediate developer. Sometimes the descriptions are not detailed enough or do not provide enough examples [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=allaboutruby.wordpress.com&blog=56213&post=173&subd=allaboutruby&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>These sites are extremely helpful for anybody starting out with Ruby on Rails:</p>
<p>1 &#8211; <a href="http://api.rubyonrails.org/">Rails API</a>. A must for getting any information about Rails methods, etc. Can be a bit overwhelming for a total newbie, but becomes vital for any intermediate developer. Sometimes the descriptions are not detailed enough or do not provide enough examples to &#8220;get it&#8221;, so additional search in google may be in order <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>2 &#8211; <a href="http://www.ruby-doc.org/core/">Ruby Docs</a>. Unofrtunately Rails API doesn&#8217;t include the Ruby methods and classes descriptions, so you sort of need to move between the two, if you don&#8217;t remember if include? is a method of Rails or Ruby. </p>
<p>3 &#8211; <a href="http://www.stackoverflow.com/">Stackoverflow</a>. A really great resource for quickly getting your questions answered or searching for a solution to a problem. Some of the very experienced Rails guys give free and valuable advice there, highly recommended!</p>
<p>4 &#8211; <a href="http://railsforum.com/">Rails Forum</a>. A good resource for searching solutions for encountered problems. Some questions do not get answered though <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> .</p>
<p>5 &#8211; <a href="http://railscasts.com/">Railscasts</a>. Great free short video tutorials on many, many topics. The pace, level of detail, comprehensibility are all top-notch, you should spend as much free time watching those as possible <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>Hope it helps. Do include additional suggestions in the comments to share with others</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/allaboutruby.wordpress.com/173/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/allaboutruby.wordpress.com/173/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/allaboutruby.wordpress.com/173/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/allaboutruby.wordpress.com/173/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/allaboutruby.wordpress.com/173/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/allaboutruby.wordpress.com/173/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/allaboutruby.wordpress.com/173/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/allaboutruby.wordpress.com/173/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/allaboutruby.wordpress.com/173/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/allaboutruby.wordpress.com/173/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=allaboutruby.wordpress.com&blog=56213&post=173&subd=allaboutruby&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://allaboutruby.wordpress.com/2009/08/21/best-resources-for-rails/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6a3304c2fbc5e2e67e8c6b0a1d9715d2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">allaboutruby</media:title>
		</media:content>
	</item>
		<item>
		<title>Yet more advice on learning Ruby &amp; Rails</title>
		<link>http://allaboutruby.wordpress.com/2009/08/16/yet-more-advice-on-learning-ruby-rails/</link>
		<comments>http://allaboutruby.wordpress.com/2009/08/16/yet-more-advice-on-learning-ruby-rails/#comments</comments>
		<pubDate>Sun, 16 Aug 2009 09:45:43 +0000</pubDate>
		<dc:creator>allaboutruby</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://allaboutruby.wordpress.com/?p=167</guid>
		<description><![CDATA[A good post on the official Ruby on Rails blog goes through advice of the Ruby development team on how to start learning Ruby.
       <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=allaboutruby.wordpress.com&blog=56213&post=167&subd=allaboutruby&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>A good post on the official Ruby on Rails blog goes through advice of the Ruby development team on how to <a href="http://weblog.rubyonrails.org/2009/8/3/how-do-i-learn-ruby-rails">start learning Ruby</a>.</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/allaboutruby.wordpress.com/167/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/allaboutruby.wordpress.com/167/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/allaboutruby.wordpress.com/167/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/allaboutruby.wordpress.com/167/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/allaboutruby.wordpress.com/167/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/allaboutruby.wordpress.com/167/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/allaboutruby.wordpress.com/167/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/allaboutruby.wordpress.com/167/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/allaboutruby.wordpress.com/167/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/allaboutruby.wordpress.com/167/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=allaboutruby.wordpress.com&blog=56213&post=167&subd=allaboutruby&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://allaboutruby.wordpress.com/2009/08/16/yet-more-advice-on-learning-ruby-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6a3304c2fbc5e2e67e8c6b0a1d9715d2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">allaboutruby</media:title>
		</media:content>
	</item>
		<item>
		<title>5 minute project in Rails</title>
		<link>http://allaboutruby.wordpress.com/2009/08/08/5-minute-project-in-rails/</link>
		<comments>http://allaboutruby.wordpress.com/2009/08/08/5-minute-project-in-rails/#comments</comments>
		<pubDate>Sat, 08 Aug 2009 18:43:57 +0000</pubDate>
		<dc:creator>allaboutruby</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[5 minute project in Rails]]></category>

		<guid isPermaLink="false">http://allaboutruby.wordpress.com/?p=155</guid>
		<description><![CDATA[Ok, you&#8217;ve installed the Ruby on Rails and you want to run a quick tutorial to see what can be done. Let&#8217;s do a simplified Journal application that allows to create users &#38; posts.
1. Create a new rails project by running a rails name_of_the_project command, like this:

rails journal

You will see a bunch of &#8220;Created xxx&#8221; [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=allaboutruby.wordpress.com&blog=56213&post=155&subd=allaboutruby&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Ok, you&#8217;ve installed the Ruby on Rails and you want to run a quick tutorial to see what can be done. Let&#8217;s do a simplified Journal application that allows to create users &amp; posts.</p>
<p>1. Create a new rails project by running a rails name_of_the_project command, like this:</p>
<pre class="brush: ruby;">
rails journal
</pre>
<p>You will see a bunch of &#8220;Created xxx&#8221; messages, that will create or copy over a bunch of directories and files.</p>
<p>2. Next, let&#8217;s create a Scaffold for our users. Scaffold is a basic structure of Model, View and Controller that allows you to quickly create, edit, show and delete objects. Let&#8217;s assume that each user will have a name, a biography and email for now. We will use a <strong>generate</strong> script in order to automatically prepare all required files for us. Here&#8217;s the command:</p>
<pre class="brush: ruby;">
ruby script/generate scaffold user name:string bio:text email:string
</pre>
<p>3. So, in the step before we have created a User model, user controller and user views for each of the basic functions of create, edit, show and delete. </p>
<p>The model now consists of three attributes which will be recorded in the database columns, named name, bio and email. In your Rails code you will access those attributes by something like @user.name, @user.bio or @user.email. </p>
<p>However, the generate script does not create the model in the database, it just creates the migration file in the db/migrate folder of your project. You can check out the directory to see that a file create_users.rb (with a timestamp in front) is actually there. If you will need to extend the model later, it can easily be done by running another migration. </p>
<p>Ok, let&#8217;s run the migration by typing the command:</p>
<pre class="brush: ruby;">
rake db:migrate
</pre>
<p>4. Now, run the server to check that everything works as expected:</p>
<pre class="brush: ruby;">
ruby script/server
</pre>
<p>And try out the following address: <a href="http://127.0.0.1:3000/users">http://127.0.0.1:3000/users</a></p>
<p>It should show you an interface to create / edit / delete users</p>
<p>5. Let&#8217;s then create a model for our posts. Here&#8217;s the same command we used for creating users:</p>
<pre class="brush: ruby;">
ruby script/generate scaffold post title:string post:text
</pre>
<p>And, as before, you need to modify the database by running a migration:</p>
<pre class="brush: ruby;">
rake db:migrate
</pre>
<p>6. Now, we want to be able to merge these two models together. What we want to say is that a User can write (have) many posts and each post has to be written by a (belong to) user. This is very simply done in the model definitions:</p>
<p>In app/models/user.rb add has_many :posts association</p>
<pre class="brush: ruby;">
class User &lt; ActiveRecord::Base
 has_many :posts
end
</pre>
<p>In app/models/post.rb add belongs_to line:</p>
<pre class="brush: ruby;">
class Post &lt; ActiveRecord::Base
 belongs_to :user
end
</pre>
<p>Now Rails knows that Posts and Users are related and you should be able to get a list of posts of a particular user by simply typing @user.posts and vice versa &#8211; an author of the post will be @post.user. This is really cool!</p>
<p>Before it all works, however, you need to provide Rails with a place in the database to store the relationship. It&#8217;s done by including the id of user to the Post model (you don&#8217;t need to store / modify anything in the User model). The notation Rails follows is [related_model_name]_id, in our case user_id. And then Rails will be able to do all the wonderful relationships. </p>
<p>You can use the already used generate script, but this time a command migration will be used:</p>
<pre class="brush: ruby;">
ruby script/generate migration add_user_id_to_post user_id:integer
</pre>
<p>And run the migration again (you should start to get used to that command by now):</p>
<pre class="brush: ruby;">
rake db:migrate
</pre>
<p>7. Now it&#8217;s time to modify our default views to take advantage of the new relationship. First, let&#8217;s show the author&#8217;s name of the post. Go to app/views/posts/show.html.erb and add lines 6-9 to it.</p>
<pre class="brush: ruby;">
&lt;p&gt;
  &lt;b&gt;Title:&lt;/b&gt;
  &lt;%=h @post.title %&gt;
&lt;/p&gt;

&lt;p&gt;
&lt;b&gt;User:&lt;/b&gt;
&lt;%= link_to @post.user.name, @post.user %&gt;
&lt;/p&gt;

&lt;p&gt;
  &lt;b&gt;Post:&lt;/b&gt;
  &lt;%=h @post.post %&gt;
&lt;/p&gt;

&lt;%= link_to 'Edit', edit_post_path(@post) %&gt; |
&lt;%= link_to 'Back', posts_path %&gt;
</pre>
<p>Let&#8217;s analyze what this command does &#8211; link_to @post.user.name, @post.user </p>
<p>First, I already mentioned that you can get the author by accessing @post.user. @post.user is actually an instance of user and you can access any attributes of user, so in order to get the name of the user you write @post.user.name. link_to is a clever method that allows you to create a link to a controller or object. The first parameter is the title of the link, the second is the url. As link_to is a smart method, you can simply pass an instance of a class to it and it will figure out how to link to it.</p>
<p>8. Now, let&#8217;s show all posts of a user when you show a user. For that we need to modify the user&#8217;s show view (app/views/users/show.html.erb), adding lines 16-23:</p>
<pre class="brush: ruby;">
&lt;p&gt;
  &lt;b&gt;Name:&lt;/b&gt;
  &lt;%=h @user.name %&gt;
&lt;/p&gt;

&lt;p&gt;
  &lt;b&gt;Bio:&lt;/b&gt;
  &lt;%=h @user.bio %&gt;
&lt;/p&gt;

&lt;p&gt;
  &lt;b&gt;Email:&lt;/b&gt;
  &lt;%=h @user.email %&gt;
&lt;/p&gt;

&lt;p&gt;
  &lt;b&gt;Posts:&lt;/b&gt;

  &lt;% @user.posts.each do |post| %&gt;
    Post: &lt;%= link_to post.title, post %&gt;&lt;br/&gt;
  &lt;% end %&gt;
  &lt;%= link_to &quot;Create new post&quot;, new_post_path %&gt;
&lt;/p&gt;

&lt;%= link_to 'Edit', edit_user_path(@user) %&gt; |
&lt;%= link_to 'Back', users_path %&gt;
</pre>
<p>Ok, this one took 4 non-HTML lines, let&#8217;s go one by one:</p>
<p>@user.posts.each do |post| &#8212; we access the array of user&#8217;s posts and iterate through each of them by using iterator each. <strong>each</strong> is the easiest way to go through all the object in an array. The iteration passes control to block and puts each element one by one of array in local variable <strong>post</strong>.</p>
<p>link_to post.title, post &#8212; this is almost the same as we did for post&#8217;s user earlier.</p>
<p>end &#8211; this is to say to Rails that it&#8217;s the end of the iteration block</p>
<p>link_to &#8220;Create new post&#8221;, new_post_path &#8212; By this line we allow a new post to be created. First parameter is the string for a name of the link, second &#8211; is again a cool way to link to action of a controller [action]_[controller]_path</p>
<p>9. Finally, when we create a new Post we need to be able to select who is the author of the post by selecting the author from the list of available authors. In app/views/posts/new.html.erb before the lines &lt;p&gt; &lt;%= f.submit &#8216;Create&#8217; %&gt; insert the new field:</p>
<pre class="brush: ruby;">
  &lt;p&gt;
    &lt;%= f.label :user_id %&gt;&lt;br /&gt;
    &lt;%= f.collection_select(:user_id, User.find(:all), :id, :name, {:prompt =&gt; true}) %&gt; &lt;/p&gt;
  &lt;/p&gt;
</pre>
<p>The first line simply creates a text label of the field. The second creates a drop down list of all users in your system. The most important are the first 2 parameters &#8211; :user_id, which tells the form which variable to store the result of selection in and User.find(:all), which provides a list of all users to the drop-down. </p>
<p>10. Now save all changes and run the application at <a href="http://127.0.0.1:3000/users">http://127.0.0.1:3000/users</a></p>
<p>This is an impressive application given that it took you only 5 minutes, 15 lines of code and couple of command-line commands. Of course, you will need to extend it with a proper user authentication and you will want to modify models and views to make the app useful, but it&#8217;s a great start.</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/allaboutruby.wordpress.com/155/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/allaboutruby.wordpress.com/155/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/allaboutruby.wordpress.com/155/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/allaboutruby.wordpress.com/155/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/allaboutruby.wordpress.com/155/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/allaboutruby.wordpress.com/155/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/allaboutruby.wordpress.com/155/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/allaboutruby.wordpress.com/155/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/allaboutruby.wordpress.com/155/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/allaboutruby.wordpress.com/155/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=allaboutruby.wordpress.com&blog=56213&post=155&subd=allaboutruby&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://allaboutruby.wordpress.com/2009/08/08/5-minute-project-in-rails/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6a3304c2fbc5e2e67e8c6b0a1d9715d2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">allaboutruby</media:title>
		</media:content>
	</item>
	</channel>
</rss>