<?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>Sikachu!&#039;s Blog &#187; Ruby</title>
	<atom:link href="http://sikachu.com/category/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://sikachu.com</link>
	<description>I&#039;m a Ruby on Rails / jQuery web developer. Follow me at @sikachu</description>
	<lastBuildDate>Sat, 25 Dec 2010 15:13:28 +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>Hoptoad ❤ Heroku</title>
		<link>http://sikachu.com/2010/12/hoptoad-loves-heroku/</link>
		<comments>http://sikachu.com/2010/12/hoptoad-loves-heroku/#comments</comments>
		<pubDate>Wed, 15 Dec 2010 14:14:56 +0000</pubDate>
		<dc:creator>Sikachu!</dc:creator>
				<category><![CDATA[My Project]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Heroku]]></category>
		<category><![CDATA[hoptoad]]></category>
		<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://sikachu.com/?p=762</guid>
		<description><![CDATA[Recently, I&#8217;ve started to migrate my application to Heroku. While most of the functionalities are there, one of the thing I was missing is Hoptoad&#8216;s deploy tracking, as you cannot add a deploy hook to call Rake task as you normally do. After I found that Heroku actually supports HTTP Post hook, I spend 2 [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, I&#8217;ve started to migrate my application to <a href="http://heroku.com/" target="_blank">Heroku</a>. While most of the functionalities are there, one of the thing I was missing is <a href="http://hoptoadapp.com/pages/home" target="_blank">Hoptoad</a>&#8216;s <a href="https://hoptoadapp.com/account/new?source=home_top_button" target="_blank">deploy tracking</a>, as you cannot add a deploy hook to call Rake task as you normally do.</p>
<p>After I found that Heroku actually supports <a href="http://docs.heroku.com/deploy-hooks#http-post-hook" target="_blank">HTTP Post hook</a>, I spend 2 hours reading API and hacking into <a href="http://github.com/thoughtbot/hoptoad_notifier" target="_blank">hoptoad_notifier</a> code. And now, I would like to presents you <a href="http://hoptoad-loves.heroku.com/" target="_blank">Hoptoad ❤ Heroku</a>.</p>
<h3>What is it?</h3>
<p>It&#8217;s a middleware application that will converts parameters from Heroku&#8217;s HTTP Post hook to parameters that Hoptoad would understand.</p>
<h3>How to use it?</h3>
<p>You just add the HTTP Post deploy hook to your Heroku project. It should look something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">heroku addons:add deployhooks:http url=http:<span style="color:#006600; font-weight:bold;">//</span>hoptoad<span style="color:#006600; font-weight:bold;">-</span>loves.<span style="color:#9900CC;">heroku</span>.<span style="color:#9900CC;">com</span><span style="color:#006600; font-weight:bold;">/</span>YOUR_HOPTOAD_API_KEY<span style="color:#006600; font-weight:bold;">/</span>YOUR_RACK_ENV</pre></div></div>

<p>You&#8217;d then substitute <code>YOUR_HOPTOAD_API_KEY</code> and <code>YOUR_RACK_ENV</code> to match your configuration.</p>
<p>You can find more information on <a href="http://github.com/sikachu/hoptoad-loves-heroku" target="_blank">GitHub</a> or the site itself: <a href="http://hoptoad-loves.heroku.com/" target="_blank">http://hoptoad-loves.heroku.com/</a></p>
<p>(Please note that Hoptoad deploy tracking feature is only available in their paid plans.)</p>
<hr />
<p>I hope you found it useful for your project. All of the code is hosted on <a href="http://github.com/sikachu/hoptoad-loves-heroku" target="_blank">GitHub</a>. Contributions and feedbacks are always welcome.</p>
]]></content:encoded>
			<wfw:commentRss>http://sikachu.com/2010/12/hoptoad-loves-heroku/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>My Devise custom routes</title>
		<link>http://sikachu.com/2010/12/my-devise-custom-routes/</link>
		<comments>http://sikachu.com/2010/12/my-devise-custom-routes/#comments</comments>
		<pubDate>Mon, 13 Dec 2010 15:40:42 +0000</pubDate>
		<dc:creator>Sikachu!</dc:creator>
				<category><![CDATA[My Idea]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Devise]]></category>
		<category><![CDATA[Rails 3]]></category>

		<guid isPermaLink="false">http://sikachu.com/?p=757</guid>
		<description><![CDATA[Since the release of Rails 3, I&#8217;ve been using Devise as my authentication gem. While it delivers everything I need, its default route doesn&#8217;t look really good when I have only one authentication scope, such as User. The default route when you&#8217;re using devise_for :users will look like this: new_user_session GET /users/sign_in(.:format) {:action=&#62;&#34;new&#34;, :controller=&#62;&#34;devise/sessions&#34;} user_session [...]]]></description>
			<content:encoded><![CDATA[<p>Since the release of Rails 3, I&#8217;ve been using <a href="http://github.com/plataformatec/devise" target="_blank">Devise</a> as my authentication gem. While it delivers everything I need, its default route doesn&#8217;t look really good when I have only one authentication scope, such as <code>User</code>. The default route when you&#8217;re using <code>devise_for :users</code> will look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="console" style="font-family:monospace;">         new_user_session GET    /users/sign_in(.:format)          {:action=&gt;&quot;new&quot;, :controller=&gt;&quot;devise/sessions&quot;}
             user_session POST   /users/sign_in(.:format)          {:action=&gt;&quot;create&quot;, :controller=&gt;&quot;devise/sessions&quot;}
     destroy_user_session GET    /users/sign_out(.:format)         {:action=&gt;&quot;destroy&quot;, :controller=&gt;&quot;devise/sessions&quot;}
            user_password POST   /users/password(.:format)         {:action=&gt;&quot;create&quot;, :controller=&gt;&quot;devise/passwords&quot;}
        new_user_password GET    /users/password/new(.:format)     {:action=&gt;&quot;new&quot;, :controller=&gt;&quot;devise/passwords&quot;}
       edit_user_password GET    /users/password/edit(.:format)    {:action=&gt;&quot;edit&quot;, :controller=&gt;&quot;devise/passwords&quot;}
                          PUT    /users/password(.:format)         {:action=&gt;&quot;update&quot;, :controller=&gt;&quot;devise/passwords&quot;}
 cancel_user_registration GET    /users/cancel(.:format)           {:action=&gt;&quot;cancel&quot;, :controller=&gt;&quot;devise/registrations&quot;}
        user_registration POST   /users(.:format)                  {:action=&gt;&quot;create&quot;, :controller=&gt;&quot;devise/registrations&quot;}
    new_user_registration GET    /users/sign_up(.:format)          {:action=&gt;&quot;new&quot;, :controller=&gt;&quot;devise/registrations&quot;}
   edit_user_registration GET    /users/edit(.:format)             {:action=&gt;&quot;edit&quot;, :controller=&gt;&quot;devise/registrations&quot;}
                          PUT    /users(.:format)                  {:action=&gt;&quot;update&quot;, :controller=&gt;&quot;devise/registrations&quot;}
                          DELETE /users(.:format)                  {:action=&gt;&quot;destroy&quot;, :controller=&gt;&quot;devise/registrations&quot;}
        user_confirmation POST   /users/confirmation(.:format)     {:action=&gt;&quot;create&quot;, :controller=&gt;&quot;devise/confirmations&quot;}
    new_user_confirmation GET    /users/confirmation/new(.:format) {:action=&gt;&quot;new&quot;, :controller=&gt;&quot;devise/confirmations&quot;}
                          GET    /users/confirmation(.:format)     {:action=&gt;&quot;show&quot;, :controller=&gt;&quot;devise/confirmations&quot;}</pre></div></div>

<p>As you can see, all of the routes are defined under <code>/users</code> path. So, I&#8217;ve modified my devise route block to this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">devise_for <span style="color:#ff3333; font-weight:bold;">:users</span>, <span style="color:#ff3333; font-weight:bold;">:skip</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:registrations</span>, <span style="color:#ff3333; font-weight:bold;">:sessions</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  <span style="color:#008000; font-style:italic;"># devise/registrations</span>
  get <span style="color:#996600;">'signup'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'devise/registrations#new'</span>, <span style="color:#ff3333; font-weight:bold;">:as</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:new_user_registration</span>
  post <span style="color:#996600;">'signup'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'devise/registrations#create'</span>, <span style="color:#ff3333; font-weight:bold;">:as</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:user_registration</span>
  get <span style="color:#996600;">'users/cancel'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'devise/registrations#cancel'</span>, <span style="color:#ff3333; font-weight:bold;">:as</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:cancel_user_registration</span>
  get <span style="color:#996600;">'users/edit'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'devise/registrations#edit'</span>, <span style="color:#ff3333; font-weight:bold;">:as</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:edit_user_registration</span>
  put <span style="color:#996600;">'users'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'devise/registrations#update'</span>
  delete <span style="color:#996600;">'users/cancel'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'devise/registrations#destroy'</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># devise/sessions</span>
  get <span style="color:#996600;">'signin'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'devise/sessions#new'</span>, <span style="color:#ff3333; font-weight:bold;">:as</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:new_user_session</span>
  post <span style="color:#996600;">'signin'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'devise/sessions#create'</span>, <span style="color:#ff3333; font-weight:bold;">:as</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:user_session</span>
  get <span style="color:#996600;">'signout'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'devise/sessions#destroy'</span>, <span style="color:#ff3333; font-weight:bold;">:as</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:destroy_user_session</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Which will yield these routes:</p>

<div class="wp_syntax"><div class="code"><pre class="console" style="font-family:monospace;">    new_user_registration GET    /signup(.:format)                 {:action=&gt;&quot;new&quot;, :controller=&gt;&quot;devise/registrations&quot;}
        user_registration POST   /signup(.:format)                 {:action=&gt;&quot;create&quot;, :controller=&gt;&quot;devise/registrations&quot;}
 cancel_user_registration GET    /users/cancel(.:format)           {:controller=&gt;&quot;devise/registrations&quot;, :action=&gt;&quot;cancel&quot;}
   edit_user_registration GET    /users/edit(.:format)             {:controller=&gt;&quot;devise/registrations&quot;, :action=&gt;&quot;edit&quot;}
                    users PUT    /users(.:format)                  {:action=&gt;&quot;update&quot;, :controller=&gt;&quot;devise/registrations&quot;}
             users_cancel DELETE /users/cancel(.:format)           {:controller=&gt;&quot;devise/registrations&quot;, :action=&gt;&quot;destroy&quot;}
         new_user_session GET    /signin(.:format)                 {:action=&gt;&quot;new&quot;, :controller=&gt;&quot;devise/sessions&quot;}
             user_session POST   /signin(.:format)                 {:action=&gt;&quot;create&quot;, :controller=&gt;&quot;devise/sessions&quot;}
     destroy_user_session GET    /signout(.:format)                {:action=&gt;&quot;destroy&quot;, :controller=&gt;&quot;devise/sessions&quot;}
            user_password POST   /users/password(.:format)         {:action=&gt;&quot;create&quot;, :controller=&gt;&quot;devise/passwords&quot;}
        new_user_password GET    /users/password/new(.:format)     {:action=&gt;&quot;new&quot;, :controller=&gt;&quot;devise/passwords&quot;}
       edit_user_password GET    /users/password/edit(.:format)    {:action=&gt;&quot;edit&quot;, :controller=&gt;&quot;devise/passwords&quot;}
                          PUT    /users/password(.:format)         {:action=&gt;&quot;update&quot;, :controller=&gt;&quot;devise/passwords&quot;}
        user_confirmation POST   /users/confirmation(.:format)     {:action=&gt;&quot;create&quot;, :controller=&gt;&quot;devise/confirmations&quot;}
    new_user_confirmation GET    /users/confirmation/new(.:format) {:action=&gt;&quot;new&quot;, :controller=&gt;&quot;devise/confirmations&quot;}
                          GET    /users/confirmation(.:format)     {:action=&gt;&quot;show&quot;, :controller=&gt;&quot;devise/confirmations&quot;}</pre></div></div>

<p>I think my new routes is much better. For example, I think user would prefer a path to sign in page to be <code>/signin</code> than <code>/users/sign_in</code>, as it more memorable.</p>
<p>I hope this post would inspire you more about customize Devise&#8217;s routes to fit your need.</p>
]]></content:encoded>
			<wfw:commentRss>http://sikachu.com/2010/12/my-devise-custom-routes/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>`method_missing` call stack in Ruby</title>
		<link>http://sikachu.com/2010/11/method-missing-call-stack-in-ruby/</link>
		<comments>http://sikachu.com/2010/11/method-missing-call-stack-in-ruby/#comments</comments>
		<pubDate>Tue, 23 Nov 2010 05:56:52 +0000</pubDate>
		<dc:creator>Sikachu!</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://sikachu.com/?p=751</guid>
		<description><![CDATA[Just found out something cool from reading &#8220;Don’t Know Metaprogramming In Ruby?&#8221; from RubyLearning blog. Consider this code snippet: class Person end &#160; class Student &#60; Person end &#160; class HighSchoolStudent &#60; Student end If you&#8217;re calling some method that&#8217;s undefined, let&#8217;s say we&#8217;re calling HighSchoolStudent#name Ruby will try to call the method in this [...]]]></description>
			<content:encoded><![CDATA[<p>Just found out something cool from reading &#8220;<a href="http://rubylearning.com/blog/2010/11/23/dont-know-metaprogramming-in-ruby/" target="_blank">Don’t Know Metaprogramming In Ruby?</a>&#8221; from RubyLearning blog. Consider this code snippet:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Person
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> Student <span style="color:#006600; font-weight:bold;">&lt;</span> Person
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> HighSchoolStudent <span style="color:#006600; font-weight:bold;">&lt;</span> Student
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>If you&#8217;re calling some method that&#8217;s undefined, let&#8217;s say we&#8217;re calling <code>HighSchoolStudent#name</code> Ruby will try to call the method in this order:</p>
<ol>
<li>HighSchoolStudent#name</li>
<li>Student#name</li>
<li>Person#name</li>
<li>Object#name</li>
<li>Kernel#name</li>
<li>BasicObject#name</li>
<li>HighSchoolStudent#method_missing</li>
<li>Student#method_missing</li>
<li>Person#method_missing</li>
<li>Object#method_missing</li>
<li>Kernel#method_missing</li>
<li>BasicObject#method_missing</li>
</ol>
<p>From this example, you can see that not explicitly define method and capture it in <code>method_missing</code> might not be good in terms of the performance. So, what should you do?</p>
<p>I think the best way to do it, if you know that this method will get called again, would be define the &#8216;real&#8217; method after it gets called. That will reduce the call stack starting from the second run, resulting in faster code.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># Let say we're defining method for #*_with_id here</span>
<span style="color:#9966CC; font-weight:bold;">def</span> method_missing<span style="color:#006600; font-weight:bold;">&#40;</span>name, <span style="color:#006600; font-weight:bold;">*</span>args<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">super</span> <span style="color:#9966CC; font-weight:bold;">if</span> name !~ <span style="color:#006600; font-weight:bold;">/</span>_with_id$<span style="color:#006600; font-weight:bold;">/</span>
&nbsp;
  define_method <span style="color:#996600;">&quot;#{name}_with_id&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    instance_variable_get<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:id</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">&quot;-&quot;</span> <span style="color:#006600; font-weight:bold;">+</span> instance_variable_get<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>Hope this will make your code run faster, while still DRY.</p>
]]></content:encoded>
			<wfw:commentRss>http://sikachu.com/2010/11/method-missing-call-stack-in-ruby/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Gems — Follow up from my talk at #BarcampBKK4</title>
		<link>http://sikachu.com/2010/10/gems-barcampbkk4/</link>
		<comments>http://sikachu.com/2010/10/gems-barcampbkk4/#comments</comments>
		<pubDate>Fri, 29 Oct 2010 16:46:44 +0000</pubDate>
		<dc:creator>Sikachu!</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[BarcampBKK4]]></category>

		<guid isPermaLink="false">http://sikachu.com/?p=749</guid>
		<description><![CDATA[I think I should noted down two gems that I mentioned in my talk. Omniauth — a middleware that help you authenticate user to various SSO sites, such as Facebook Connect, Twitter, Google Account Devise — fully-functional authentication system. If you write an app that need authentication, this should be the solution for you. Writing [...]]]></description>
			<content:encoded><![CDATA[<p>I think I should noted down two gems that I mentioned in my talk.</p>
<ul>
<li><strong><a href="http://github.com/intridea/omniauth" target="_blank">Omniauth</a></strong> — a middleware that help you authenticate user to various SSO sites, such as Facebook Connect, Twitter, Google Account</li>
<li><strong><a href="http://github.com/plataformatec/devise" target="_blank">Devise</a></strong> — fully-functional authentication system. If you write an app that need authentication, this should be the solution for you. Writing your own authentication is discourage now. More on this later.</li>
</ul>
<p>I feel like I was saying 3 of them, but I can&#8217;t remember the other one. Anyway, don&#8217;t forget to dig into <a href="http://www.rubygems.org/" target="_blank">RubyGems</a> to find out some great gem to use in your project.</p>
]]></content:encoded>
			<wfw:commentRss>http://sikachu.com/2010/10/gems-barcampbkk4/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>10 Things you should know about Ruby — #BarcampBKK4</title>
		<link>http://sikachu.com/2010/10/10-things-you-should-know-about-ruby/</link>
		<comments>http://sikachu.com/2010/10/10-things-you-should-know-about-ruby/#comments</comments>
		<pubDate>Sun, 24 Oct 2010 15:20:28 +0000</pubDate>
		<dc:creator>Sikachu!</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[BarcampBKK4]]></category>

		<guid isPermaLink="false">http://sikachu.com/?p=743</guid>
		<description><![CDATA[I&#8217;ve done a talk about Ruby in Barcamp Bangkok 4 on Oct 24. Here is the slide in case someone want it. 10 things you should know about ruby You can download it if you want. This slide is CC-BY-NC-SA (as some photo I use require share-alike license.) One good question actually was the difference [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve done a talk about Ruby in Barcamp Bangkok 4 on Oct 24. Here is the slide in case someone want it.</p>
<div style="width:425px" id="__ss_5544219"><strong style="display:block;margin:12px 0 4px"><a href="http://www.slideshare.net/sikachu/10-things-you-should-know-about-ruby" title="10 things you should know about ruby">10 things you should know about ruby</a></strong><object id="__sse5544219" width="425" height="355"><param name="movie" value="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=10thingsyoushouldknowaboutruby-101024103648-phpapp01&#038;stripped_title=10-things-you-should-know-about-ruby&#038;userName=sikachu" /><param name="allowFullScreen" value="true"/><param name="allowScriptAccess" value="always"/><embed name="__sse5544219" src="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=10thingsyoushouldknowaboutruby-101024103648-phpapp01&#038;stripped_title=10-things-you-should-know-about-ruby&#038;userName=sikachu" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="355"></embed></object></div>
<p>You can <a href="http://www.slideshare.net/sikachu/10-things-you-should-know-about-ruby/download" target="_blank">download</a> it if you want. This slide is <a href="http://creativecommons.org/licenses/by-nc-sa/2.0/" target="_blank">CC-BY-NC-SA</a> (as some photo I use require share-alike license.)</p>
<p>One good question actually was the difference between <code>module</code> and <code>class</code>. Actually, it just that simple — you cannot create object from a module.</p>
<p><code>Module</code> exists in Ruby to help you DRY up your code.</p>
<p>When you write several classes which have the same functionality, you better take that portion of the code out and put it in a module and <code>include</code> them back into your classes. That&#8217;s actually something Ruby coder loves to do — DRY up your code, and make it more maintainable.</p>
<hr />
<p>If you have any more question, feel free to post them in the comment section. I actually fumbled on my word a lot, because I didn&#8217;t get enough sleep last night. If you also want me to clearify something up, please let me know. Anyway, I hope you enjoyed it.</p>
]]></content:encoded>
			<wfw:commentRss>http://sikachu.com/2010/10/10-things-you-should-know-about-ruby/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>RailsCamp (Thailand) 2010</title>
		<link>http://sikachu.com/2010/10/railscamp-thailand-2010/</link>
		<comments>http://sikachu.com/2010/10/railscamp-thailand-2010/#comments</comments>
		<pubDate>Wed, 20 Oct 2010 06:52:35 +0000</pubDate>
		<dc:creator>Sikachu!</dc:creator>
				<category><![CDATA[News]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://sikachu.com/?p=739</guid>
		<description><![CDATA[เพิ่งทราบข่าวมาวันนี้ว่าที่ประเทศไทย จะมีคนจัดงาน RailsCamp แล้ว! RailsCamp (Thailand) 2010 ดีใจ + ตกใจนิดหน่อย เพราะเป็นโปรเจคที่คิดไว้ตั้งแต่เมื่อ 2-3 ปีที่แล้ว ตั้งแต่เมื่อตอนที่เขียน Rails ใหม่ๆ ว่าอยากให้มีงานที่เป็น Ruby on Rails Community ของคนไทย &#8230; ในเมื่อมีคนทำแล้ว ผมก็ขอสนับสนุนเต็มทีครับ ผมคงจะเตรียมเรื่องไปพูดนิดหน่อย ไม่แน่ใจเหมือนกันว่าเรื่องอะไร แต่ถ้าดูจาก Schedule ปัจจุบัน อาจจะได้พูดเรื่อง Behavior Driven Development using Cucumber หรือไม่ก็ Devise เพราะว่าเรื่องใหญ่ๆ อย่าง Rails 3 กับ Ruby 1.9 ได้อาจารย์ @rawitat กับคุณ @neokain พูดไปแล้ว ไปเจอกันโลด วันเสาร์ที่ 13 พฤศจิกายน 2553 [...]]]></description>
			<content:encoded><![CDATA[<p>เพิ่งทราบข่าวมาวันนี้ว่าที่ประเทศไทย จะมีคนจัดงาน RailsCamp แล้ว!</p>
<p><a href="http://www.thairor.com/2010/10/blog-post.html" target="_blank">RailsCamp (Thailand) 2010</a></p>
<p>ดีใจ + ตกใจนิดหน่อย เพราะเป็นโปรเจคที่คิดไว้ตั้งแต่เมื่อ 2-3 ปีที่แล้ว ตั้งแต่เมื่อตอนที่เขียน Rails ใหม่ๆ ว่าอยากให้มีงานที่เป็น Ruby on Rails Community ของคนไทย &#8230; ในเมื่อมีคนทำแล้ว ผมก็ขอสนับสนุนเต็มทีครับ ผมคงจะเตรียมเรื่องไปพูดนิดหน่อย ไม่แน่ใจเหมือนกันว่าเรื่องอะไร แต่ถ้าดูจาก Schedule ปัจจุบัน อาจจะได้พูดเรื่อง Behavior Driven Development using Cucumber หรือไม่ก็ Devise เพราะว่าเรื่องใหญ่ๆ อย่าง Rails 3 กับ Ruby 1.9 ได้อาจารย์ <a href="http://twitter.com/rawitat" target="_blank">@rawitat</a> กับคุณ <a href="http://twitter.com/neokain" target="_blank">@neokain</a> พูดไปแล้ว <img src='http://sikachu.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<p>ไปเจอกันโลด วันเสาร์ที่ 13 พฤศจิกายน 2553 เวลา 9:30-17:00 น. ณ Opendream</p>
<p>ปล. ได้เวลาปัดฝุ่นโปรเจคลับแล้วสินะ <img src='http://sikachu.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://sikachu.com/2010/10/railscamp-thailand-2010/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Possible pitfall on ActiveRecord::Base#create</title>
		<link>http://sikachu.com/2010/08/possible-pitfall-on-activerecordbasecreate/</link>
		<comments>http://sikachu.com/2010/08/possible-pitfall-on-activerecordbasecreate/#comments</comments>
		<pubDate>Sat, 21 Aug 2010 16:32:44 +0000</pubDate>
		<dc:creator>Sikachu!</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[active_record]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Rails 3]]></category>

		<guid isPermaLink="false">http://sikachu.com/?p=722</guid>
		<description><![CDATA[I came across this a while ago when I was trying to clean up my code. Just write it down so you won&#8217;t follow me Consider that Post having :title attribute and class Post &#60; ActiveRecord::Base validates_presence_of :title end Well, what do you think would be the result of the following expression? if Post.create&#40;:title =&#62; [...]]]></description>
			<content:encoded><![CDATA[<p>I came across this a while ago when I was trying to clean up my code. Just write it down so you won&#8217;t follow me <img src='http://sikachu.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Consider that <code>Post</code> having <code>:title</code> attribute and</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 <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  validates_presence_of <span style="color:#ff3333; font-weight:bold;">:title</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Well, what do you think would be the result of the following expression?</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">if</span> Post.<span style="color:#9900CC;">create</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:title</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">nil</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Saved!&quot;</span>
<span style="color:#9966CC; font-weight:bold;">else</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Validation error ...&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Somehow, I expected <code>"Validation error ..."</code> message from it, but I got <code>"Saved!"</code> Why?</p>
<h3>Return value of <code>#create</code></h3>
<p>You need to remember that <code>#create</code> method always return the object itself, which have the <code>id</code> as <code>nil</code>. This, evaluates to <code>true</code> and make our code fail. The solution for this problem would be using <code>#new</code> to create object, and then using <code>#save</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">post = Post.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:title</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">nil</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">if</span> post.<span style="color:#9900CC;">save</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Saved!&quot;</span>
<span style="color:#9966CC; font-weight:bold;">else</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Validation error ...&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>For you one-liner, I think you can do this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">if</span> post = Post.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:title</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">nil</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&amp;&amp;</span> post.<span style="color:#9900CC;">save</span></pre></div></div>

<p>or (as suggested by <a href="http://twitter.com/shr/status/21801270429" target="_blank">shr</a>)</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">if</span> Post.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:title</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">nil</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">save</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://sikachu.com/2010/08/possible-pitfall-on-activerecordbasecreate/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Ruby 1.9.2 is out!!</title>
		<link>http://sikachu.com/2010/08/ruby-1-9-2-is-out/</link>
		<comments>http://sikachu.com/2010/08/ruby-1-9-2-is-out/#comments</comments>
		<pubDate>Wed, 18 Aug 2010 16:08:33 +0000</pubDate>
		<dc:creator>Sikachu!</dc:creator>
				<category><![CDATA[News]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Ruby 1.9]]></category>

		<guid isPermaLink="false">http://sikachu.com/?p=716</guid>
		<description><![CDATA[Just got the news in Twitter today that Ruby 1.9.2 is out! This is the Ruby that we&#8217;ve been long-awaits which shouldn&#8217;t have any strange compatibility issue with the upcoming Rails 3, and wayyyy faster than 1.8.7. The timing couldn&#8217;t be better, as we&#8217;re celebrating #whyday (@celebratewhyday) tomorrow (Aug 19.) On that day, you would [...]]]></description>
			<content:encoded><![CDATA[<p>Just got the news in Twitter today that <a href="http://twitter.com/yugui/status/21499615787" target="_blank">Ruby 1.9.2 is out</a>! This is the Ruby that we&#8217;ve been long-awaits which shouldn&#8217;t have any strange compatibility issue with the upcoming Rails 3, and wayyyy faster than 1.8.7.</p>
<p>The timing couldn&#8217;t be better, as we&#8217;re celebrating <a href="http://whyday.org/" target="_blank">#whyday</a> (<a href="http://twitter.com/celebratewhyday" target="_blank">@celebratewhyday</a>) tomorrow (Aug 19.) On that day, you would get hacking on something great! It&#8217;s the best time to trying to test out your application, or hacking something, using Ruby 1.9.2.</p>
<p>So what&#8217;re you waiting for? Download it, install it, and running your application on it. <a href="http://www.ruby-lang.org/en/news/2010/08/18/ruby-1-9-2-is-released/" target="_blank">Go grab it while it&#8217;s hot!</a></p>
<p>By the way, if you&#8217;re using the awesome <a href="http://rvm.beginrescueend.com/" target="_blank">RVM</a> (And why shouldn&#8217;t you??) Just go ahead and do:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">rvm update <span style="color: #660033;">--head</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> rvm reload <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> rvm <span style="color: #c20cb9; font-weight: bold;">install</span> 1.9.2 <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> rvm 1.9.2 <span style="color: #660033;">--default</span></pre></div></div>

<p>And have fun riding on this shiny Ruby! I warn you, it&#8217;s slippery. <img src='http://sikachu.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://sikachu.com/2010/08/ruby-1-9-2-is-out/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>เจออะไรใน Rails 3: เพิ่มพลัง ActiveRecord#find (ตอนที่ 2)</title>
		<link>http://sikachu.com/2010/02/rails-3-activerecord-find-part-2/</link>
		<comments>http://sikachu.com/2010/02/rails-3-activerecord-find-part-2/#comments</comments>
		<pubDate>Fri, 12 Feb 2010 01:15:59 +0000</pubDate>
		<dc:creator>Sikachu!</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Rails 3]]></category>

		<guid isPermaLink="false">http://sikachu.com/?p=442</guid>
		<description><![CDATA[เจออะไรใน Rails 3 เป็นสกู๊ปพิเศษสำหรับนำเสนอสิ่งใหม่ๆ ที่จะมีเพิ่มขึ้นมาใน Ruby on Rails 3.0 หลังจากที่ผมนำเสนอเรื่องของการ deprecate options ทั้งหมดของ ActiveRecord#find แล้วเปลี่ยนเป็นเมธอดที่ทำหน้าที่คล้ายๆ กับ named_scope กันไปแล้ว ตอนนี้ผมไปเจอข้อมูลจาก @lifo ซึ่งพูดเพิ่มเติมในเรื่องของเมธอดที่จะถูก deprecated และเมธอดที่ให้ใช้แทนครับ สิ่งที่จะถูกถอดออกไป การเรียกใช้ #find โดยมี option hash นั้นจะ deprecated ออกไป User.find&#40;:first, :where =&#62; &#123;:status =&#62; &#34;suspended&#34;&#125;&#41; แต่อย่างไรก็ตาม #find method จะยังคงอยู่ โดยคุณสามารถใช้มันเพื่อหา record ตาม ID ได้แทน เช่น User.find&#40;1&#41; User.find&#40;3,4,5&#41; # ให้ argument เป็น array [...]]]></description>
			<content:encoded><![CDATA[<p><em><strong>เจออะไรใน Rails 3</strong> เป็นสกู๊ปพิเศษสำหรับนำเสนอสิ่งใหม่ๆ ที่จะมีเพิ่มขึ้นมาใน Ruby on Rails 3.0</em></p>
<p>หลังจากที่ผมนำเสนอเรื่องของการ deprecate options ทั้งหมดของ <code>ActiveRecord#find</code> แล้วเปลี่ยนเป็นเมธอดที่ทำหน้าที่คล้ายๆ กับ <code>named_scope</code> กันไปแล้ว ตอนนี้ผมไปเจอข้อมูลจาก <a href="http://m.onkey.org/2010/1/22/active-record-query-interface" target="_blank">@lifo</a> ซึ่งพูดเพิ่มเติมในเรื่องของเมธอดที่จะถูก deprecated และเมธอดที่ให้ใช้แทนครับ</p>
<h3>สิ่งที่จะถูกถอดออกไป</h3>
<p>การเรียกใช้ <code>#find</code> โดยมี option hash นั้นจะ deprecated ออกไป</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">User.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:first</span>, <span style="color:#ff3333; font-weight:bold;">:where</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span>:status <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;suspended&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>แต่อย่างไรก็ตาม <code>#find</code> method จะยังคงอยู่ โดยคุณสามารถใช้มันเพื่อหา record ตาม ID ได้แทน เช่น</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">User.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>
User.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">3</span>,<span style="color:#006666;">4</span>,<span style="color:#006666;">5</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;"># ให้ argument เป็น array ได้แล้ว</span></pre></div></div>

<p>การใช้ <code>#find(:first), #find(:all)</code> จะถูกแทนที่ด้วยเมธอด <code>#first, #all</code></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">User.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:first</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;"># =&gt; deprecated</span>
User.<span style="color:#9900CC;">first</span> <span style="color:#008000; font-style:italic;"># use this</span></pre></div></div>

<p>และการใช้เมธอดสำหรับการคำนวณทุกอันนั้น ก็จะไม่รับ argument ที่เป็น hash แล้วเช่นกัน</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">User.<span style="color:#9900CC;">count</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:id</span>, <span style="color:#ff3333; font-weight:bold;">:conditions</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span>:status <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;suspended&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;"># =&gt; deprecated</span>
User.<span style="color:#9900CC;">where</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#123;</span>:status <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;suspended&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">count</span> <span style="color:#008000; font-style:italic;"># use this</span></pre></div></div>

<p>แต่ยังคงยกเว้น <code>#count</code> ที่ยังคงรับตัวเลือก <code>:distinct</code> อยู่</p>
<p>นอกจากนั้นการกำหนดค่า <code>named_scope</code> โดยใช้ option hash และเมธอด <code>#scoped_by_xxx</code> และ <code>#default_scope</code> ก็ถูกถอดออกเช่นกัน</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># all lines here are deprecated</span>
named_scope <span style="color:#ff3333; font-weight:bold;">:suspended</span>, <span style="color:#ff3333; font-weight:bold;">:conditions</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:status</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;suspended&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
default_scope <span style="color:#ff3333; font-weight:bold;">:conditions</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;status != 'deleted'&quot;</span>
User.<span style="color:#9900CC;">scoped_by_status</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;suspended&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<h3>เมธอดใหม่ทั้งหมด</h3>
<p>ActiveRecord ใน Rails 3 นั้น จะมีการเพิ่มเมธอดเหล่านี้</p>
<ul>
<li>where</li>
<li>having</li>
<li>select</li>
<li>group</li>
<li>order</li>
<li>limit</li>
<li>offset</li>
<li>joins</li>
<li>includes</li>
<li>lock</li>
<li>readonly</li>
<li>from</li>
</ul>
<p>เมธอดทุกอย่างนั้นสามารถ chain เข้าหากันได้ เพราะฉะนั้นคุณก็จะสามารถทำอย่างนี้ได้</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">User.<span style="color:#9900CC;">where</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:status</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;suspended&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">includes</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:event_log</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">limit</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">15</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">order</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>
User.<span style="color:#9900CC;">where</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;">=&gt;</span> <span style="color:#996600;">&quot;john_doe&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">joins</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:profile</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>และเนื่องจาก result ของมันเป็น lazy-loading หมด ทำให้คุณสามารถนำมันไปเก็บในตัวแปร หรือว่าเพิ่ม condition เข้าไปทีหลังได้ โดย SQL จะถูกส่งไปยัง server เมื่อคุณเรียกใช้ <code>#each, #first, #all</code> เท่านั้น</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">suspended_users = User.<span style="color:#9900CC;">where</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:status</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;suspended&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
latest_suspended_users = suspended_users.<span style="color:#9900CC;">order</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;id DESC&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">limit</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">5</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
User.<span style="color:#9900CC;">where</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:status</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;suspended&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;"># ยังไม่ query</span>
User.<span style="color:#9900CC;">where</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:status</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;suspended&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">all</span> <span style="color:#008000; font-style:italic;"># query แล้ว!</span></pre></div></div>

<p>เพราะฉะนั้นถ้าคุณไม่สนใจในเรื่องของการ lazy loading และต้องการจะโหลดข้อมูลลงมาเก็บไว้ใน array ก่อน คุณก็เพียงแค่ใช้ #all ตามหลัง query ของคุณเท่านั้นเอง</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">suspended_users = User.<span style="color:#9900CC;">where</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:status</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;suspended&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">all</span> <span style="color:#008000; font-style:italic;"># ข้อมูลถูกเก็บใน suspended_users</span></pre></div></div>

<h3>named_scope ยังเปลี่ยนไปเป็น scope อีก</h3>
<p>นอกจากที่กล่าวมาข้างต้นนั้น ยังมีการเปลี่ยนชื่อเมธอดของ <code>named_scope</code> ให้เป็น <code>scope</code> และรองรับการใช้เมธอดใหม่ของ finder เพราะฉะนั้นจากโค้ด</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">named_scope <span style="color:#ff3333; font-weight:bold;">:suspended</span>, <span style="color:#ff3333; font-weight:bold;">:conditions</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:status</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;suspended&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>ก็ต้องเปลี่ยนไปเป็น</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">scope <span style="color:#ff3333; font-weight:bold;">:suspended</span>, where<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:status</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;suspended&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>นอกจากนั้นยังมีการเพิ่มเติม <code>with_scope</code> และ <code>with_exclusive_scope</code> ซึ่งโค้ดในบล็อกที่ให้มานั้นจะมี scope ตามที่ระบุไว้แทน</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">with_scope<span style="color:#006600; font-weight:bold;">&#40;</span>where<span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#ff3333; font-weight:bold;">:status</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'suspended'</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  ...
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">with_exclusive_scope<span style="color:#006600; font-weight:bold;">&#40;</span>User.<span style="color:#9900CC;">online</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  ...
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<h3>แล้วเมื่อไรจะต้องเปลี่ยนละเนี่ย &#8230;</h3>
<p>คุณไม่จำเป็นต้องรีบเปลี่ยนโค้ดในส่วนของการใช้ finder เพราะใน Rails 3.0 นั้น ทุกอย่างยังคงจะใช้ได้ตามปกติ โดยที่มีกำหนดการว่าเมธอดเก่าๆ นั้นจะถูกนำออกไปใน Rails 3.2 ครับ เพราะฉะนั้นคุณก็ยังคงสามารถใช้โค้ดเก่าๆ ในการ query ข้อมูลมาได้ แต่ผมแนะนำว่าค่อยๆ ไล่เปลี่ยนไป ก็จะทำให้โค้ดของคุณพร้อมสำหรับการ upgrade ไปยัง version ใหม่ๆ ครับ <img src='http://sikachu.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://sikachu.com/2010/02/rails-3-activerecord-find-part-2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>เจออะไรใน Rails 3: เข้ารหัส cookie และสร้าง cookie ที่ไม่มีวันหมดอายุอย่างง่ายๆ</title>
		<link>http://sikachu.com/2010/02/rails-3-signed-cookie-and-permanent-cookie/</link>
		<comments>http://sikachu.com/2010/02/rails-3-signed-cookie-and-permanent-cookie/#comments</comments>
		<pubDate>Thu, 11 Feb 2010 01:15:20 +0000</pubDate>
		<dc:creator>Sikachu!</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Rails 3]]></category>

		<guid isPermaLink="false">http://sikachu.com/?p=420</guid>
		<description><![CDATA[เจออะไรใน Rails 3 เป็นสกู๊ปพิเศษสำหรับนำเสนอสิ่งใหม่ๆ ที่จะมีเพิ่มขึ้นมาใน Ruby on Rails 3.0 เมื่อก่อนนี้ ถ้าหากเราต้องการที่จะทำ cookie ที่ไม่มีวันหมดอายุ (เช่น remember me) เราจำเป็นที่จะต้องกำหนดเวลาหมดอายุของ cookie เอง เช่น cookies&#91;:authorization_key&#93; = &#123; :value =&#62; @user.authorization_key, :expires =&#62; 20.years.from_now.utc &#125; แต่หลังจาก commit ของ DHH อันนี้ ทำให้เราสามารถเขียนใหม่ได้เป็น cookies.permanent&#91;:authorization_key&#93; = @user.authorization_key นอกจากนั้น เพื่อเพิ่มความปลอดภัยให้กับ Cookie เราก็ยังสามารถที่จะจับมัน signed หรือพูดง่ายๆ ก็คือเข้ารหัสมันไว้ได้ โดยการใช้เมธอด signed cookies.signed&#91;:user_id&#93; = @user.id ที่เยี่ยมไปกว่านั้นคือ การที่ทั้งสองเมธอดนั้นสามารถเชื่อมต่อกันได้ เพราะฉะนั้นตัวอย่างข้างต้นสำหรับ remember [...]]]></description>
			<content:encoded><![CDATA[<p><em><strong>เจออะไรใน Rails 3</strong> เป็นสกู๊ปพิเศษสำหรับนำเสนอสิ่งใหม่ๆ ที่จะมีเพิ่มขึ้นมาใน Ruby on Rails 3.0</em></p>
<p>เมื่อก่อนนี้ ถ้าหากเราต้องการที่จะทำ cookie ที่ไม่มีวันหมดอายุ (เช่น remember me) เราจำเป็นที่จะต้องกำหนดเวลาหมดอายุของ cookie เอง เช่น</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">cookies<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:authorization_key</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#006600; font-weight:bold;">&#123;</span>
  <span style="color:#ff3333; font-weight:bold;">:value</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0066ff; font-weight:bold;">@user</span>.<span style="color:#9900CC;">authorization_key</span>,
  <span style="color:#ff3333; font-weight:bold;">:expires</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">20</span>.<span style="color:#9900CC;">years</span>.<span style="color:#9900CC;">from_now</span>.<span style="color:#9900CC;">utc</span>
<span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>แต่หลังจาก commit ของ DHH <a href="http://github.com/rails/rails/commit/0200e20f148c96afceeebc4da7b5985643f9f707" target="_blank">อันนี้</a> ทำให้เราสามารถเขียนใหม่ได้เป็น</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">cookies.<span style="color:#9900CC;">permanent</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:authorization_key</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#0066ff; font-weight:bold;">@user</span>.<span style="color:#9900CC;">authorization_key</span></pre></div></div>

<p>นอกจากนั้น เพื่อเพิ่มความปลอดภัยให้กับ Cookie เราก็ยังสามารถที่จะจับมัน signed หรือพูดง่ายๆ ก็คือเข้ารหัสมันไว้ได้ โดยการใช้เมธอด <code>signed</code></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">cookies.<span style="color:#9900CC;">signed</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:user_id</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#0066ff; font-weight:bold;">@user</span>.<span style="color:#9900CC;">id</span></pre></div></div>

<p>ที่เยี่ยมไปกว่านั้นคือ การที่ทั้งสองเมธอดนั้นสามารถเชื่อมต่อกันได้ เพราะฉะนั้นตัวอย่างข้างต้นสำหรับ remember me นั้น เราก็สามารถแก้โค้ดให้เป็น</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">cookies.<span style="color:#9900CC;">signed</span>.<span style="color:#9900CC;">permanent</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:authorization_key</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#0066ff; font-weight:bold;">@user</span>.<span style="color:#9900CC;">authorization_key</span></pre></div></div>

<p>ซึ่งจะทำให้ <code>cookies[:authorization_key]</code> นั้นถูกเข้ารหัสก่อนที่จะส่งไปยัง web browser</p>
<h3>แล้วมันเข้ารหัสกับอะไรล่ะ?</h3>
<p>เป็นคำถามที่ดีครับ! Application ใหม่ๆ ที่ถูกสร้างมานั้นจะมีไฟล์ชื่อ <code>cookie_verification_secret.rb</code> อยู่ใน <code>config/initializer</code> ซึ่งมันจะมี key ที่ใช้ในการเข้ารหัสเมื่อใช้คำสั่ง <code>cookies.signed</code> อยู่ เพราะฉะนั้นถ้าเป็น application เก่า สิ่งที่คุณต้องทำก็แค่สร้างไฟล์นี้ขึ้นมาแล้วเพิ่มบรรทัดนี้</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#6666ff; font-weight:bold;">ActionController::Base</span>.<span style="color:#9900CC;">cookie_verification_secret</span> = <span style="color:#996600;">'z3cret'</span>;</pre></div></div>

<p>อย่าลืมเปลี่ยน token ของคุณให้เป็นข้อความยากๆ ที่เดาไม่ได้นะครับ <img src='http://sikachu.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<h3>แล้วถ้ามีการแก้ไข cookie ที่ถูกเข้ารหัสละ?</h3>
<p>มันก็จะ verify cookie ไม่ผ่าน แล้วก็จะโยน exception <code>ActiveSupport::MessageVerifier::InvalidSignature</code> มาครับ</p>
<p>ยังมีอะไรอีกเพียบเลยครับที่ยังเหลือให้เขียนถึง อย่าลืมติดตามอ่านตอนต่อๆ ไปนะครับ <img src='http://sikachu.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://sikachu.com/2010/02/rails-3-signed-cookie-and-permanent-cookie/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

