<?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>Cyberpunk &#187; Ruby on Rails</title>
	<atom:link href="http://punk.tw/tag/ruby-on-rails/feed" rel="self" type="application/rss+xml" />
	<link>http://punk.tw</link>
	<description>網際叛客：學習心得、Web 2.0與網路觀察、Google應用與技術、Ruby on Rails, Google App Engine</description>
	<lastBuildDate>Tue, 07 Feb 2012 08:42:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>如何將現有的Ruby on Rails專案移植到Heroku？</title>
		<link>http://punk.tw/2009/09/27/%e5%a6%82%e4%bd%95%e5%b0%87%e7%8f%be%e6%9c%89%e7%9a%84ruby-on-rails%e5%b0%88%e6%a1%88%e7%a7%bb%e6%a4%8d%e5%88%b0heroku%ef%bc%9f</link>
		<comments>http://punk.tw/2009/09/27/%e5%a6%82%e4%bd%95%e5%b0%87%e7%8f%be%e6%9c%89%e7%9a%84ruby-on-rails%e5%b0%88%e6%a1%88%e7%a7%bb%e6%a4%8d%e5%88%b0heroku%ef%bc%9f#comments</comments>
		<pubDate>Sun, 27 Sep 2009 15:16:40 +0000</pubDate>
		<dc:creator>deduce</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://punk.tw/?p=960</guid>
		<description><![CDATA[今天試著將兩個手頭上已經沒在密集維護但偶有流量的網站移植到Heroku上來降低維護的成本，過程中相當輕鬆地將專案無痛轉移到Heroku之外，連資料都一起順利轉移過去了，以下是轉移步驟的簡介。 將專案納入git版本控管中，指令是git init 建立一個新的Heroku空間，指令是heroku create或是heroku create YOUR_APP_NAME 在config/environment.rb加上$KCODE='u'（避免中文編碼問題，我是加在第一行） 將所有檔案丟到Heroku上：git add . ; git commit -a -m "deploy to Heroku" ; git push heroku master 步驟其實很簡單，官方文件上應該也能找到更詳盡的教學，另外要注意的是，如果您的專案擁有自己的網址，希望能一併指到Heroku，還需要注意下列事項： 將DNS中的CNAME 指到proxy.heroku.com，或是將A Record指到75.101.163.44, 75.101.145.87, 174.129.212.2，Heroku的程式在新增custom domain name時會先檢查您的DNS Records。 請在Heroku的帳戶管理中輸入您的信用卡資料（custom domain name雖然是免費的服務，但要驗證信用卡） 使用heroku addons:add custom_domains指令啟用自定網址 使用heroku domains:add YOUR_DOMAIN_NAME來新增網址 當然，上述步驟可以不透過指令，直接在Heroku的管理介面完成。 我自己在移植的過程中遇到兩個問題，第一個是資料丟上Heroku之後都變成亂碼，但我的MySQL編碼就是UTF8了，實在沒道理，因此就嘗試加上$KCODE的設定，沒想到馬上管用。 第二個問題是資料如何丟到Heroku，步驟也很簡單： sudo gem install tap（tap就是讓你可以無痛轉移的關鍵Ruby gem） heroku db:push（如此一來就會把資料庫，包括schema、data、index完整送上Heroku） 其中要注意的是，Heroku設計的原意是讓你可以將開發過程中已有的測試資料（development環境）丟上Heroku一併測試，因此我目前沒有找到如何讀取production環境的參數，我自己的作法是在config/database.yml中將development的設定改為production的設定，讓程式去讀取production。 或是你也可以使用指令的方式指定任意的資料庫：heroku db:push [...]]]></description>
			<content:encoded><![CDATA[<p>今天試著將兩個手頭上已經沒在密集維護但偶有流量的網站移植到Heroku上來降低維護的成本，過程中相當輕鬆地將專案無痛轉移到Heroku之外，連資料都一起順利轉移過去了，以下是轉移步驟的簡介。</p>
<ol>
<li>將專案納入git版本控管中，指令是git init</li>
<li>建立一個新的Heroku空間，指令是heroku create或是heroku create YOUR_APP_NAME</li>
<li>在config/environment.rb加上$KCODE='u'（避免中文編碼問題，我是加在第一行）</li>
<li>將所有檔案丟到Heroku上：git add . ; git commit -a -m "deploy to Heroku" ; git push heroku master</li>
</ol>
<p>步驟其實很簡單，官方文件上應該也能找到更詳盡的教學，另外要注意的是，如果您的專案擁有自己的網址，希望能一併指到Heroku，還需要注意下列事項：</p>
<ol>
<li><span style="background-color: #ffffff;">將DNS中的CNAME 指到proxy.heroku.com，或是將A Record指到75.101.163.44, 75.101.145.87, 174.129.212.2，Heroku的程式在新增custom domain name時會先檢查您的DNS Records。</span></li>
<li><span style="background-color: #ffffff;">請在Heroku的帳戶管理中輸入您的信用卡資料（custom domain name雖然是免費的服務，但要驗證信用卡）</span></li>
<li><span style="background-color: #ffffff;">使用heroku addons:add custom_domains指令啟用自定網址</span></li>
<li><span style="background-color: #ffffff;">使用heroku domains:add YOUR_DOMAIN_NAME來新增網址</span></li>
</ol>
<p>當然，上述步驟可以不透過指令，直接在Heroku的管理介面完成。</p>
<p>我自己在移植的過程中遇到兩個問題，第一個是資料丟上Heroku之後都變成亂碼，但我的MySQL編碼就是UTF8了，實在沒道理，因此就嘗試加上$KCODE的設定，沒想到馬上管用。</p>
<p>第二個問題是資料如何丟到Heroku，步驟也很簡單：</p>
<ol>
<li>sudo gem install tap（tap就是讓你可以無痛轉移的關鍵Ruby gem）</li>
<li>heroku db:push（如此一來就會把資料庫，包括schema、data、index完整送上Heroku）</li>
</ol>
<p>其中要注意的是，Heroku設計的原意是讓你可以將開發過程中已有的測試資料（development環境）丟上Heroku一併測試，因此我目前沒有找到如何讀取production環境的參數，我自己的作法是在config/database.yml中將development的設定改為production的設定，讓程式去讀取production。</p>
<p>或是你也可以使用指令的方式指定任意的資料庫：heroku db:push mysql://YOUR_USER_NAME:YOUR_PASSWORD@localhost/DB_NAME</p>
<p>P.S. 當然，如果嫌Heroku不好用或是其他原因想要搬家，只要下heroku db:pull，就會把雲端上的資料撈回來放在development環境，我自己是用SQLite，資料就會倒進SQLite了，是不是很方便呢？:p</p>
]]></content:encoded>
			<wfw:commentRss>http://punk.tw/2009/09/27/%e5%a6%82%e4%bd%95%e5%b0%87%e7%8f%be%e6%9c%89%e7%9a%84ruby-on-rails%e5%b0%88%e6%a1%88%e7%a7%bb%e6%a4%8d%e5%88%b0heroku%ef%bc%9f/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>如何在Heroku上架設免費、開放源碼的專案管理軟體：Redmine</title>
		<link>http://punk.tw/2009/09/27/%e5%a6%82%e4%bd%95%e5%9c%a8heroku%e4%b8%8a%e6%9e%b6%e8%a8%ad%e5%85%8d%e8%b2%bb%e3%80%81%e9%96%8b%e6%94%be%e6%ba%90%e7%a2%bc%e7%9a%84%e5%b0%88%e6%a1%88%e7%ae%a1%e7%90%86%e8%bb%9f%e9%ab%94%ef%bc%9aredmi</link>
		<comments>http://punk.tw/2009/09/27/%e5%a6%82%e4%bd%95%e5%9c%a8heroku%e4%b8%8a%e6%9e%b6%e8%a8%ad%e5%85%8d%e8%b2%bb%e3%80%81%e9%96%8b%e6%94%be%e6%ba%90%e7%a2%bc%e7%9a%84%e5%b0%88%e6%a1%88%e7%ae%a1%e7%90%86%e8%bb%9f%e9%ab%94%ef%bc%9aredmi#comments</comments>
		<pubDate>Sun, 27 Sep 2009 08:14:08 +0000</pubDate>
		<dc:creator>deduce</dc:creator>
				<category><![CDATA[專案管理]]></category>
		<category><![CDATA[Heroku]]></category>
		<category><![CDATA[redmine]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://punk.tw/2009/09/27/943.html</guid>
		<description><![CDATA[Redmine是一套使用Ruby on Rails所開發的軟體專案管理系統，我自己使用Redmine來追蹤、管理自己手邊的軟體專案已經一年多了，深深感受到Redmine所帶來的便利性。雖然軟體專案管理最終是「人」的管理以及制度如何落實的學問，但一套好的管理工具還是可以有效地提昇團隊合作效率，並且有助於追蹤工作進度。關於Redmine的介紹，改天再另外寫文章介紹（當然網路上應該有許多參考）。 這篇文章著重在如何利用Heroku（號稱提供Rails的雲端運算環境、簡單的部署流程並支援Git）架設免費的Redmine專案管理環境。 環境方面您至少要擁有Rails開發環境、Git版本控制軟體、Ruby gem: Heroku以及Heroku帳號，至於其他需要安裝的Ruby Gems請參考Redmine本身的需求。 svn co http://redmine.rubyforge.org/svn/trunk redmine cd redmine 編輯.gitignore並移除下列兩行內容config/initializers/session_store.rbpublic/plugin_assets rake config/initializers/session_store.rb mv config/database.yml.example config/database.yml rake db:migrate RAILS_ENV="demo"（這一段我是用SQLite的資料庫，是Redmine預設的，有跑這一段好像才會作一些初始化的動作，我就沒仔細去研究如何繞過這一段了，您可以自己在local端開個mysql來完成這個項目） git init; echo .svn >> .gitignore ; git add . ; git commit -a -m "Init Redmine project" svn propset svn:ignore .git .（請svn忽略.git目錄，請注意.git之後還有一個 .） heroku create（或是heroku create your_app_name，若沒指定名稱則會由Heroku隨機產生；完成指令後可以下heroku info觀看相關資訊） git push heroku [...]]]></description>
			<content:encoded><![CDATA[<p>Redmine是一套使用Ruby on Rails所開發的軟體專案管理系統，我自己使用Redmine來追蹤、管理自己手邊的軟體專案已經一年多了，深深感受到Redmine所帶來的便利性。雖然軟體專案管理最終是「人」的管理以及制度如何落實的學問，但一套好的管理工具還是可以有效地提昇團隊合作效率，並且有助於追蹤工作進度。關於Redmine的介紹，改天再另外寫文章介紹（當然網路上應該有許多參考）。</p>
<p>這篇文章著重在如何利用Heroku（號稱提供Rails的雲端運算環境、簡單的部署流程並支援Git）架設免費的Redmine專案管理環境。</p>
<p>環境方面您至少要擁有Rails開發環境、Git版本控制軟體、Ruby gem: Heroku以及Heroku帳號，至於其他需要安裝的Ruby Gems請參考Redmine本身的需求。</p>
<ol>
<li>svn co http://redmine.rubyforge.org/svn/trunk redmine</li>
<li>cd redmine</li>
<li>編輯.gitignore並移除下列兩行內容<br />config/initializers/session_store.rb<br />public/plugin_assets</li>
<li>rake config/initializers/session_store.rb</li>
<li>mv config/database.yml.example config/database.yml</li>
<li>rake db:migrate RAILS_ENV="demo"（這一段我是用SQLite的資料庫，是Redmine預設的，有跑這一段好像才會作一些初始化的動作，我就沒仔細去研究如何繞過這一段了，您可以自己在local端開個mysql來完成這個項目）</li>
<li>git init; echo .svn >> .gitignore ; git add . ; git commit -a -m "Init Redmine project"</li>
<li>svn propset svn:ignore .git .（請svn忽略.git目錄，請注意.git之後還有一個 .）</li>
<li>heroku create（或是heroku create your_app_name，若沒指定名稱則會由Heroku隨機產生；完成指令後可以下heroku info觀看相關資訊）</li>
<li>git push heroku master</li>
<li>heroku rake db:migrate</li>
</ol>
<p>此時在瀏覽器中造訪您的Heroku網址應該就會看到正常執行的畫面了。Redmine預設的管理員帳號密碼皆為admin，您可以直接登入開始使用Redmine來做為您的軟體專案管理系統。</p>
<p>P.S. 依照上列步驟安裝後尚無法使用E-mail發信功能（E-mail notifications for any updates），我在自己的主機上是習慣使用Gmail（或是Google Apps下的Mail）來發信免去自行維護郵件主機的麻煩，我猜Heroku上也可以如法炮製，由於這部份我還沒有研究，等下一篇文章再與各位分享。</p>
]]></content:encoded>
			<wfw:commentRss>http://punk.tw/2009/09/27/%e5%a6%82%e4%bd%95%e5%9c%a8heroku%e4%b8%8a%e6%9e%b6%e8%a8%ad%e5%85%8d%e8%b2%bb%e3%80%81%e9%96%8b%e6%94%be%e6%ba%90%e7%a2%bc%e7%9a%84%e5%b0%88%e6%a1%88%e7%ae%a1%e7%90%86%e8%bb%9f%e9%ab%94%ef%bc%9aredmi/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>在Rails 2.0中如何避免在URL使用資料流水號</title>
		<link>http://punk.tw/2008/04/29/%e5%9c%a8rails-20%e4%b8%ad%e5%a6%82%e4%bd%95%e9%81%bf%e5%85%8d%e5%9c%a8url%e4%bd%bf%e7%94%a8%e8%b3%87%e6%96%99%e6%b5%81%e6%b0%b4%e8%99%9f</link>
		<comments>http://punk.tw/2008/04/29/%e5%9c%a8rails-20%e4%b8%ad%e5%a6%82%e4%bd%95%e9%81%bf%e5%85%8d%e5%9c%a8url%e4%bd%bf%e7%94%a8%e8%b3%87%e6%96%99%e6%b5%81%e6%b0%b4%e8%99%9f#comments</comments>
		<pubDate>Mon, 28 Apr 2008 19:23:18 +0000</pubDate>
		<dc:creator>deduce</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[ActiveRecord]]></category>

		<guid isPermaLink="false">http://punk.tw/2008/04/29/857.html</guid>
		<description><![CDATA[<p>我曾寫過一篇「<a href="http://punk.tw/2007/10/06/340.html">Rails: 建立Permalink，避免流水號洩漏網站資料</a>」針對有人擔心（或質疑）Ruby on Rails中以資料表主鍵流水號作為URL中的參數，會導致網站洩漏某些不希望被外界一眼看穿的資訊，例如使用者總數或文章總數之類的數據，因此簡單地透過Model在存取資料庫時的before_create方法建立每一筆資料的unique key來避免這個問題。</p> ]]></description>
			<content:encoded><![CDATA[<p>我曾寫過一篇「<a href="http://punk.tw/2007/10/06/340.html">Rails: 建立Permalink，避免流水號洩漏網站資料</a>」針對有人擔心（或質疑）Ruby on Rails中以資料表主鍵流水號作為URL中的參數，會導致網站洩漏某些不希望被外界一眼看穿的資訊，例如使用者總數或文章總數之類的數據，因此簡單地透過Model在存取資料庫時的before_create方法建立每一筆資料的unique key來避免這個問題。</p>
<p>之前使用Rails 1.2.x版本時根據我當時的作法並無不妥，因為我自己的coding習慣是會完整地指定controller, action以及id，並在對應的action中使用find_by_key的方法來找到資料。</p>
<p>然而，升級到Rails 2.0後便會出現許多麻煩，例如預設產生的scaffold必須修改許多地方，才能讓Model.find_by_key、redirect_to <strong>post_url</strong>(@post)之類的方法正確運作。</p>
<p>於是我一直到前陣子才找到應該比較正規的作法：在Model中使用to_param方法。</p>
<p>to_param(): Enables Active Record objects to be used as URL parameters in Action Pack automatically.</p>
<p>這麼好用的方法我居然不是一開始就發現，只能說相見恨晚，好險我開發Rails 2.0的時間還不算長，沒走到太多冤枉路。要實踐本標題所說的在Rails 2.0中如何避免在URL使用資料流水號便相當簡單！</p>
<p>1. 建立Scaffold：（通常會對key這個欄位建立index並設定字串長度limit，別忘了:p）<br />script/generate post title:string content:text key:string</p>
<p>2. 將Post的migration寫進資料庫後修改Post model如下：</p>
<p>
<pre class="prettyprint">
class Post < ActiveRecord::Base
  before_create :generate_key

<strong>  def self.find(*args)
    if args.first.is_a?(String)
      find_by_key(args.shift, *args) or raise ActiveRecord::RecordNotFound
    else
      super
    end
  end

  def to_param
    key
  end</strong>

  protected
  # 我前一篇文章產生key的方式不太一樣，這應該是個人習慣。
  # 另外，其實亂碼的網址沒有SEO的效益，建議還是弄個slug吧！
  def generate_key
    require 'digest/sha1'
    self.key = Digest::SHA1.hexdigest( Time.now.to_s.split(//).sort_by {rand}.join )[15..24]
  end
end
</pre>
</p>
<p>就這樣！<strong>概念很簡單，就是不再用ID當查詢的參數，全面改用key</strong>啦！關鍵就在於使用to_param()方法以及對find做進一步的判斷，因為我<strong>除了Model.find(:all)之類的Symbol，其餘的CRUD都是傳入Key作為參數</strong>，因此在此僅簡單地判斷是否為字串。</p>
<p>如此一來，不管你是什麼user_posts_url, new_user_post_url都可以輕鬆地直接以ActiveRecord Object傳遞，起碼我之前曾經嘗試用edit_post_url(@post.key)這種方式傳值，非常麻煩、要改Controller又要改View，現在用了to_param()，什麼都不用改了！又優雅又簡單，寫起來更加快樂、效率也提昇了:p</p>
<p>P.S. 其實在Rails 1.2.x應該也是這麼做比較優雅，不限於2.0</p>
]]></content:encoded>
			<wfw:commentRss>http://punk.tw/2008/04/29/%e5%9c%a8rails-20%e4%b8%ad%e5%a6%82%e4%bd%95%e9%81%bf%e5%85%8d%e5%9c%a8url%e4%bd%bf%e7%94%a8%e8%b3%87%e6%96%99%e6%b5%81%e6%b0%b4%e8%99%9f/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ruby Plus：Ruby與Rails的免費教學影片</title>
		<link>http://punk.tw/2008/01/15/ruby-plus%ef%bc%9aruby%e8%88%87rails%e7%9a%84%e5%85%8d%e8%b2%bb%e6%95%99%e5%ad%b8%e5%bd%b1%e7%89%87</link>
		<comments>http://punk.tw/2008/01/15/ruby-plus%ef%bc%9aruby%e8%88%87rails%e7%9a%84%e5%85%8d%e8%b2%bb%e6%95%99%e5%ad%b8%e5%bd%b1%e7%89%87#comments</comments>
		<pubDate>Tue, 15 Jan 2008 02:04:45 +0000</pubDate>
		<dc:creator>deduce</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://punk.tw/2008/01/15/607.html</guid>
		<description><![CDATA[Ruby Plus是個類似Railscasts的影片收藏站，提供各種Rails（或Ruby）相關的教學影片，例如：如何在Rails 2.0中上傳圖片、如何開發Rails Plugins，或是最近比較熱門的RESTful目前也有幾篇教學影片可以參考。 註：Railscasts因為出來得比較早，教學影片較多，也可參考參考。:D]]></description>
			<content:encoded><![CDATA[<p><a href="http://punk.tw/wp-content/uploads/2008/01/image35.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="74" alt="image" src="http://punk.tw/wp-content/uploads/2008/01/image-thumb34.png" width="318" border="0"></a> </p>
<p><a href="http://www.rubyplus.org/">Ruby Plus</a>是個類似<a href="http://railscasts.com/">Railscasts</a>的影片收藏站，提供各種Rails（或Ruby）相關的教學影片，例如：<a href="http://www.rubyplus.org/episodes/31-How-to-upload-images-in-Rails-2-.html">如何在Rails 2.0中上傳圖片</a>、<a href="http://www.rubyplus.org/episodes/10-How-to-develop-Plugins-in-Rails-The-Basics.html">如何開發Rails Plugins</a>，或是最近比較熱門的RESTful目前也有幾篇教學影片<a href="http://www.rubyplus.org/episodes/archive">可以參考</a>。</p>
<p>註：Railscasts因為出來得比較早，教學影片較多，也可參考參考。:D</p>
]]></content:encoded>
			<wfw:commentRss>http://punk.tw/2008/01/15/ruby-plus%ef%bc%9aruby%e8%88%87rails%e7%9a%84%e5%85%8d%e8%b2%bb%e6%95%99%e5%ad%b8%e5%bd%b1%e7%89%87/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Heroku：線上撰寫Rails程式初體驗</title>
		<link>http://punk.tw/2007/11/20/heroku%ef%bc%9a%e7%b7%9a%e4%b8%8a%e6%92%b0%e5%af%abrails%e7%a8%8b%e5%bc%8f%e5%88%9d%e9%ab%94%e9%a9%97</link>
		<comments>http://punk.tw/2007/11/20/heroku%ef%bc%9a%e7%b7%9a%e4%b8%8a%e6%92%b0%e5%af%abrails%e7%a8%8b%e5%bc%8f%e5%88%9d%e9%ab%94%e9%a9%97#comments</comments>
		<pubDate>Tue, 20 Nov 2007 07:22:37 +0000</pubDate>
		<dc:creator>deduce</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Web2.0 Website]]></category>
		<category><![CDATA[Heroku]]></category>

		<guid isPermaLink="false">http://punk.tw/2007/11/20/443.html</guid>
		<description><![CDATA[Heroku是一個讓網頁開發人員可以在線上編輯Rails程式的網路服務，目前還在limited beta的階段，有興趣申請的讀者可以到申請網址登記排隊。上一篇文章提到Heroku的幾個特色，這篇文章就登入來玩玩看吧！ 初次登入之後會看到如下畫面： 此時尚未建立任何應用程式，點選建立程式之後會轉到另一個網址：http://edit.untitled-22289.heroku.com/，由於尚未對應用程式命名，網址是以流水號加上untitled的方式命名，這是可以設定的。（說不定還支援使用者自行指定domain name？或許將會是個付費服務）。 上圖是應用程式的設定畫面，可以自訂網址、支援協同編輯、匯入/匯出、程式版本快照（簡單的版本控制），至於Other裡面目前只有Destroy一項功能:p 因為內文都是一些截圖，使用起來對我而言也沒有太大的驚喜，所以先講結論吧！ 適合初學者環境都已經建好了，連資料庫都免費提供，完全不需要學習建置環境或是任何設定，就可以開始快快樂樂寫Rails。 適合拿來當維護環境手邊沒有電腦、沒有合適的開發環境、沒有安全的網路環境，又急著要修改？如果類似的服務可以提供https://的SSL環境，或許可以拿來玩玩看。 適合拿來推廣Rails技術高手想要推廣Rails，但手邊沒有現成的環境，登入Heroku馬上就可以開始demo Ruby on Rails的威力，新手看到一個留言板居然沒有幾行程式就搞定，一定會馬上被拐進來的:D 下圖即編輯程式的主畫面： 畫面的構成我就不多做介紹了，基本上在編輯畫面可以看到Rails app的目錄結構、檢視資料庫schema以及Rails的log file，點選右上角的圖示則會馬上跳轉到應用程式執行的頁面，讓你知道目前執行的狀況。 畫面左下角則有幾個寫Rails必備的指令工具：Generator, Console以及Rake 先打開Console模式來玩玩看，點選Console之後會跳到另一個網址，此時出現的是全螢幕的Console畫面，等等再建幾個Model來玩玩看： HelloWorld，建個留言板！ Code Generator的畫面： 執行之後也會告知執行結果： 稍微寫一下migration，使用編輯器的感覺是非常不順手，我的作業系統、Firefox都是英文環境，語系也設為en-us，但在編輯器裡面卻會出現游標與實際文字位置無法對齊的狀況，實在是相當奇妙。 先不管它，先migrate上去了！Rake的畫面也是純指令模式，這點就不太親切啦～建議開發團隊可以考慮NetBeans那種IDE懶人用法啊XD 看到這個畫面，我才想起我根本還沒設定database啊XD趕快看看database.yaml如下： 換句話說，heroku目前提供了database而且還是postgresql :p，說不定以後也會是個向使用者索取費用的服務。 接下來建立scaffold讓我的guestbook跑起來吧！ 建立完成之後，應該就可以馬上看到程式目前的畫面啦～！ 檢視資料庫 檢視資料庫的畫面可以看到目前有哪些資料表： 也可以直接點進去修改： 檢視Log file 點選各個訊息還可以看到更詳盡的資訊。]]></description>
			<content:encoded><![CDATA[<p>Heroku是一個讓網頁開發人員可以在線上編輯Rails程式的網路服務，目前還在limited beta的階段，有興趣申請的讀者可以到<a href="http://heroku.com/beta">申請網址</a>登記排隊。上一篇文章提到Heroku的幾個特色，這篇文章就登入來玩玩看吧！</p>
<p>初次登入之後會看到如下畫面：</p>
<p><a href="http://punk.tw/wp-content/uploads/2007/11/image10.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="222" alt="image" src="http://punk.tw/wp-content/uploads/2007/11/image-thumb9.png" width="432" border="0"></a> </p>
<p>此時尚未建立任何應用程式，點選建立程式之後會轉到另一個網址：<a title="http://edit.untitled-22289.heroku.com/" href="http://edit.untitled-22289.heroku.com/">http://edit.untitled-22289.heroku.com/</a>，由於尚未對應用程式命名，網址是以流水號加上untitled的方式命名，這是可以設定的。（說不定還支援使用者自行指定domain name？或許將會是個付費服務）。<br /><a href="http://punk.tw/wp-content/uploads/2007/11/image11.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="261" alt="image" src="http://punk.tw/wp-content/uploads/2007/11/image-thumb10.png" width="416" border="0"></a> </p>
<p>上圖是應用程式的設定畫面，可以自訂網址、支援協同編輯、匯入/匯出、程式版本快照（簡單的版本控制），至於Other裡面目前只有Destroy一項功能:p</p>
<p>因為內文都是一些截圖，使用起來對我而言也沒有太大的驚喜，所以先講結論吧！</p>
<ul>
<li>適合初學者<br />環境都已經建好了，連資料庫都免費提供，完全不需要學習建置環境或是任何設定，就可以開始快快樂樂寫Rails。</li>
<li>適合拿來當維護環境<br />手邊沒有電腦、沒有合適的開發環境、沒有安全的網路環境，又急著要修改？如果類似的服務可以提供https://的SSL環境，或許可以拿來玩玩看。</li>
<li>適合拿來推廣Rails<br />技術高手想要推廣Rails，但手邊沒有現成的環境，登入Heroku馬上就可以開始demo Ruby on Rails的威力，新手看到一個留言板居然沒有幾行程式就搞定，一定會馬上被拐進來的:D</li>
</ul>
<p>下圖即編輯程式的主畫面：</p>
<p><a href="http://punk.tw/wp-content/uploads/2007/11/image12.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="310" alt="image" src="http://punk.tw/wp-content/uploads/2007/11/image-thumb11.png" width="434" border="0"></a> </p>
<p>畫面的構成我就不多做介紹了，基本上在編輯畫面可以看到Rails app的目錄結構、檢視資料庫schema以及Rails的log file，點選右上角的圖示則會馬上跳轉到應用程式執行的頁面，讓你知道目前執行的狀況。</p>
<p>畫面左下角則有幾個寫Rails必備的指令工具：Generator, Console以及Rake</p>
<p><a href="http://punk.tw/wp-content/uploads/2007/11/image13.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="121" alt="image" src="http://punk.tw/wp-content/uploads/2007/11/image-thumb12.png" width="129" border="0"></a> </p>
<p>先打開Console模式來玩玩看，點選Console之後會跳到另一個網址，此時出現的是全螢幕的Console畫面，等等再建幾個Model來玩玩看：</p>
<p><a href="http://punk.tw/wp-content/uploads/2007/11/image14.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="163" alt="image" src="http://punk.tw/wp-content/uploads/2007/11/image-thumb13.png" width="303" border="0"></a> </p>
<h1>HelloWorld，建個留言板！</h1>
<p>Code Generator的畫面：</p>
<p><a href="http://punk.tw/wp-content/uploads/2007/11/image15.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="291" alt="image" src="http://punk.tw/wp-content/uploads/2007/11/image-thumb14.png" width="422" border="0"></a> </p>
<p>執行之後也會告知執行結果：</p>
<p><a href="http://punk.tw/wp-content/uploads/2007/11/image16.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="231" alt="image" src="http://punk.tw/wp-content/uploads/2007/11/image-thumb15.png" width="339" border="0"></a> </p>
<p>稍微寫一下migration，使用編輯器的感覺是非常不順手，我的作業系統、Firefox都是英文環境，語系也設為en-us，但在編輯器裡面卻會出現<strong>游標與實際文字位置無法對齊</strong>的狀況，實在是相當奇妙。</p>
<p><a href="http://punk.tw/wp-content/uploads/2007/11/image17.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="123" alt="image" src="http://punk.tw/wp-content/uploads/2007/11/image-thumb16.png" width="304" border="0"></a> </p>
<p>先不管它，先migrate上去了！Rake的畫面也是純指令模式，這點就不太親切啦～建議開發團隊可以考慮NetBeans那種IDE懶人用法啊XD</p>
<p><a href="http://punk.tw/wp-content/uploads/2007/11/image18.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="252" alt="image" src="http://punk.tw/wp-content/uploads/2007/11/image-thumb17.png" width="358" border="0"></a> </p>
<p>看到這個畫面，我才想起我根本還沒設定database啊XD趕快看看database.yaml如下：</p>
<p><a href="http://punk.tw/wp-content/uploads/2007/11/image19.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="116" alt="image" src="http://punk.tw/wp-content/uploads/2007/11/image-thumb18.png" width="203" border="0"></a> </p>
<p>換句話說，heroku目前提供了database而且還是postgresql :p，說不定以後也會是個向使用者索取費用的服務。</p>
<p>接下來建立scaffold讓我的guestbook跑起來吧！</p>
<p><a href="http://punk.tw/wp-content/uploads/2007/11/image20.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="240" alt="image" src="http://punk.tw/wp-content/uploads/2007/11/image-thumb19.png" width="416" border="0"></a> </p>
<p>建立完成之後，應該就可以馬上看到程式目前的畫面啦～！</p>
<p><a href="http://punk.tw/wp-content/uploads/2007/11/image21.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="131" alt="image" src="http://punk.tw/wp-content/uploads/2007/11/image-thumb20.png" width="301" border="0"></a> </p>
<h1>檢視資料庫</h1>
<p>檢視資料庫的畫面可以看到目前有哪些資料表：</p>
<p><a href="http://punk.tw/wp-content/uploads/2007/11/image22.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="38" alt="image" src="http://punk.tw/wp-content/uploads/2007/11/image-thumb21.png" width="361" border="0"></a> </p>
<p>也可以直接點進去修改：</p>
<p><a href="http://punk.tw/wp-content/uploads/2007/11/image23.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="233" alt="image" src="http://punk.tw/wp-content/uploads/2007/11/image-thumb22.png" width="382" border="0"></a> </p>
<h1>檢視Log file</h1>
<p><a href="http://punk.tw/wp-content/uploads/2007/11/image24.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="31" alt="image" src="http://punk.tw/wp-content/uploads/2007/11/image-thumb23.png" width="432" border="0"></a> </p>
<p>點選各個訊息還可以看到更詳盡的資訊。</p>
]]></content:encoded>
			<wfw:commentRss>http://punk.tw/2007/11/20/heroku%ef%bc%9a%e7%b7%9a%e4%b8%8a%e6%92%b0%e5%af%abrails%e7%a8%8b%e5%bc%8f%e5%88%9d%e9%ab%94%e9%a9%97/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Heroku：讓你用網頁介面寫Rails（順便Hosting）</title>
		<link>http://punk.tw/2007/11/20/heroku%ef%bc%9a%e8%ae%93%e4%bd%a0%e7%94%a8%e7%b6%b2%e9%a0%81%e4%bb%8b%e9%9d%a2%e5%af%abrails%ef%bc%88%e9%a0%86%e4%be%bfhosting%ef%bc%89</link>
		<comments>http://punk.tw/2007/11/20/heroku%ef%bc%9a%e8%ae%93%e4%bd%a0%e7%94%a8%e7%b6%b2%e9%a0%81%e4%bb%8b%e9%9d%a2%e5%af%abrails%ef%bc%88%e9%a0%86%e4%be%bfhosting%ef%bc%89#comments</comments>
		<pubDate>Tue, 20 Nov 2007 06:34:50 +0000</pubDate>
		<dc:creator>deduce</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Web2.0 Website]]></category>
		<category><![CDATA[Heroku]]></category>

		<guid isPermaLink="false">http://punk.tw/2007/11/20/411.html</guid>
		<description><![CDATA[http://heroku.com/ （最近因為忙著申請學校，同時面對工作上的專案跟學校的專題，實在是難有餘裕寫文章，今天算是忙裡偷閒:p幾天沒寫文章手真的很癢，過一陣子忙完會盡量多發文的。） Heroku是個號稱可以讓你在線上撰寫你的Rails程式，並且連Hosting的服務都一起搞定的網路服務，有興趣的朋友可以到此網址排隊申請：http://heroku.com/beta，邀請函目前需要等待12天左右。 Heroku上列出了幾個特色如下： 線上編輯程式 支援Hosting，新建或是匯入Rails程式後即可直接啟動:D 分享或協同編輯 支援匯入、匯出 還支援完整的Console模式&#160; References Heroku: An Online Rails Development and App Hosting Environment]]></description>
			<content:encoded><![CDATA[<p><a href="http://heroku.com/"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="51" alt="image" src="http://punk.tw/wp-content/uploads/2007/11/image9.png" width="205" border="0"></a> <a title="http://heroku.com/" href="http://heroku.com/">http://heroku.com/</a></p>
<p> （最近因為忙著申請學校，同時面對工作上的專案跟學校的專題，實在是難有餘裕寫文章，今天算是忙裡偷閒:p幾天沒寫文章手真的很癢，過一陣子忙完會盡量多發文的。）</p>
<p><a href="http://heroku.com/">Heroku</a>是個號稱可以讓你在線上撰寫你的Rails程式，並且連Hosting的服務都一起搞定的網路服務，有興趣的朋友可以到此網址排隊申請：<a title="http://heroku.com/beta" href="http://heroku.com/beta">http://heroku.com/beta</a>，邀請函目前需要等待12天左右。</p>
<p><a href="http://heroku.com/">Heroku</a>上列出了<a href="http://heroku.com/features">幾個特色</a>如下：</p>
<ul>
<li>線上編輯程式<br /><a href="http://punk.tw/wp-content/uploads/2007/11/image4.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="109" alt="image" src="http://punk.tw/wp-content/uploads/2007/11/image-thumb4.png" width="367" border="0"></a> </li>
<li>支援Hosting，新建或是匯入Rails程式後即可直接啟動:D<br /><a href="http://punk.tw/wp-content/uploads/2007/11/image5.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="107" alt="image" src="http://punk.tw/wp-content/uploads/2007/11/image-thumb5.png" width="368" border="0"></a> </li>
<li>分享或協同編輯<br /><a href="http://punk.tw/wp-content/uploads/2007/11/image6.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="107" alt="image" src="http://punk.tw/wp-content/uploads/2007/11/image-thumb6.png" width="380" border="0"></a> </li>
<li>支援匯入、匯出<br /><a href="http://punk.tw/wp-content/uploads/2007/11/image7.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="102" alt="image" src="http://punk.tw/wp-content/uploads/2007/11/image-thumb7.png" width="367" border="0"></a> </li>
<li>還支援完整的Console模式<br /><a href="http://punk.tw/wp-content/uploads/2007/11/image8.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="96" alt="image" src="http://punk.tw/wp-content/uploads/2007/11/image-thumb8.png" width="367" border="0"></a>&nbsp;</li>
</ul>
<p>References</p>
<ul>
<li><a href="http://www.rubyinside.com/heroku-an-online-rails-development-and-app-hosting-environment-647.html">Heroku: An Online Rails Development and App Hosting Environment</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://punk.tw/2007/11/20/heroku%ef%bc%9a%e8%ae%93%e4%bd%a0%e7%94%a8%e7%b6%b2%e9%a0%81%e4%bb%8b%e9%9d%a2%e5%af%abrails%ef%bc%88%e9%a0%86%e4%be%bfhosting%ef%bc%89/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Rails: 建立多對多關係（M:N）</title>
		<link>http://punk.tw/2007/11/11/rails-%e5%bb%ba%e7%ab%8b%e5%a4%9a%e5%b0%8d%e5%a4%9a%e9%97%9c%e4%bf%82%ef%bc%88mn%ef%bc%89</link>
		<comments>http://punk.tw/2007/11/11/rails-%e5%bb%ba%e7%ab%8b%e5%a4%9a%e5%b0%8d%e5%a4%9a%e9%97%9c%e4%bf%82%ef%bc%88mn%ef%bc%89#comments</comments>
		<pubDate>Sun, 11 Nov 2007 13:57:43 +0000</pubDate>
		<dc:creator>deduce</dc:creator>
				<category><![CDATA[Ruby on Rails學習筆記]]></category>
		<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://punk.tw/2007/11/11/397.html</guid>
		<description><![CDATA[前陣子寫了兩篇關於建立好友名單的文章：Rails: 建立好友名單、Rails: 建立好友名單（續）加上好友描述，文中有提到，除非有特殊需求，否則建議建立多對多關係請盡量使用has_and_belongs_to_many(habtm)或has_many :through的方式來建立。 我個人比較常用到的是has_many :through，也就是本篇的主題。 範例說明 本範例將建立物品清單管理，每個使用者擁有多個物品，例如A擁有電腦、手機、相機；使用者設定物品清單的時候可以建立描述，例如紀錄購入時間、價格或是其他文字描述等等。 資料庫規劃 首先建立兩個Model分別為User、Item，分別對應到Users以及Items資料表，另外建立記錄兩者relationship的Model及Table，使用migration來建立內容如下： # db/migrate/001_create_users.rb class CreateUsers < ActiveRecord::Migration def self.up create_table :users do &#124;t&#124; t.column :name, :string, :null => false t.column :created_at, :datetime end end def self.down drop_table :users end end # db/migrate/002_create_items.rb class CreateItems < ActiveRecord::Migration def self.up create_table :items do &#124;t&#124; t.column :name, :string, :null [...]]]></description>
			<content:encoded><![CDATA[<p>前陣子寫了兩篇關於建立好友名單的文章：<a href="http://punk.tw/2007/10/06/341.html">Rails: 建立好友名單</a>、<a href="http://punk.tw/2007/10/06/342.html">Rails: 建立好友名單（續）加上好友描述</a>，文中有提到，除非有特殊需求，否則建議建立多對多關係請盡量使用has_and_belongs_to_many(habtm)或has_many :through的方式來建立。</p>
<p>我個人比較常用到的是has_many :through，也就是本篇的主題。</p>
<h1>範例說明</h1>
<p>本範例將建立物品清單管理，每個使用者擁有多個物品，例如A擁有電腦、手機、相機；使用者設定物品清單的時候可以建立描述，例如紀錄購入時間、價格或是其他文字描述等等。</p>
<h1>資料庫規劃</h1>
<p>首先建立兩個Model分別為User、Item，分別對應到Users以及Items資料表，另外建立記錄兩者relationship的Model及Table，使用migration來建立內容如下：</p>
<p>
<pre class="prettyprint"># db/migrate/001_create_users.rb
class CreateUsers < ActiveRecord::Migration
  def self.up
    create_table :users do |t|
      t.column :name, :string, :null => false
      t.column :created_at, :datetime
    end
  end

  def self.down
    drop_table :users
  end
end

# db/migrate/002_create_items.rb
class CreateItems < ActiveRecord::Migration
  def self.up
    create_table :items do |t|
      t.column :name, :string, :null => false
      t.column :description, :text
      t.column :created_at, :datetime
    end
  end

  def self.down
    drop_table :items
  end
end

# db/migrate/003_create_ownerships.rb
class CreateOwnerships < ActiveRecord::Migration
  def self.up
    create_table <img src='http://punk.tw/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> wnerships do |t|
      t.column :user_id, :integer, :null => false
      t.column :item_id, :integer, :null => false
      t.column :description, :text
      t.column :created_at, :datetime
      t.column :updated_at, :datetime
    end
  end

  def self.down
    drop_table <img src='http://punk.tw/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> wnerships
  end
end
</pre>
<p>
Model的程式如下，簡單來說就是透過第三個Model來記錄兩個Model之間的關係：</p>
<pre class="prettyprint">
# app/models/item.rb
class Item < ActiveRecord::Base
  has_many <img src='http://punk.tw/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> wnerships
  has_many :users, :through => <img src='http://punk.tw/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> wnerships
end

# app/models/user.rb
class User < ActiveRecord::Base
  has_many <img src='http://punk.tw/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> wnerships
  has_many :items, :through => <img src='http://punk.tw/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> wnerships
end

# app/models/ownership.rb
class Ownership < ActiveRecord::Base
  belongs_to :item
  belongs_to :user
end
</pre>
</p>
<p>
如此一來，就可以用下列指令來增加User, Item，並且可以查詢到某User所有的items，或是<br />
擁有某item的users。</p>
<pre class="prettyprint">
peter = User.create(:name => "Peter")

deduce = User.create(:name => "Deduce")

phone = Item.create(:name => "phone")
camera = Item.create(:name => "camera")

peter.items << phone

peter.items.count
phone.users.count
</pre></p>
]]></content:encoded>
			<wfw:commentRss>http://punk.tw/2007/11/11/rails-%e5%bb%ba%e7%ab%8b%e5%a4%9a%e5%b0%8d%e5%a4%9a%e9%97%9c%e4%bf%82%ef%bc%88mn%ef%bc%89/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rails: 建立好友名單（續）加上好友描述</title>
		<link>http://punk.tw/2007/10/06/rails-%e5%bb%ba%e7%ab%8b%e5%a5%bd%e5%8f%8b%e5%90%8d%e5%96%ae%ef%bc%88%e7%ba%8c%ef%bc%89%e5%8a%a0%e4%b8%8a%e5%a5%bd%e5%8f%8b%e6%8f%8f%e8%bf%b0</link>
		<comments>http://punk.tw/2007/10/06/rails-%e5%bb%ba%e7%ab%8b%e5%a5%bd%e5%8f%8b%e5%90%8d%e5%96%ae%ef%bc%88%e7%ba%8c%ef%bc%89%e5%8a%a0%e4%b8%8a%e5%a5%bd%e5%8f%8b%e6%8f%8f%e8%bf%b0#comments</comments>
		<pubDate>Sat, 06 Oct 2007 13:48:14 +0000</pubDate>
		<dc:creator>deduce</dc:creator>
				<category><![CDATA[Ruby on Rails學習筆記]]></category>
		<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://punk.tw/2007/10/06/342.html</guid>
		<description><![CDATA[前言 這篇文章你可以視為「如何在多對多關係中，記錄額外的資訊」。其實一般想要在M:N之間的關係記錄額外的資訊，應該是會透過has_many :through比較恰當，不過因為當初我在寫好友名單時，先用了上一篇文章的寫法，所以才找到本篇文章要談的作法。 簡單來說，如果要記錄的資訊只有一個欄位，用本篇作法是快又有效；否則，我認為還是用has_many :through應該比較方便。後者的作法下次再談。 正文開始... 常見的社群網站，建立好友之間的關係不外乎是加入好友、並通知對方，似乎比較少有可以替好友分群組、加上描述的功能，其中丁丁大站就是可以替好友們加上描述以及分組的。這篇文章就是教你如何像丁丁大站一樣，用簡單的程式建立好友關係並加上簡單的描述。 首先一樣是你必須有一張記錄Friendship的Table，偷用上一篇的Migration（這樣上一篇瞬間變得毫無意義了-.-，不過我的想法是，一般人應該只會用到上一篇的作法。）。注意，與上一篇不同的是，這裡多了description欄位 class AddFriendship < ActiveRecord::Migration def self.up create_table :friendships, :id => false do &#124;t&#124; t.column :user_id, :integer, :null => false t.column :friend_id, :integer, :null => false t.column :description :string end end def self.down drop_table :friendships end end 接下來一樣是定義model，一樣是拿上一篇的程式，多了三行程式： attr_accessor :description，由於ActiveRecord的Class在mapping到資料庫時，是直接對應到table的column，但目前users table沒有description這個column，因此我們在此給User這個class一個屬性，等一下在建立好友關係時就可以一併連好友描述都塞到friendships裡面。 :insert_sql，因為除了ActiveRecord會幫我們找到foreign_key所屬的欄位之外，我們還需要塞入額外的資訊，因此自訂語法。 after_find的定義，由於ActiveRecord撈出資料後，User這個model本身沒有description欄位，所以我們在撈出好友關係的時候，再把description寫到剛剛建立好的屬性。 class User < ActiveRecord::Base attr_accessor [...]]]></description>
			<content:encoded><![CDATA[<h1>前言</h1>
<p>這篇文章你可以視為「如何在多對多關係中，記錄額外的資訊」。其實一般想要在M:N之間的關係記錄額外的資訊，應該是會透過has_many :through比較恰當，不過因為當初我在寫好友名單時，先用了<a href="http://punk.tw/2007/10/06/341.html">上一篇文章</a>的寫法，所以才找到本篇文章要談的作法。</p>
<p>簡單來說，如果要記錄的資訊只有一個欄位，用本篇作法是快又有效；否則，我認為還是用has_many :through應該比較方便。後者的作法下次再談。</p>
<h1>正文開始...</h1>
<p>常見的社群網站，建立好友之間的關係不外乎是加入好友、並通知對方，似乎比較少有可以替好友分群組、加上描述的功能，其中丁丁大站就是可以替好友們加上描述以及分組的。這篇文章就是教你如何像丁丁大站一樣，用簡單的程式建立好友關係並加上簡單的描述。</p>
<p>首先一樣是你必須有一張記錄Friendship的Table，偷用上一篇的Migration（這樣<a href="http://punk.tw/2007/10/06/341.html">上一篇</a>瞬間變得毫無意義了-.-，不過我的想法是，一般人應該只會用到<a href="http://punk.tw/2007/10/06/341.html">上一篇</a>的作法。）。注意，與<a href="http://punk.tw/2007/10/06/341.html">上一篇</a>不同的是，這裡多了description欄位</p>
<pre class="prettyprint">class AddFriendship < ActiveRecord::Migration
  def self.up
    create_table :friendships, :id => false do |t|
      t.column :user_id, :integer, :null => false
      t.column :friend_id, :integer, :null => false
      t.column :description :string
    end
  end

  def self.down
    drop_table :friendships
  end
end</pre>
<p>接下來一樣是定義model，一樣是拿上一篇的程式，多了三行程式：</p>
<ul>
<li>attr_accessor :description，由於ActiveRecord的Class在mapping到資料庫時，是直接對應到table的column，但目前users table沒有description這個column，因此我們在此給User這個class一個屬性，等一下在建立好友關係時就可以一併連好友描述都塞到friendships裡面。</li>
<li>:insert_sql，因為除了ActiveRecord會幫我們找到foreign_key所屬的欄位之外，我們還需要塞入額外的資訊，因此自訂語法。</li>
<li>after_find的定義，由於ActiveRecord撈出資料後，User這個model本身沒有description欄位，所以我們在撈出好友關係的時候，再把description寫到剛剛建立好的屬性。</li>
</ul>
<pre class="prettyprint">
class User < ActiveRecord::Base
  attr_accessor :description
  has_and_belongs_to_many :friends,
    :class_name => "User",
    :join_table => "friendships",
    :association_foreign_key => "friend_id",
    :foreign_key => "user_id",
    :insert_sql => 'INSERT into friendships (user_id, friend_id, description) VALUES (#{id}, #{record.id}, \'#{record.description}\')'
    # 要特別注意引號的使用，尤其description通常都是string，所以用單引號框起來

  def after_find
    self.description = self["description"]
    # <del datetime="2007-10-06T14:06:59+00:00">說實在這邊我也不是很確定寫法是否正確XD</del>應該說可以work，但不知道有沒有更好的寫法:p
  end
end
</pre>
<p>如此一來，你就可以用下列的程式來建立、取得好友及描述</p>
<pre class="prettyprint">
u = User.create(:name => "deduce")
k = User.create(:name => "punk")
u.description = "Rails愛好者:deduce"
k.friends <<  u unless k.friends.include?(u)

# 輸出好友描述
k.friends.each do |friend|
  puts friend.description
end
</pre>
<p>註：description的字串丟進去之前要記得先處理，避免發生不必要的問題，例如SQL injection之類的。
</p>
<h1>References</h1>
<ul>
<li><a href="http://punk.tw/2007/10/06/341.html">Rails: 建立好友名單</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://punk.tw/2007/10/06/rails-%e5%bb%ba%e7%ab%8b%e5%a5%bd%e5%8f%8b%e5%90%8d%e5%96%ae%ef%bc%88%e7%ba%8c%ef%bc%89%e5%8a%a0%e4%b8%8a%e5%a5%bd%e5%8f%8b%e6%8f%8f%e8%bf%b0/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Rails: 建立好友名單</title>
		<link>http://punk.tw/2007/10/06/rails-%e5%bb%ba%e7%ab%8b%e5%a5%bd%e5%8f%8b%e5%90%8d%e5%96%ae</link>
		<comments>http://punk.tw/2007/10/06/rails-%e5%bb%ba%e7%ab%8b%e5%a5%bd%e5%8f%8b%e5%90%8d%e5%96%ae#comments</comments>
		<pubDate>Sat, 06 Oct 2007 11:59:16 +0000</pubDate>
		<dc:creator>deduce</dc:creator>
				<category><![CDATA[Ruby on Rails學習筆記]]></category>
		<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://punk.tw/2007/10/06/341.html</guid>
		<description><![CDATA[網站想要開發「好友名單」功能時，會新增Table來記錄好友之間的關係，在Rails應該怎麼做呢？假設目前系統已經有User這個Model，我們將會建立 User has_many friends的關係來記錄每個User擁有的好友們。 首先新增一張Table來記錄Friendship（請善用Migration）： class AddFriendship < ActiveRecord::Migration def self.up create_table :friendships, :id => false do &#124;t&#124; t.column :user_id, :integer, :null => false t.column :friend_id, :integer, :null => false end end def self.down drop_table :friendships end end 接下來在User Model中定義好友關係： class User < ActiveRecord::Base has_and_belongs_to_many :friends, :class_name => "User", :join_table => "friendships", :association_foreign_key => "friend_id", [...]]]></description>
			<content:encoded><![CDATA[<p>網站想要開發「好友名單」功能時，會新增Table來記錄好友之間的關係，在Rails應該怎麼做呢？假設目前系統已經有User這個Model，我們將會建立 User has_many friends的關係來記錄每個User擁有的好友們。</p>
<p>首先新增一張Table來記錄Friendship（請善用Migration）：</p>
<p>
<pre class="prettyprint">class AddFriendship < ActiveRecord::Migration
  def self.up
    create_table :friendships, :id => false do |t|
      t.column :user_id, :integer, :null => false
      t.column :friend_id, :integer, :null => false
    end
  end

  def self.down
    drop_table :friendships
  end
end</pre>
</p>
<p>接下來在User Model中定義好友關係：
<pre class="prettyprint">
class User < ActiveRecord::Base
  has_and_belongs_to_many :friends,
    :class_name => "User",
    :join_table => "friendships",
    :association_foreign_key => "friend_id",
    :foreign_key => "user_id"
end
</pre>
</p>
<p>如此一來，你就可以用下列語法來建立使用者之間的好友關係：
<pre class="prettyprint">u = User.create(:name => "deduce")
k = User.create(:name => "punk")
u.friends << k if not u.friends.include?(k)
# 如果 u 的好友不包含 k 則加入好友，不需要另外進行儲存的動作
</pre>
</p>
<p>u.friends取得好友名單、u.friends.count取得好友人數</p>
]]></content:encoded>
			<wfw:commentRss>http://punk.tw/2007/10/06/rails-%e5%bb%ba%e7%ab%8b%e5%a5%bd%e5%8f%8b%e5%90%8d%e5%96%ae/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Rails: 建立Permalink，避免流水號洩漏網站資料</title>
		<link>http://punk.tw/2007/10/06/rails-%e5%bb%ba%e7%ab%8bpermalink%ef%bc%8c%e9%81%bf%e5%85%8d%e6%b5%81%e6%b0%b4%e8%99%9f%e6%b4%a9%e6%bc%8f%e7%b6%b2%e7%ab%99%e8%b3%87%e6%96%99</link>
		<comments>http://punk.tw/2007/10/06/rails-%e5%bb%ba%e7%ab%8bpermalink%ef%bc%8c%e9%81%bf%e5%85%8d%e6%b5%81%e6%b0%b4%e8%99%9f%e6%b4%a9%e6%bc%8f%e7%b6%b2%e7%ab%99%e8%b3%87%e6%96%99#comments</comments>
		<pubDate>Sat, 06 Oct 2007 10:11:37 +0000</pubDate>
		<dc:creator>deduce</dc:creator>
				<category><![CDATA[Ruby on Rails學習筆記]]></category>
		<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://punk.tw/2007/10/06/340.html</guid>
		<description><![CDATA[XDite曾經在「以 ROR 打造網站，設計盲點所引發的惡搞危機」這篇文章中提到Rails的scaffold所建立出來的程式（或說開發者很習慣直接以id操作Model），由於在URL上是直接以流水號的方式呈現，我們便可以利用一些簡單的程式爬完某個網站的特定頁面，來取得所有的文章、所有的使用者頁面。 事實上XDite應該是有在另外一場活動提過因應的對策，雖然我沒抓下來聽，不過我猜應該是改變Controller的設計方式，以亂碼或是其他方式來產生URL。 首先在Table裡面加上「permalink」這個column，如果你是要在既有的table上新增，可以使用migration來新增欄位（當然你也可以砍掉重練XD） class AddPermalink < ActiveRecord::Migration def self.up add_column :posts, :permalink, :string add_index :posts, :permalink, :unique => true end def self.down remove_index :posts, :permalink remove_column :posts, :permalink end end 接下來在Model裡面定義permalink的產生方式 class Post < ActiveRecord::Base before_create :generate_permalink protected def generate_permalink # 定義permalink的產生方式 self.permalink = Time.now.strftime("%Y-%m-%d_") + rand(100000).to_s end end 這邊我是用時間加亂數的方式產生permalink，事實上這樣的寫法還是有可能重複，不如用SHA或MD5的方式來產生Permalink也不錯（雖然長了點）。 class Post < [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://blog.xdite.net/">XDite</a>曾經在「<a href="http://blog.xdite.net/?p=424">以 ROR 打造網站，設計盲點所引發的惡搞危機</a>」這篇文章中提到Rails的scaffold所建立出來的程式（或說開發者很習慣直接以id操作Model），由於在URL上是直接以流水號的方式呈現，我們便可以利用一些簡單的程式爬完某個網站的特定頁面，來取得所有的文章、所有的使用者頁面。</p>
<p>事實上XDite應該是有在另外一場活動提過<a href="http://lukhnos.org/blog/zh/archives/551">因應的對策</a>，雖然我沒抓下來聽，不過我猜應該是改變Controller的設計方式，以亂碼或是其他方式來產生URL。</p>
<p>首先在Table裡面加上「permalink」這個column，如果你是要在既有的table上新增，可以使用migration來新增欄位（當然你也可以砍掉重練XD） </p>
<p>
<pre class="prettyprint">class AddPermalink < ActiveRecord::Migration
  def self.up
    add_column :posts, :permalink, :string
    add_index :posts, :permalink, :unique => true
  end

  def self.down
    remove_index :posts, :permalink
    remove_column :posts, :permalink
  end
end
</pre>
<p>接下來在Model裡面定義permalink的產生方式
<pre class="prettyprint">class Post < ActiveRecord::Base
  before_create :generate_permalink

  protected
    def generate_permalink
      # 定義permalink的產生方式
      self.permalink = Time.now.strftime("%Y-%m-%d_") + rand(100000).to_s
    end
end</pre>
</p>
<p>這邊我是用時間加亂數的方式產生permalink，事實上這樣的寫法還是有可能重複，不如用SHA或MD5的方式來產生Permalink也不錯（雖然長了點）。
<pre class="prettyprint">class Post < ActiveRecord::Base
  before_create :generate_permalink

  protected
    def generate_permalink
      # 用標題+時間進行md5的編碼
      require 'digest/md5'
      self.permalink = Digest::MD5.hexdigest(self.title + Time.now.to_s)
    end
end
</pre>
<p>往後在連結可以使用： </p>
<pre class="prettyprint"> link_to post.title,
                :controller => "post",
                :action => "show",
                :id => post.permalink
</pre>
<p>在Controller裡面Action的寫法則是：
<pre class="prettyprint">class PostController < ApplicationController
  def show
    @post = Post.find_by_permalink(params[:id])
  end
end
</pre>
]]></content:encoded>
			<wfw:commentRss>http://punk.tw/2007/10/06/rails-%e5%bb%ba%e7%ab%8bpermalink%ef%bc%8c%e9%81%bf%e5%85%8d%e6%b5%81%e6%b0%b4%e8%99%9f%e6%b4%a9%e6%bc%8f%e7%b6%b2%e7%ab%99%e8%b3%87%e6%96%99/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>本來要寫Rails心得文，結果變成一篇牢騷文</title>
		<link>http://punk.tw/2007/09/30/%e6%9c%ac%e4%be%86%e8%a6%81%e5%af%abrails%e5%bf%83%e5%be%97%e6%96%87%ef%bc%8c%e7%b5%90%e6%9e%9c%e8%ae%8a%e6%88%90%e4%b8%80%e7%af%87%e7%89%a2%e9%a8%b7%e6%96%87</link>
		<comments>http://punk.tw/2007/09/30/%e6%9c%ac%e4%be%86%e8%a6%81%e5%af%abrails%e5%bf%83%e5%be%97%e6%96%87%ef%bc%8c%e7%b5%90%e6%9e%9c%e8%ae%8a%e6%88%90%e4%b8%80%e7%af%87%e7%89%a2%e9%a8%b7%e6%96%87#comments</comments>
		<pubDate>Sun, 30 Sep 2007 15:51:06 +0000</pubDate>
		<dc:creator>deduce</dc:creator>
				<category><![CDATA[Murmur]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://punk.tw/2007/09/30/325.html</guid>
		<description><![CDATA[如同Title，懶得看我發牢騷的就跳過吧，一不小心就扯遠了:p 上回寫有關ActiveRecord的文章其實到現在都一直提不起勁來寫，一來是我認為基礎性的教學，網路上或書店裡都有很多參考資料，似乎輪不到我來寫；二來是進階的教學我又不夠力。但站在推廣的角度來說，關於Ruby on Rails的文章總是多多益善。 還記得以前我跟老師、同學們分享Ruby on Rails這個嶄新的框架時，快速在短短幾分鐘內依賴ActiveRecord及Dynamic Scaffold所搭建起來的簡單網站，讓在場的人都留下了滿深刻的印象。然而，對老師來說，他認為MVC pattern或ORM的實作雖然可以快速、有效地提高生產力，但這個框架的能耐有多大是他最關心的。 說實在，Ruby on Rails的能耐我也無從說起，因為Ruby on Rails能做到的，其他語言也都能做到，甚至其他語言、框架、技術有著更悠久的歷史、更成熟的架構，甚至更有力的後台，例如.NET, JAVA頗受企業青睞，出問題時即使自己公司內部的技術人員無法搞定，還可以求救於其他公司甚至原廠。 當時老師問我一句：「Ruby on Rails拿來做電子商務、處理金流、交易系統ok嗎？」事實上我當然認為是ok的，但我也很清楚要做這些事情"好像"不該拿RoR來做，總之，當下的我實在難以回答老師的問題，頓時我覺得自己目光如豆，我的視野大概就只停留在Web Development，所謂企業級的解決方案，我可說是個門外漢。（事實上我們那們課修的是Web 2.0研究，我報告純Web相關的應該沒錯啊XD如果是要拿Web做金流、電子商務，那RoR理所當然也是個選擇嘛，要加密、要搞Web Service、要連結不同的資料庫也都沒問題啊～:p） 老師似乎沒有太大興趣，那同學們呢？大概是我沒有表現出Rails的優美，同學們大概也只是當作我在報告給老師聽、而不認為我是在推廣-.-，總之後來我只成功讓一位同學繼續走上學習Ruby on Rails的道路，而他也成功讓一位女同學在某一門課的期末專題使用Ruby on Rails開發網頁。現在回頭想想，當初真是辛苦他們了orz 在那之後，如同日前我剛換上的新版面所言，我的工作、專案主要使用.NET Framework開發，Ruby on Rails是我閒暇時拿來娛樂的工具之一。主要原因有二，一是我認為除了網頁開發之外，我還想學習更多的技術；二是因為人在江湖、身不由己，我的工作、我的專案都是不得已要使用.NET的orz... 不過，學習Ruby on Rails的過程著實讓我成長不少， 寫Rails有時候是一種享受:D 這篇本來是想寫這一陣子開發Rails網頁，有關使用Helper的心得，不過既然已經扯遠了，那就下次再說吧XD]]></description>
			<content:encoded><![CDATA[<p>如同Title，懶得看我發牢騷的就跳過吧，一不小心就扯遠了:p</p>
<p>上回寫有關ActiveRecord的文章其實到現在都一直提不起勁來寫，一來是我認為基礎性的教學，網路上或書店裡都有很多參考資料，似乎輪不到我來寫；二來是進階的教學我又不夠力。但站在推廣的角度來說，關於Ruby on Rails的文章總是多多益善。</p>
<p>還記得以前我跟老師、同學們分享Ruby on Rails這個嶄新的框架時，快速在短短幾分鐘內依賴ActiveRecord及Dynamic Scaffold所搭建起來的簡單網站，讓在場的人都留下了滿深刻的印象。然而，對老師來說，他認為MVC pattern或ORM的實作雖然可以快速、有效地提高生產力，但這個框架的能耐有多大是他最關心的。</p>
<p>說實在，Ruby on Rails的能耐我也無從說起，因為Ruby on Rails能做到的，其他語言也都能做到，甚至其他語言、框架、技術有著更悠久的歷史、更成熟的架構，甚至更有力的後台，例如.NET, JAVA頗受企業青睞，出問題時即使自己公司內部的技術人員無法搞定，還可以求救於其他公司甚至原廠。</p>
<p>當時老師問我一句：「Ruby on Rails拿來做電子商務、處理金流、交易系統ok嗎？」事實上我當然認為是ok的，但我也很清楚要做這些事情"好像"不該拿RoR來做，總之，當下的我實在難以回答老師的問題，頓時我覺得自己目光如豆，我的視野大概就只停留在Web Development，所謂企業級的解決方案，我可說是個門外漢。（事實上我們那們課修的是Web 2.0研究，我報告純Web相關的應該沒錯啊XD如果是要拿Web做金流、電子商務，那RoR理所當然也是個選擇嘛，要加密、要搞Web Service、要連結不同的資料庫也都沒問題啊～:p）</p>
<p>老師似乎沒有太大興趣，那同學們呢？大概是我沒有表現出Rails的優美，同學們大概也只是當作我在報告給老師聽、而不認為我是在推廣-.-，總之後來我只成功讓一位同學繼續走上學習Ruby on Rails的道路，而他也成功讓一位女同學在某一門課的期末專題使用Ruby on Rails開發網頁。現在回頭想想，當初真是辛苦他們了orz</p>
<p>在那之後，如同日前我剛換上的新版面所言，我的工作、專案主要使用.NET Framework開發，Ruby on Rails是我閒暇時拿來娛樂的工具之一。主要原因有二，一是我認為除了網頁開發之外，我還想學習更多的技術；二是因為人在江湖、身不由己，我的工作、我的專案都是不得已要使用.NET的orz...</p>
<p>不過，學習Ruby on Rails的過程著實讓我成長不少， 寫Rails有時候是一種享受:D</p>
<p>這篇本來是想寫這一陣子開發Rails網頁，有關使用Helper的心得，不過既然已經扯遠了，那就下次再說吧XD</p>
]]></content:encoded>
			<wfw:commentRss>http://punk.tw/2007/09/30/%e6%9c%ac%e4%be%86%e8%a6%81%e5%af%abrails%e5%bf%83%e5%be%97%e6%96%87%ef%bc%8c%e7%b5%90%e6%9e%9c%e8%ae%8a%e6%88%90%e4%b8%80%e7%af%87%e7%89%a2%e9%a8%b7%e6%96%87/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>jq4r: jQuery on Rails</title>
		<link>http://punk.tw/2007/09/20/jq4r-jquery-on-rails</link>
		<comments>http://punk.tw/2007/09/20/jq4r-jquery-on-rails#comments</comments>
		<pubDate>Thu, 20 Sep 2007 13:04:51 +0000</pubDate>
		<dc:creator>deduce</dc:creator>
				<category><![CDATA[AJAX Framework]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://punk.tw/2007/09/20/259.html</guid>
		<description><![CDATA[ericksk說：「既然找不到別人寫的，那就自己寫吧！」，因此他寫了一套jq4r，全名叫jQuery helpers for Rails。真是頗吸引我的，之前用過一陣子的jQuery，感覺還滿好用的，最近不但推出新版還搞了一套jQuery UI，之前在某個日文網站看到的評論稱之為「jQuery's Scriptaculous」。 anyway，ericsk的文章在此：jq4r - jQuery helpers for Rails 他另外開了一個blog及google code project專門來放jq4r： jq4r project: http://jq4r.googlecode.com/ jq4r blog: http://jq4r.blogspot.com/]]></description>
			<content:encoded><![CDATA[<p><a href="http://blog.ericsk.org/">ericksk</a>說：「既然找不到別人寫的，那就自己寫吧！」，因此他寫了一套jq4r，全名叫jQuery helpers for Rails。真是頗吸引我的，之前用過一陣子的jQuery，感覺還滿好用的，最近不但推出新版還搞了一套jQuery UI，之前在某個日文網站看到的評論稱之為「jQuery's Scriptaculous」。</p>
<p>anyway，ericsk的文章在此：<a href="http://blog.ericsk.org/archives/767">jq4r - jQuery helpers for Rails</a></p>
<p>他另外開了一個blog及google code project專門來放jq4r：</p>
<ul>
<li>jq4r project: <a href="http://jq4r.googlecode.com/">http://jq4r.googlecode.com/</a> </li>
<li>jq4r blog: <a href="http://jq4r.blogspot.com/">http://jq4r.blogspot.com/</a> </li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://punk.tw/2007/09/20/jq4r-jquery-on-rails/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ActiveRecord: Model之間的dependent選項</title>
		<link>http://punk.tw/2007/09/18/activerecord-model%e4%b9%8b%e9%96%93%e7%9a%84dependent%e9%81%b8%e9%a0%85</link>
		<comments>http://punk.tw/2007/09/18/activerecord-model%e4%b9%8b%e9%96%93%e7%9a%84dependent%e9%81%b8%e9%a0%85#comments</comments>
		<pubDate>Tue, 18 Sep 2007 08:13:05 +0000</pubDate>
		<dc:creator>deduce</dc:creator>
				<category><![CDATA[Ruby on Rails學習筆記]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://punk.tw/2007/09/18/253.html</guid>
		<description><![CDATA[利用ActiveRecord提供的dependent選項處理資料相依性 最近在ptt的Ruby板，有板友在討論刪除資料時如何進行驗證，原po的問題大致上是說倘若某一筆資料跟其他的資料有所關聯，該如何顯示錯誤訊息給使用者、告知因為有資料的關聯參照存在所以無法刪除。 看到這個問題，我心中第一個想到的是「就用CASCADE的方式把資料一口氣刪光啊！」XD不過隨即想想，大多數的資料可不是說想刪就能隨便刪的，例如在Ruby板上的討論串就有人提到要刪除某個分類名稱，但在刪除前要先將該分類底下的文章都指定為未分類。 總之，好奇心驅使之下，我猜想ActiveRecord裡面一定有針對關聯性特別設計一套機制來處理，果不其然其實在has_one, has_many底下就有:dependent來處理互相參照的資料。 :dependent有三個選項：（在此假設User has_many Posts, Post has_many Comments） class User < ActiveRecord::Base has_many :posts, :dependent => :destroy end :destroy：呼叫User.destroy時，ActiveRecord會呼叫Post.destroy來達到刪除Posts的目的，也就是與該Posts相關聯的comments也會被一併刪除。（除非Post與Comments之間的關係是nullify） :delete_all：呼叫User.destroy時，僅刪除User本身及該user的posts。 :nullify：不刪除關聯的物件，僅將User的posts之Foreign Key改為NULL 至於要在物件刪除之前，若要做驗證動作，就如同ptt Ruby的板主godfat分享的一樣，使用"before_destroy"可以在呼叫destroy之前先做其他動作。例如在這個網頁裡面看到的sample code（before_destroy是寫在Model裡，destroy的動作則是由controller中的action來呼叫）: class Group < ActiveRecord::Base acts_as_tree rder => "name" has_and_belongs_to_many => :users before_destroy :validates_no_dependents def validates_no_dependents if children.size > 0 &#124;&#124; users.size > 0 errors.add :base, "Cannot delete [...]]]></description>
			<content:encoded><![CDATA[<h1>利用ActiveRecord提供的dependent選項處理資料相依性</h1>
<p>最近在ptt的Ruby板，有板友在討論刪除資料時如何進行驗證，原po的問題大致上是說倘若某一筆資料跟其他的資料有所關聯，該如何顯示錯誤訊息給使用者、告知因為有資料的關聯參照存在所以無法刪除。</p>
<p>看到這個問題，我心中第一個想到的是「就用CASCADE的方式把資料一口氣刪光啊！」XD不過隨即想想，大多數的資料可不是說想刪就能隨便刪的，例如在Ruby板上的討論串就有人提到要刪除某個分類名稱，但在刪除前要先將該分類底下的文章都指定為未分類。</p>
<p>總之，好奇心驅使之下，我猜想ActiveRecord裡面一定有針對關聯性特別設計一套機制來處理，果不其然其實在has_one, has_many底下就有:dependent來處理互相參照的資料。</p>
<p>:dependent有三個選項：（在此假設User has_many Posts, Post has_many Comments）
<pre class="prettyprint">
class User < ActiveRecord::Base
  has_many :posts, :dependent => :destroy
end
</pre>
</p>
<ul>
<li><strong>:destroy</strong>：呼叫User.destroy時，ActiveRecord會呼叫Post.destroy來達到刪除Posts的目的，也就是與該Posts相關聯的comments也會被一併刪除。（除非Post與Comments之間的關係是nullify）</li>
<li><strong>:delete_all</strong>：呼叫User.destroy時，僅刪除User本身及該user的posts。 </li>
<li><strong>:nullify</strong>：不刪除關聯的物件，僅將User的posts之Foreign Key改為NULL </li>
</ul>
<p>
至於要在物件刪除之前，若要做驗證動作，就如同ptt Ruby的板主<a href="http://godfat.org/">godfat</a>分享的一樣，使用"before_destroy"可以在呼叫destroy之前先做其他動作。例如在<a href="http://railsforum.com/viewtopic.php?pid=35928">這個網頁</a>裡面看到的sample code（before_destroy是寫在Model裡，destroy的動作則是由controller中的action來呼叫）:</p>
<pre class="prettyprint">
class Group < ActiveRecord::Base
  acts_as_tree <img src='http://punk.tw/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> rder => "name"

  has_and_belongs_to_many => :users

  before_destroy :validates_no_dependents

  def validates_no_dependents
    if children.size > 0 || users.size > 0
      errors.add :base, "Cannot delete this group, as it has sub-groups and/or users"
    end
  end
end
</pre></p>
]]></content:encoded>
			<wfw:commentRss>http://punk.tw/2007/09/18/activerecord-model%e4%b9%8b%e9%96%93%e7%9a%84dependent%e9%81%b8%e9%a0%85/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>[Rails學習心得] ActiveRecord：定義Model之間的關係</title>
		<link>http://punk.tw/2007/09/18/rails%e5%ad%b8%e7%bf%92%e5%bf%83%e5%be%97-activerecord%ef%bc%9a%e5%ae%9a%e7%be%a9model%e4%b9%8b%e9%96%93%e7%9a%84%e9%97%9c%e4%bf%82</link>
		<comments>http://punk.tw/2007/09/18/rails%e5%ad%b8%e7%bf%92%e5%bf%83%e5%be%97-activerecord%ef%bc%9a%e5%ae%9a%e7%be%a9model%e4%b9%8b%e9%96%93%e7%9a%84%e9%97%9c%e4%bf%82#comments</comments>
		<pubDate>Tue, 18 Sep 2007 07:02:14 +0000</pubDate>
		<dc:creator>deduce</dc:creator>
				<category><![CDATA[Ruby on Rails學習筆記]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://punk.tw/2007/09/18/252.html</guid>
		<description><![CDATA[在上一篇文章裡稍微提到了ActiveRecord能讓你不需寫SQL語法便能快速地存取資料，（事實上我只提到了讀取的部份，寫入其實也相當簡單，依樣畫葫蘆，只是使用的method不同罷了）這篇便來談談在ActiveRecord裡，是如何處理Model與Model之間的關係。 定義Model之間的關係 平常我們在描述兩個資料表之間的關係，通常可能會說兩張table是一對一、一對多、或是多對多，而在ActiveRecord裡，兩個Model之間的關係主要以上述的三種方式來定義。 延續上一篇文章中我所建立的三個model: User, Video, and Comment，這三個model之間的關係如下： User has_many videos：User可以擁有多個Videos User has_many comments：User可以擁有多個Comments Video belongs_to user：Video必然會屬於某個User Comment belongs_to user：Comment也會屬於某個User Video has_many comments：每個影片都可以有多筆Comments Comment belongs_to video：每個 comment也會屬於某個Video 看這樣的描述，如果是尚未接觸過Ruby on Rails或是ActiveRecord的讀者一定會感到疑惑，根本聽不懂我在說什麼嘛！請看下列三段程式碼，分別是上述三個Model的程式： #Model: rails_app/app/models/user.rb class User < ActiveRecord::Base has_many :videos has_many :comments end #Model: rails_app/app/models/video.rb class Video < ActiveRecord::Base has_many :comments belongs_to :user end #Model: rails_app/app/models/comment.rb class [...]]]></description>
			<content:encoded><![CDATA[<p>在<a href="http://punk.tw/2007/09/15/241.html">上一篇文章</a>裡稍微提到了ActiveRecord能讓你不需寫SQL語法便能快速地存取資料，（事實上我只提到了讀取的部份，寫入其實也相當簡單，依樣畫葫蘆，只是使用的method不同罷了）這篇便來談談在ActiveRecord裡，是如何處理Model與Model之間的關係。</p>
<h1>定義Model之間的關係</h1>
<p>平常我們在描述兩個資料表之間的關係，通常可能會說兩張table是一對一、一對多、或是多對多，而在ActiveRecord裡，兩個Model之間的關係主要以上述的三種方式來定義。</p>
<p>延續上一篇文章中我所建立的三個model: User, Video, and Comment，這三個model之間的關係如下：
<ul>
<li>User has_many videos：User可以擁有多個Videos</li>
<li>User has_many comments：User可以擁有多個Comments</li>
<li>Video belongs_to user：Video必然會屬於某個User</li>
<li>Comment belongs_to user：Comment也會屬於某個User</li>
<li>Video has_many comments：每個影片都可以有多筆Comments</li>
<li>Comment belongs_to video：每個 comment也會屬於某個Video</li>
</ul>
<p>  看這樣的描述，如果是尚未接觸過Ruby on Rails或是ActiveRecord的讀者一定會感到疑惑，根本聽不懂我在說什麼嘛！請看下列三段程式碼，分別是上述三個Model的程式：
<pre class="prettyprint">
#Model: rails_app/app/models/user.rb
class User < ActiveRecord::Base
  has_many :videos
  has_many :comments
end
</pre>
<pre class="prettyprint">
#Model: rails_app/app/models/video.rb
class Video < ActiveRecord::Base
  has_many :comments
  belongs_to :user
end
</pre>
<pre class="prettyprint">
#Model: rails_app/app/models/comment.rb
class Comment < ActiveRecord::Base
  belongs_to :user
  belongs_to :video
end
</pre>
<p>看出什麼了嗎？只要使用很直觀的has_many, belongs_to這樣的method便可以輕鬆定義Model之間的關係。
</p>
<h1>定義資料表欄位：使用Migration</h1>
<p>接著，我們還必須仰賴migration替我們新增對應的資料表或欄位，在上一篇文章中已經完成了User migration的程式：</p>
<pre class="prettyprint">
#Migration: rails_app/app/db/migrate/001_create_users.rb
class CreateUsers < ActiveRecord::Migration
  def self.up
    create_table :users do |t|
      t.column :name, :string, :null => false
      t.column :email, :string, :null => false
      t.column :password, :string, :null => false
      t.column :nickname, :string, :null => false
      t.column :memo, :text
      t.column :created_at, :datetime
      t.column :updated_at, :datetime
    end
  end

  def self.down
    drop_table :users
  end
end
</pre>
<p>在上一篇文章中，除了self.up、self.down兩個method之外，我並沒有交代其他程式碼所代表的意義，這邊稍微補充一下</p>
<ul>
<li>create_table代表建立一個新的資料表。透過對資料表進行管理的method尚有：deop_table, add_column, remove_column, rename_column等等。</li>
<li>create_table :users代表建立一個名為users的資料表，其後開始是一個block</li>
<li>t.column :name, :string, :null => false分別代表"table中的欄位"、"欄位名稱"、"資料型態"、"不可為null"，這樣的寫法在Ruby是很常見的寫法，假設你不懂Ruby、但卻對Rails有興趣，我建議你可以透過在學習Rails的過程中慢慢熟悉Ruby語言的寫法，我相信很快就上手。</li>
<li>有關於建立欄位時的相關選項，可以參考Rails的文件，或是往後的文章或許會用到，我會再一一介紹。
</li>
<li>別忘了created_at, updated_at是ActiveRecord中的Magic Column Name，它可以幫你在對資料表寫入資料的時候，自動產生資料建立或修改的時間。（雖然說寫入時間這種事情只要一行就可以解決）</li>
</ul>
<p>
接下來我們要繼續完成與Video、Comment這兩個model相關的migration。migration檔案通常是由generator在建立model的同時，就會自動新增一個migration的檔案，而generator也可以單獨產生一個migration file讓你對資料庫的schema進行改變。
</p>
<pre class="prettyprint">
#Migration: rails_app/app/db/migrate/002_create_videos.rb
class CreateVideos < ActiveRecord::Migration
  def self.up
    create_table :videos do |t|
      t.column :title, :string, :null => false
      t.column :description, :text
      t.column :user_id, :integer
      t.column :created_at, :datetime
    end
  end

  def self.down
    drop_table :videos
  end
end
</pre>
<pre class="prettyprint">
#Migration: rails_app/app/db/migrate/003_create_comments.rb
class CreateComments < ActiveRecord::Migration
  def self.up
    create_table :comments do |t|
      t.column :content, :text
      t.column :user_id, :integer
      t.column :video_id, :integer
      t.column :created_at, :datetime
    end
  end

  def self.down
    drop_table :comments
  end
end
</pre>
<p>從上面的Migration可以發現下列兩點：</p>
<ul>
<li>Migrate目錄裡面的檔案，檔名會依001, 002, 003...的次序為開頭，這代表migration的版本。例如你開發到第10版時，發現第十版新增的table或欄位是有錯的，你可以馬上將資料庫的schema還原到第九版。</li>
<li>上一篇文中我有提到，Magic Column Name中包含了「xxxx_id」這樣的格式，因此在video、comment的table中可以看到出現「user_id」、「video_id」這樣的欄位，往後ActiveRecord便是用這個欄位來替你找到對應的資料（自動幫你join table）</li>
</ul>
<h1>將Migration定義的內容寫入資料庫</h1>
<p>從上一篇文章透過generate指令來產生model、migration files，到本篇文章我們實際去修改上述的檔案內容，如果熟練大概三分鐘就可以完成，接下來便可以開始感受到ActiveRecord所帶來的高生產力與享受:D
</p>
<p>
做完上述動作之後，我們便要將定義好的table schema寫入資料庫裡面，在Rails的目錄結構裡面，config/database.yml便是在定義資料庫的相關設定值。設定資料庫的動作一般是在建立好Rails Project便該完成的，這算是開發Rails project的前置動作之一，那為甚麼我到現在才說咧？其實只是我忘了先講怎麼建立專案、怎麼設定資料庫罷了.....以後如果要把這個系列彙整起來給Rails新鮮人看，再寫一篇好了XD</p>
<p>
關於Rails Project的前置動作可以參考<a href="http://blog.pbg4.org/">午夜盧比人Winson</a>寫的「<a href="http://blog.pbg4.org/2007/8/3/my-rails-way">My Rails Way</a>」，裡面提到的技巧都相當實用，像是第一點提到的</p>
<blockquote><p>
為了節省時間，我通常會維護一個叫做sample的Rails project，裡面把簡易的帳號、密碼、登入做起來，以及設計一個model，也把他的tag、comment功能作起來，並把相關的測試案例寫好、常用的外掛裝起來，然後整包壓成一個sample.zip檔。
</p></blockquote>
<p>倘若資料庫已經設定好，接下來會使用到「rake」指令來將我們規劃好的schema寫入資料庫裡面，指令如下：<br />
<code>rake db:migrate</code><br />
通常會得到下面的結果：</p>
<pre class="prettyprint">
deduce@nccu:~/test$ rake db:migrate
(in /home/deduce/test)
== CreateUsers: migrating ======================================
-- create_table(:users)
   -> 0.0170s
== CreateUsers: migrated (0.0174s) =============================

== CreateVideos: migrating =====================================
-- create_table(:videos)
   -> 0.0090s
== CreateVideos: migrated (0.0094s) ============================

== CreateComments: migrating ===================================
-- create_table(:comments)
   -> 0.0089s
== CreateComments: migrated (0.0093s) ==========================
</pre>
<p>如果沒有出現錯誤訊息就代表資料表皆成功建立，接下來我們便可以開始使用ActiveRecord來對資料庫進行各種關聯式的存取動作！
</p>
<p>註：實在是有點拖稿，不過我寫這些文章是希望可以盡量淺白，所以盡量詳細、盡量簡單，一樣，有任何錯誤或建議請多指教。</p>
]]></content:encoded>
			<wfw:commentRss>http://punk.tw/2007/09/18/rails%e5%ad%b8%e7%bf%92%e5%bf%83%e5%be%97-activerecord%ef%bc%9a%e5%ae%9a%e7%be%a9model%e4%b9%8b%e9%96%93%e7%9a%84%e9%97%9c%e4%bf%82/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>[Rails學習心得] ActiveRecord：存取資料庫不用寫SQL語法？</title>
		<link>http://punk.tw/2007/09/15/rails%e5%ad%b8%e7%bf%92%e5%bf%83%e5%be%97-activerecord%ef%bc%9a%e5%ad%98%e5%8f%96%e8%b3%87%e6%96%99%e5%ba%ab%e4%b8%8d%e7%94%a8%e5%af%absql%e8%aa%9e%e6%b3%95%ef%bc%9f</link>
		<comments>http://punk.tw/2007/09/15/rails%e5%ad%b8%e7%bf%92%e5%bf%83%e5%be%97-activerecord%ef%bc%9a%e5%ad%98%e5%8f%96%e8%b3%87%e6%96%99%e5%ba%ab%e4%b8%8d%e7%94%a8%e5%af%absql%e8%aa%9e%e6%b3%95%ef%bc%9f#comments</comments>
		<pubDate>Fri, 14 Sep 2007 17:35:59 +0000</pubDate>
		<dc:creator>deduce</dc:creator>
				<category><![CDATA[Ruby on Rails學習筆記]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://punk.tw/2007/09/15/241.html</guid>
		<description><![CDATA[ActiveRecord：存取資料庫不用寫SQL語法？ 首先，Ruby on Rails對我而言，最大的衝擊便是Rails中的ActiveRecord實作了ORM (Object-relational mapping)，讓程式對資料庫進行存取的動作時，程式設計師不需要寫任何一行SQL語法，就可以存取到特定的資料表並找到自己想要的資料，沒接觸過Rails或是ActiveRecord的人，會認為：「真的有這麼神奇嗎？」 我可以告訴你，是的，真的就這麼神奇！但，90%的一般性需求不需要寫SQL語法，而剩下的10%特殊需求，Rails又提供給你充足的彈性讓你自訂SQL語法對資料庫進行存取。 如此的作法正符合了Convention Over Configuration的精神，RoR裡面使用大量的慣例來方便程式設計師進行開發，當慣例無法滿足需求時，她同時間會提供足夠的空間讓你自訂想要的功能。 舉例來說，假設要開發一個類似Youtube的網站，資料庫中起碼會有這三個資料表： users：儲存使用者資料 videos：儲存影片的基本資料、發表時間（此例不考慮影像壓縮、儲存技術XD） comments：儲存使用者發表的影片評論 在Rails中的作法，通常會使用下列的語法來建立與資料表相對應的三個Model： ruby script/generate model user ruby script/generate model video ruby script/generate model comment &#34;generate&#34;指令除了會產生上述的三個Model，同時還會產生三個對應的migration，migration中文通常翻作遷移，概念是資料庫的版本演進由migration來控制，包含新增、刪除資料表，或是新增欄位、刪除欄位、欄位類型變更、欄位名稱變更、欄位屬性變更等等，都可以在migration裡面完成。 由migration來進行資料表的管理，優點是無須在意後端使用的資料庫系統為何，migration會自動幫你產生對應到不同資料庫的資料型態及語法，因此無論後端是MySQL、MSSQL Server、DB2、Oracle，ActiveRecord的Migration都可以替你完成資料表的建置與管理。（事實上可能不是每個資料庫系統都有100%的支援。） 由generate指令所產生的migration檔案通常如下： class CreateUsers < ActiveRecord::Migration def self.up create_table :users do &#124;t&#124; end end def self.down drop_table :users end end 上述的程式碼會對資料庫進行新增table的動作，可以發現其中定義了self.up及self.down兩個method，這代表當程式設計者對於目前對資料庫進行的修改感到不滿意時，可以馬上回到前一個版本，此時migration便會自動執行self.down的程式；換句話說，migration裡面定義的是兩個反向的動作，例如：新增table、移除table；新增欄位、移除欄位。 接下來我將這個CreateUsers的migration修改如下： class CreateUsers < ActiveRecord::Migration [...]]]></description>
			<content:encoded><![CDATA[<h1>ActiveRecord：存取資料庫不用寫SQL語法？</h1>
<p>首先，Ruby on Rails對我而言，最大的衝擊便是Rails中的ActiveRecord實作了ORM (<a href="http://en.wikipedia.org/wiki/Object-relational_mapping">Object-relational mapping</a>)，讓程式對資料庫進行存取的動作時，程式設計師不需要寫任何一行SQL語法，就可以存取到特定的資料表並找到自己想要的資料，沒接觸過Rails或是ActiveRecord的人，會認為：「真的有這麼神奇嗎？」</p>
<p>我可以告訴你，是的，真的就這麼神奇！<strong>但</strong>，90%的一般性需求不需要寫SQL語法，而剩下的10%特殊需求，Rails又提供給你充足的彈性讓你自訂SQL語法對資料庫進行存取。</p>
<p>如此的作法正符合了Convention Over Configuration的精神，RoR裡面使用大量的慣例來方便程式設計師進行開發，當慣例無法滿足需求時，她同時間會提供足夠的空間讓你自訂想要的功能。</p>
<p>舉例來說，假設要開發一個類似Youtube的網站，資料庫中起碼會有這三個資料表：</p>
<ul>
<li>users：儲存使用者資料 </li>
<li>videos：儲存影片的基本資料、發表時間（此例不考慮影像壓縮、儲存技術XD） </li>
<li>comments：儲存使用者發表的影片評論 </li>
</ul>
<p>在Rails中的作法，通常會使用下列的語法來<strong>建立與資料表相對應的三個Model</strong>：</p>
<p><code>ruby script/generate model user      <br />ruby script/generate model video       <br />ruby script/generate model comment</code></p>
<p>&quot;generate&quot;指令除了會產生上述的三個Model，同時還會產生三個對應的migration，migration中文通常翻作遷移，概念是資料庫的版本演進由migration來控制，包含新增、刪除資料表，或是新增欄位、刪除欄位、欄位類型變更、欄位名稱變更、欄位屬性變更等等，都可以在migration裡面完成。</p>
<p>由migration來進行資料表的管理，優點是無須在意後端使用的資料庫系統為何，migration會自動幫你產生對應到不同資料庫的資料型態及語法，因此無論後端是MySQL、MSSQL Server、DB2、Oracle，ActiveRecord的Migration都可以替你完成資料表的建置與管理。（事實上可能不是每個資料庫系統都有100%的支援。）</p>
<p>由generate指令所產生的migration檔案通常如下：<br /> 
<pre class="prettyprint">
class CreateUsers < ActiveRecord::Migration
  def self.up
    create_table :users do |t|
    end
  end

  def self.down
    drop_table :users
  end
end
</pre>
<p>上述的程式碼會對資料庫進行新增table的動作，可以發現其中定義了self.up及self.down兩個method，這代表當程式設計者對於目前對資料庫進行的修改感到不滿意時，可以馬上回到前一個版本，此時migration便會自動執行self.down的程式；換句話說，migration裡面定義的是兩個反向的動作，例如：新增table、移除table；新增欄位、移除欄位。
</p>
<p>接下來我將這個CreateUsers的migration修改如下：</p>
<pre class="prettyprint">
class CreateUsers < ActiveRecord::Migration
  def self.up
    create_table :users do |t|
      t.column :name, :string, :null => false
      t.column :email, :string, :null => false
      t.column :password, :string, :null => false
      t.column :nickname, :string, :null => false
      t.column :memo, :text
      t.column :created_at, :datetime
      t.column :updated_at, :datetime
    end
  end

  def self.down
    drop_table :users
  end
end
</pre>
<p>我定義了七個欄位給users這張table，如此的定義方式，在接下來的開發過程將會有幾個重要的效果：</p>
<ul>
<li>ActiveRecord的慣例是自動為每個Table建立「id」這個欄位，作為Primary Key以供存取；換句話說，表格內實際上會有我定義的七個欄位加上id共八個欄位。當然，這是慣例，你不想要id這個欄位也是可以的:D</li>
<li>接下來你可以使用User.find(1)來找到在users資料表中id=1的資料，背後其實是自動幫你完成一段SQL語法："SELECT * FROM users WHERE users.id=1"</li>
<li>當然，你可以使用User.find_by_name("deduce")或是User.find_by_email("deduce@gmail.com")這樣的方式來找到特定的資料，背後實際上是幫你轉換成類似"SELECT * FROM users WHERE users.email = 'deduce@gmail.com'"這樣的語法。這在Ruby裡面是Dynamic Methods的概念，她會根據class definition去產生對應的class method來方便使用。</li>
<li>另外Find method也可以用User.find(:all, :conditions => "搜尋條件", <img src='http://punk.tw/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> rder => "排序條件")來找資料，絕大多數的資料存取都可以靠這樣的方式完成，其他的可以靠User.find_by_sql("SQL語法")來完成:)
</li>
<li>接著，下面這段程式可以簡單地在網頁上show出某個user的基本資料
<pre class="prettyprint">
<% user = User.find(1) %>
帳號：<%= user.name %>
電子郵件：<%= user.email %>
暱稱：<%= user.nickname %>
何時註冊？<%= user.created_at %>
</pre>
<p>如何？是不是非常簡單而且非常直觀:)
</li>
<li>此外，注意到兩個欄位：created_at及updated_at，這樣的欄位名稱在ActiveRecord中稱為「Magic Column Names」，欄位只要是以Magic Column Names來命名，在資料寫入Table的同時，ActiveRecord會自動幫你產生相關的資料。以created_at來說就是資料的建立時間，而updated_at則是資料的修改時間。常見的Magic Column Names還有created_on, updated_on（_at與_on的差別在於一個有記錄詳細時間、一個只記錄日期），xxxx_id則是預設的Foreign Key名稱，例如我們稍後會在video及comment兩個table建立user_id當作Foreign Key來reference到user table，如此便可以得知某個video或comment是屬於哪個user。
</li>
<li>想統計目前站上有多少註冊會員？簡單，網頁上輸出一行&lt;%= User.count %&gt;便是目前的會員數了，或是想一口氣輸出全站User的資料，可以用如下寫法：
<pre class="prettyprint">
<% users = User.find(:all) %>
  <% users.each do |user| %>
  User序號：<= user.id %>
  帳號：<%= user.name %>
  電子郵件：<%= user.email %>
  暱稱：<%= user.nickname %>
  何時註冊？<%= user.created_at %>
<% end %>
</pre>
<p>（Ruby也可以用 for user in users的寫法，不過我比較習慣用.each）
</li>
</ul>
<p>
事實上，要深入談ActiveRecord的話，是可以寫成一本書的，最近才剛出版的一本新書「<a href="http://www.amazon.com/Pro-ActiveRecord-Databases-Ruby-Rails/dp/1590598474">Pro ActiveRecord: Database with Ruby on Rails</a>」內容便是在探討ActiveRecord的深入技巧，Amazon上的Book Description中有一段話是這麼說的：</p>
<blockquote><p>
Because ActiveRecord is configured with default assumptions that mesh perfectly with the Rails framework, Rails developers often find they hardly need think about it at all. However, if you are developing in Ruby without Rails, or are deploying against legacy databases designed without Rails in mind, or you just want to take advantage of database-specific features such as large objects and stored procedures, you need the in-depth knowledge of ActiveRecord found in this book.<br />大致上是說，身為一個Rails developer，由於Rails跟ActiveRecord有著許多的慣例（configured with defaul assumptions），因此我們通常不太需要想太多，反正用就對了。然而如果身為一個Rails developer，或是你想單獨拿ActiveRecord來開發其他與資料庫有關的project，就需要有更多深入的瞭解，這也是這本書的主要目的。</p></blockquote>
<p>我想這或許會是一本值得購入的好書吧？:D
</p>
<p>這篇文章簡單地介紹了ActiveRecord如何讓Rails developer輕鬆地存取資料，下一篇再來談Model與Model之間要如何運作，文中有任何錯誤或對本文有任何指教請留言或來信告知（deduce_at_gmail_dot_com），感激不盡！</p>
]]></content:encoded>
			<wfw:commentRss>http://punk.tw/2007/09/15/rails%e5%ad%b8%e7%bf%92%e5%bf%83%e5%be%97-activerecord%ef%bc%9a%e5%ad%98%e5%8f%96%e8%b3%87%e6%96%99%e5%ba%ab%e4%b8%8d%e7%94%a8%e5%af%absql%e8%aa%9e%e6%b3%95%ef%bc%9f/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>10分鐘內開發出Twitter網站：使用Ruby on Rails</title>
		<link>http://punk.tw/2007/09/15/10%e5%88%86%e9%90%98%e5%85%a7%e9%96%8b%e7%99%bc%e5%87%batwitter%e7%b6%b2%e7%ab%99%ef%bc%9a%e4%bd%bf%e7%94%a8ruby-on-rails</link>
		<comments>http://punk.tw/2007/09/15/10%e5%88%86%e9%90%98%e5%85%a7%e9%96%8b%e7%99%bc%e5%87%batwitter%e7%b6%b2%e7%ab%99%ef%bc%9a%e4%bd%bf%e7%94%a8ruby-on-rails#comments</comments>
		<pubDate>Fri, 14 Sep 2007 17:15:15 +0000</pubDate>
		<dc:creator>deduce</dc:creator>
				<category><![CDATA[Ruby on Rails學習筆記]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://punk.tw/2007/09/15/240.html</guid>
		<description><![CDATA[剛剛在訂便當被買走了這篇文章提到，用Ruby on Rails可以很快地開發出一個網站，到底有多快呢？；事實上如果已經聽過或是稍微接觸過Ruby on Rails的朋友，應該都有聽說關於RoR的高生產力傳說，例如thegiive去年曾經po過的幾篇文章，都讓我學習Rails的心更加堅定XD像是他曾經提到： 根據 Beyond JAVA 裡面，Justin Gehtland 用 Rails 重寫一個用 JAVA Spring/Hibernate 寫好的 Project，他發現程式碼比例 JAVA ：Rails 大概是 3.48 : 1 。附帶一提，他重寫的開發時間開發時間比是 16 : 1，更噁心的數字。 當然我後來也去弄了一本Beyond Java來看（有出中文翻譯本，某些學校圖書館或許能借到），對當時的我而言，聽到這麼多RoR的高生產力傳說、又好像可以少寫很多程式碼來達到同樣的目的，我就開始進入了Rails的世界，雖然一年來進步緩慢，不過畢竟是當興趣嘛XD &#xA0; 左邊這張圖片大概可以也可以稍微表達為何我熱愛Ruby &#38; Rails，而不乖乖聽老師的話學Java XD &#xA0; anyway，這篇文章的重點應該是標題中提到的「用RoR在十分鐘內開發出Twitter網站」，這是我在日本ZDNet網站上看到的一個短片「Ruby on Railsで10分で作るTwitterもどき」，有興趣的請直接連結到該網站觀看影片，網站上有提供影片中原始碼的下載！ 作者用十分鐘左右的時間開發出一個簡單的Twitter雛型，包含了三個Model: User, friendship, status，分別是使用者的個人資料、好友名單的relation以及留下的訊息，光看這樣好像滿簡單的，但要在十分鐘內飆出來也不是那麼簡單。（如果是我就先把程式碼都先放在旁邊準備複製貼上XD） 我想作者可以在十分鐘內就完成這支簡單的程式，除了對Ruby on Rails要夠熟悉之外，還有兩個很重要的關鍵： TextMate在編輯過程中節省大量的coding時間。 Rails的兩大精神幫了許多忙Don't Repeat Yourselft, Convention over Configuration 有關TextMate這套編輯器，應該是很多Rails developer的最愛，關於TextMate應該是另外一個故事了，我最近也正式成為Mac User，尚在摸索這套工具所能帶來的高生產力與享受:p [...]]]></description>
			<content:encoded><![CDATA[<p>剛剛在<a href="http://punk.tw/2007/09/14/234.html">訂便當被買走了</a>這篇文章提到，用Ruby on Rails可以很快地開發出一個網站，到底有多快呢？；事實上如果已經聽過或是稍微接觸過Ruby on Rails的朋友，應該都有聽說關於RoR的高生產力傳說，例如<a href="http://lightyror.thegiive.net/">thegiive</a>去年曾經po過的幾篇文章，都讓我學習Rails的心更加堅定XD像是他<a href="http://lightyror.thegiive.net/2006/10/rails.html">曾經提到</a>：</p>
<blockquote><p>根據 <a href="http://www.amazon.com/Beyond-Java-Bruce-Tate/dp/0596100949/sr=8-1/qid=1161735660/ref=pd_bbs_sr_1/102-3923019-8384917?ie=UTF8&amp;s=books">Beyond JAVA </a>裡面，Justin Gehtland 用 Rails 重寫一個用 JAVA Spring/Hibernate 寫好的 Project，他發現程式碼比例 JAVA ：Rails 大概是 3.48 : 1 。附帶一提，他重寫的開發時間開發時間比是 16 : 1，更噁心的數字。</p>
</blockquote>
<p><a href="http://punk.tw/wp-content/uploads/2007/09/image15.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="261" alt="image" src="http://punk.tw/wp-content/uploads/2007/09/image-thumb15.png" width="316" align="left" border="0" /></a>當然我後來也去弄了一本Beyond Java來看（有出<a href="http://www.oreilly.com.tw/product_java.php?id=a187">中文翻譯本</a>，某些學校圖書館或許能借到），對當時的我而言，聽到這麼多RoR的高生產力傳說、又好像可以少寫很多程式碼來達到同樣的目的，我就開始進入了Rails的世界，雖然一年來進步緩慢，不過畢竟是當興趣嘛XD</p>
<p>&#xA0;</p>
<p>左邊這張圖片大概可以也可以稍微表達為何我熱愛Ruby &amp; Rails，而不乖乖聽老師的話學Java XD</p>
<p>&#xA0;</p>
<p>anyway，這篇文章的重點應該是標題中提到的「<strong>用RoR在十分鐘內開發出<a href="http://twitter.com/">Twitter</a>網站</strong>」，這是我在日本ZDNet網站上看到的一個短片「<a href="http://japan.zdnet.com/video/screencast/story/0,3800079413,20354695,00.htm">Ruby on Railsで10分で作るTwitterもどき</a>」，有興趣的請直接連結到該網站觀看影片，<strong>網站上有提供影片中原始碼的下載！</strong></p>
<p>作者用十分鐘左右的時間開發出一個簡單的Twitter雛型，包含了三個Model: User, friendship, status，分別是使用者的個人資料、好友名單的relation以及留下的訊息，光看這樣好像滿簡單的，但要在十分鐘內飆出來也不是那麼簡單。（如果是我就先把程式碼都先放在旁邊準備複製貼上XD）</p>
<p>我想作者可以在十分鐘內就完成這支簡單的程式，除了對Ruby on Rails要夠熟悉之外，還有兩個很重要的關鍵：</p>
<ol>
<ol>
<li>TextMate在編輯過程中節省大量的coding時間。 </li>
<li>Rails的兩大精神幫了許多忙Don't Repeat Yourselft, Convention over Configuration </li>
</ol>
</ol>
<p>有關TextMate這套編輯器，應該是很多Rails developer的最愛，關於TextMate應該是另外一個故事了，我最近也正式成為Mac User，尚在摸索這套工具所能帶來的高生產力與享受:p</p>
<p>從下篇文章開始，我想分享一些最近開發Rails的感想以及Rails到底在網站的開發過程中幫了些什麼忙。簡單分享個人心得與體會，如果誤會了Rails的精神還請不吝指教:)</p>
]]></content:encoded>
			<wfw:commentRss>http://punk.tw/2007/09/15/10%e5%88%86%e9%90%98%e5%85%a7%e9%96%8b%e7%99%bc%e5%87%batwitter%e7%b6%b2%e7%ab%99%ef%bc%9a%e4%bd%bf%e7%94%a8ruby-on-rails/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rails聖經中文翻譯本：碼上就會Rails敏捷開發網站</title>
		<link>http://punk.tw/2007/08/07/rails%e8%81%96%e7%b6%93%e4%b8%ad%e6%96%87%e7%bf%bb%e8%ad%af%e6%9c%ac%ef%bc%9a%e7%a2%bc%e4%b8%8a%e5%b0%b1%e6%9c%83rails%e6%95%8f%e6%8d%b7%e9%96%8b%e7%99%bc%e7%b6%b2%e7%ab%99</link>
		<comments>http://punk.tw/2007/08/07/rails%e8%81%96%e7%b6%93%e4%b8%ad%e6%96%87%e7%bf%bb%e8%ad%af%e6%9c%ac%ef%bc%9a%e7%a2%bc%e4%b8%8a%e5%b0%b1%e6%9c%83rails%e6%95%8f%e6%8d%b7%e9%96%8b%e7%99%bc%e7%b6%b2%e7%ab%99#comments</comments>
		<pubDate>Tue, 07 Aug 2007 02:17:59 +0000</pubDate>
		<dc:creator>deduce</dc:creator>
				<category><![CDATA[Books]]></category>
		<category><![CDATA[Murmur]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://punk.tw/2007/08/07/rails%e8%81%96%e7%b6%93%e4%b8%ad%e6%96%87%e7%bf%bb%e8%ad%af%e6%9c%ac%ef%bc%9a%e7%a2%bc%e4%b8%8a%e5%b0%b1%e6%9c%83rails%e6%95%8f%e6%8d%b7%e9%96%8b%e7%99%bc%e7%b6%b2%e7%ab%99/</guid>
		<description><![CDATA[昨晚逛天瓏的時候發現Rails的聖經Agile Web Development with Rails的中文翻譯本出版了，聖經大概是我從讀大學以來讀得最勤奮的一本原文書，也因此本來很喜愛收藏各類書籍的我，對這本翻譯書實在提不起太大興趣。 目前在天瓏的價格應該是NT$450（定價600，目前好像是打75折），價格還算可以，如果不習慣讀原文書，這本翻譯書應該是個非常好的選擇。（比起市面上某些中文Rails書籍而言XD） 博客來連結：http://www.books.com.tw/exep/prod/booksfile.php?item=0010374225 參考網站與圖片來源：《碼上就會：Rails敏捷開發網站》新書上市！]]></description>
			<content:encoded><![CDATA[<p>昨晚逛天瓏的時候發現Rails的聖經Agile Web Development with Rails的中文翻譯本出版了，聖經大概是我從讀大學以來讀得最勤奮的一本原文書，也因此本來很喜愛收藏各類書籍的我，對這本翻譯書實在提不起太大興趣。</p>
<p>目前在天瓏的價格應該是NT$450（定價600，目前好像是打75折），價格還算可以，如果不習慣讀原文書，這本翻譯書應該是個非常好的選擇。（比起市面上某些中文Rails書籍而言XD）</p>
<p><a href="http://punk.tw/wp-content/uploads/images/RailsRails_8FD8/image_3.png" atomicselection="true"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="240" alt="image" src="http://punk.tw/wp-content/uploads/images/RailsRails_8FD8/image_thumb_3.png" width="217" border="0"></a> </p>
<p>博客來連結：<a title="http://www.books.com.tw/exep/prod/booksfile.php?item=0010374225" href="http://www.books.com.tw/exep/prod/booksfile.php?item=0010374225">http://www.books.com.tw/exep/prod/booksfile.php?item=0010374225</a></p>
<p>參考網站與圖片來源：<a href="http://swf.com.tw/?p=105">《碼上就會：Rails敏捷開發網站》新書上市！</a></p>
]]></content:encoded>
			<wfw:commentRss>http://punk.tw/2007/08/07/rails%e8%81%96%e7%b6%93%e4%b8%ad%e6%96%87%e7%bf%bb%e8%ad%af%e6%9c%ac%ef%bc%9a%e7%a2%bc%e4%b8%8a%e5%b0%b1%e6%9c%83rails%e6%95%8f%e6%8d%b7%e9%96%8b%e7%99%bc%e7%b6%b2%e7%ab%99/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>AjaxScaffold &amp; ActiveScaffold for Ruby on Rails</title>
		<link>http://punk.tw/2007/06/15/ajaxscaffold-activescaffold-for-ruby-on-rails</link>
		<comments>http://punk.tw/2007/06/15/ajaxscaffold-activescaffold-for-ruby-on-rails#comments</comments>
		<pubDate>Thu, 14 Jun 2007 19:06:17 +0000</pubDate>
		<dc:creator>deduce</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Ruby on Rails Plugin]]></category>

		<guid isPermaLink="false">http://punk.tw/2007/06/15/ajaxscaffold-activescaffold-for-ruby-on-rails/</guid>
		<description><![CDATA[AjaxScaffold: AJAX interface for your Ruby on Rails modelshttp://www.ajaxscaffold.com/ ActiveScaffold: A Ruby on Rails plugin for dynamic, AJAX CRUD interfaceshttp://activescaffold.com/ 我自己還沒有實際用來開發，不過光看demo還滿吸引人的，簡單的CRUD機制都包好並且用Ajax的方式運作，包成Ruby on Rails的plugin使得安裝、使用都很簡單，有空再來研究研究。]]></description>
			<content:encoded><![CDATA[<p>AjaxScaffold: AJAX interface for your Ruby on Rails models<br /><a href="http://www.ajaxscaffold.com/">http://www.ajaxscaffold.com/</a>
<p>ActiveScaffold: A Ruby on Rails plugin for dynamic, AJAX CRUD interfaces<br /><a href="http://activescaffold.com/">http://activescaffold.com/</a>
<p>我自己還沒有實際用來開發，不過光看demo還滿吸引人的，簡單的CRUD機制都包好並且用Ajax的方式運作，包成Ruby on Rails的plugin使得安裝、使用都很簡單，有空再來研究研究。</p>
]]></content:encoded>
			<wfw:commentRss>http://punk.tw/2007/06/15/ajaxscaffold-activescaffold-for-ruby-on-rails/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>HOWTO: Ruby on Rails中取出最新的一筆資料</title>
		<link>http://punk.tw/2007/04/04/howto-ruby-on-rails%e4%b8%ad%e5%8f%96%e5%87%ba%e6%9c%80%e6%96%b0%e7%9a%84%e4%b8%80%e7%ad%86%e8%b3%87%e6%96%99</link>
		<comments>http://punk.tw/2007/04/04/howto-ruby-on-rails%e4%b8%ad%e5%8f%96%e5%87%ba%e6%9c%80%e6%96%b0%e7%9a%84%e4%b8%80%e7%ad%86%e8%b3%87%e6%96%99#comments</comments>
		<pubDate>Wed, 04 Apr 2007 14:02:27 +0000</pubDate>
		<dc:creator>deduce</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://punk.tw/2007/04/04/howto-ruby-on-rails%e4%b8%ad%e5%8f%96%e5%87%ba%e6%9c%80%e6%96%b0%e7%9a%84%e4%b8%80%e7%ad%86%e8%b3%87%e6%96%99/</guid>
		<description><![CDATA[使用Active Record時可以使用 Model.find(:first)來取出第一筆資料，或是Model.find(:all)來取出全部的資料，那如果想要取出最近的一筆資料該怎麼做呢？其實只需要加個排序的條件即可。 範例： @event = Event.find(:first, : order =&#62; "created_at DESC") Updated: 補充一下，其實如果是@event = Event.find(:all)，那麼@event.last便是最後一筆資料了。]]></description>
			<content:encoded><![CDATA[<p>使用Active Record時可以使用 Model.find(:first)來取出第一筆資料，或是Model.find(:all)來取出全部的資料，那如果想要取出最近的一筆資料該怎麼做呢？其實只需要加個排序的條件即可。</p>
<div>範例：</div>
<p><code>@event = Event.find(:first, : order =&gt; "created_at DESC")</code>
<p>Updated: 補充一下，其實如果是@event = Event.find(:all)，那麼@event.last便是最後一筆資料了。</p>
]]></content:encoded>
			<wfw:commentRss>http://punk.tw/2007/04/04/howto-ruby-on-rails%e4%b8%ad%e5%8f%96%e5%87%ba%e6%9c%80%e6%96%b0%e7%9a%84%e4%b8%80%e7%ad%86%e8%b3%87%e6%96%99/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ruby on Rails</title>
		<link>http://punk.tw/2007/03/23/ruby-on-rails-2</link>
		<comments>http://punk.tw/2007/03/23/ruby-on-rails-2#comments</comments>
		<pubDate>Thu, 22 Mar 2007 16:43:05 +0000</pubDate>
		<dc:creator>deduce</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://punk.tw/2007/03/23/ruby-on-rails-2/</guid>
		<description><![CDATA[昨天在網路上抓到的一張Ruby on Rails的架構圖，點圖可放大。 P.S.若有侵權請來信告知deduce_@_gmail.com, thanks!]]></description>
			<content:encoded><![CDATA[<p><a href="http://punk.tw/wp-content/uploads/images/RubyonRails_A13/084510_1955218822_wiegsazn9.jpg" atomicselection="true"><img height="181" src="http://punk.tw/wp-content/uploads/images/RubyonRails_A13/084510_1955218822_wiegsazn_thumb7.jpg" width="240"></a> </p>
<p>昨天在網路上抓到的一張Ruby on Rails的架構圖，點圖可放大。</p>
<p>P.S.若有侵權請來信告知deduce_@_gmail.com, thanks!</p>
]]></content:encoded>
			<wfw:commentRss>http://punk.tw/2007/03/23/ruby-on-rails-2/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rails Recipes</title>
		<link>http://punk.tw/2007/03/18/rails-recipes</link>
		<comments>http://punk.tw/2007/03/18/rails-recipes#comments</comments>
		<pubDate>Sun, 18 Mar 2007 08:53:09 +0000</pubDate>
		<dc:creator>deduce</dc:creator>
				<category><![CDATA[Books]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://punk.tw/2007/03/18/rails-recipes/</guid>
		<description><![CDATA[我連Agile Web Development with Rails都還沒看完，就迫不及待拿出Rails Recipes開始看看裡面提供了些什麼有趣的玩意兒。Rails Recipes這本書比較適合已經對於Ruby on Rails有點瞭解，甚至是已經開始製作、經營網站的User，裡面提供了一些很實用的技巧，共70個Recipes分成以下幾篇： User Interface Recipes Database Recipes Controller Recipes Testing Recipes Big-Picture Recipes Email Recipes 我覺得這本書跟Agile Web Development with Rails都很有收藏價值，Rails Recipe在每一章的開頭會先敘述一段網站開發者可能遭遇的「Problem」（或是一段簡單的場景敘述），隨後列出需要準備的「Ingredients」，並且提出對應的「Solution」，內容我認為是簡短有力，沒事拿來當閒書翻閱一下或是放在旁邊當工具書參考用都很有幫助。 註：另一本「Advanced Rails Recipes」預計在August 2007出版，真是令人期待！http://www.pragmaticprogrammer.com/titles/fr_mrr/index.html]]></description>
			<content:encoded><![CDATA[</p>
<p><img height="166" src="http://punk.tw/wp-content/uploads/images/RailsRecipes_EC85/image04.png" width="138"> </p>
<p>我連Agile Web Development with Rails都還沒看完，就迫不及待拿出Rails Recipes開始看看裡面提供了些什麼有趣的玩意兒。Rails Recipes這本書比較適合已經對於Ruby on Rails有點瞭解，甚至是已經開始製作、經營網站的User，裡面提供了一些很實用的技巧，共70個Recipes分成以下幾篇：</p>
<ul>
<li>User Interface Recipes</li>
<li>Database Recipes</li>
<li>Controller Recipes</li>
<li>Testing Recipes</li>
<li>Big-Picture Recipes</li>
<li>Email Recipes</li>
</ul>
<p>我覺得這本書跟Agile Web Development with Rails都很有收藏價值，Rails Recipe在每一章的開頭會先敘述一段網站開發者可能遭遇的「Problem」（或是一段簡單的場景敘述），隨後列出需要準備的「Ingredients」，並且提出對應的「Solution」，內容我認為是簡短有力，沒事拿來當閒書翻閱一下或是放在旁邊當工具書參考用都很有幫助。</p>
<p>註：另一本「Advanced Rails Recipes」預計在August 2007出版，真是令人期待！<br /><a title="http://www.pragmaticprogrammer.com/titles/fr_mrr/index.html" href="http://www.pragmaticprogrammer.com/titles/fr_mrr/index.html">http://www.pragmaticprogrammer.com/titles/fr_mrr/index.html</a></p>
]]></content:encoded>
			<wfw:commentRss>http://punk.tw/2007/03/18/rails-recipes/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>22 Successful Ruby on Rails Web Applications</title>
		<link>http://punk.tw/2007/03/17/22-successful-ruby-on-rails-web-applications</link>
		<comments>http://punk.tw/2007/03/17/22-successful-ruby-on-rails-web-applications#comments</comments>
		<pubDate>Sat, 17 Mar 2007 13:33:09 +0000</pubDate>
		<dc:creator>deduce</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Web2.0 Website]]></category>

		<guid isPermaLink="false">http://punk.tw/2007/03/17/22-successful-ruby-on-rails-web-applications/</guid>
		<description><![CDATA[http://rubycorner.net/articles/22-successful-ruby-on-rails-web-applications/ 這篇文章介紹了22個成功的Ruby on Rails個案，稍微做個簡單的中文翻譯替螢幕前的訪客省點時間好了:) （另外在http://happycodr.com/biglist有更多的網站介紹，共有388個！） 第一部份主要是37signals他們公司所經營的網站， Basecamphttp://www.basecamphq.com專案管理網站 Campfirehttp://www.campfirenow.com即時群組聊天網站 Backpackhttp://www.backpackit.com資訊彙整與行事曆網站 Tadalisthttp://www.tadalist.com簡單好用的Todo List（要做的事） Writeboardhttp://www.writeboard.com線上共同編輯文件 第二部份則是在RubyonRails.org有介紹的網站， 43things.comhttp://www.43things.com這個網站也算是Web 2.0與Ruby on Rails的經典網站，人們可以上去分享自己想做的事情，藉此來找到跟自己有著相同目標的朋友們。 odeo.comhttp://odeo.com音樂分享網站 shopify.comhttp://shopify.com提供使用者快速建立屬於自己的電子商務網站 fluxionhttp://www.fluxiom.com管理、組織並分想你的數位資產 strongspace.comhttp://www.strongspace.comFile hosting檔案分享網站 Typohttp://www.typosphere.orgBlogging engine，這在Ruby on Rails也算是很紅的一套blog系統 第三部份應該是筆者自行發現的網站，還滿不錯的～看到好多以前沒逛過的站 ajaxed.comhttp://www.ajaxed.com不需要寫程式即可輕輕鬆鬆在你現有的網站上加上Ajax的效果 atopsites.comhttp://atopsites.com仿Alaxa.com的網站 asoboo.comhttp://asoboo.com針對Creative people所設立的社群網站（筆者特別強調該網站是佈署在Win2003） thebeststuffintheworldhttp://www.thebeststuffintheworld.com有趣的web 2.0應用 dealplumber.comhttp://www.dealplumber.com另一個有趣的社群網站 digital-tunes.nethttp://www.digital-tunes.net專做音樂的電子商務網站 flagr.comhttp://www.flagr.com看起來應該是跟Google map有關的Mashup godefy.comhttp://www.godefy.com有關網路行銷公司或產品的比較 lovexpress.frhttp://www.lovexpress.fr法國的交友網站 podomatic.comhttp://podomatic.com專門分享podcasts的網站 pixrat.comhttp://www.pixrat.comSocial picture bookmars，人家del.icio.us做的是書籤，pixrat做的是「圖籤」，其實前陣子出現的diggirl概念就類似Pixrat，都是要收藏美女並且用類似「digg」的方式來推選出本日熱門，不過Pixrat的精神比較接近書籤，比較著重在收藏、分享，而diggirl則是著重在本日最熱門，比較接近digg的概念。（但diggirl同時又搭配幹圖王可以收藏，還滿威的！）]]></description>
			<content:encoded><![CDATA[<p><a title="http://rubycorner.net/articles/22-successful-ruby-on-rails-web-applications/" href="http://rubycorner.net/articles/22-successful-ruby-on-rails-web-applications/">http://rubycorner.net/articles/22-successful-ruby-on-rails-web-applications/</a></p>
<p>這篇文章介紹了22個成功的Ruby on Rails個案，稍微做個簡單的中文翻譯替螢幕前的訪客省點時間好了:)</p>
<p>（另外在<a href="http://happycodr.com/biglist">http://happycodr.com/biglist</a>有更多的網站介紹，共有388個！）</p>
<p>第一部份主要是37signals他們公司所經營的網站，</p>
<ol>
<li>Basecamp<br /><a href="http://www.basecamphq.com">http://www.basecamphq.com</a><br />專案管理網站
<li>Campfire<br /><a href="http://www.campfirenow.com">http://www.campfirenow.com</a><br />即時群組聊天網站
<li>Backpack<br /><a href="http://www.backpackit.com">http://www.backpackit.com</a><br />資訊彙整與行事曆網站
<li>Tadalist<br /><a href="http://www.tadalist.com">http://www.tadalist.com</a><br />簡單好用的Todo List（要做的事）
<li>Writeboard<br /><a href="http://www.writeboard.com">http://www.writeboard.com</a><br />線上共同編輯文件</li>
</ol>
<p>第二部份則是在RubyonRails.org有介紹的網站，</p>
<ol>
<li>43things.com<br /><a href="http://www.43things.com">http://www.43things.com</a><br />這個網站也算是Web 2.0與Ruby on Rails的經典網站，人們可以上去分享自己想做的事情，藉此來找到跟自己有著相同目標的朋友們。
<li>odeo.com<br /><a href="http://odeo.com">http://odeo.com</a><br />音樂分享網站
<li>shopify.com<br /><a href="http://shopify.com">http://shopify.com</a><br />提供使用者快速建立屬於自己的電子商務網站
<li>fluxion<br /><a href="http://www.fluxiom.com">http://www.fluxiom.com</a><br />管理、組織並分想你的數位資產
<li>strongspace.com<br /><a href="http://www.strongspace.com">http://www.strongspace.com</a><br />File hosting檔案分享網站
<li>Typo<br /><a href="http://www.typosphere.org">http://www.typosphere.org</a><br />Blogging engine，這在Ruby on Rails也算是很紅的一套blog系統</li>
</ol>
<p>第三部份應該是筆者自行發現的網站，還滿不錯的～看到好多以前沒逛過的站</p>
<ol>
<li>ajaxed.com<br /><a href="http://www.ajaxed.com">http://www.ajaxed.com</a><br />不需要寫程式即可輕輕鬆鬆在你現有的網站上加上Ajax的效果
<li>atopsites.com<br /><a href="http://atopsites.com">http://atopsites.com</a><br />仿Alaxa.com的網站
<li>asoboo.com<br /><a href="http://asoboo.com">http://asoboo.com</a><br />針對Creative people所設立的社群網站（筆者特別強調該網站是佈署在Win2003）
<li>thebeststuffintheworld<br /><a href="http://www.thebeststuffintheworld.com">http://www.thebeststuffintheworld.com</a><br />有趣的web 2.0應用
<li>dealplumber.com<br /><a href="http://www.dealplumber.com">http://www.dealplumber.com</a><br />另一個有趣的社群網站
<li>digital-tunes.net<br /><a href="http://www.digital-tunes.net">http://www.digital-tunes.net</a><br />專做音樂的電子商務網站
<li>flagr.com<br /><a href="http://www.flagr.com">http://www.flagr.com</a><br />看起來應該是跟Google map有關的Mashup
<li>godefy.com<br /><a href="http://www.godefy.com">http://www.godefy.com</a><br />有關網路行銷公司或產品的比較
<li>lovexpress.fr<br /><a href="http://www.lovexpress.fr">http://www.lovexpress.fr</a><br />法國的交友網站
<li>podomatic.com<br /><a href="http://podomatic.com">http://podomatic.com</a><br />專門分享podcasts的網站
<li>pixrat.com<br /><a href="http://www.pixrat.com">http://www.pixrat.com</a><br />Social picture bookmars，人家del.icio.us做的是書籤，pixrat做的是「<strong>圖籤</strong>」，其實前陣子出現的diggirl概念就類似Pixrat，都是要收藏美女並且用類似「digg」的方式來推選出本日熱門，不過Pixrat的精神比較接近書籤，比較著重在收藏、分享，而diggirl則是著重在本日最熱門，比較接近digg的概念。（但diggirl同時又搭配幹圖王可以收藏，還滿威的！）</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://punk.tw/2007/03/17/22-successful-ruby-on-rails-web-applications/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Top 10 Ruby on Rails performance tips</title>
		<link>http://punk.tw/2007/03/14/top-10-ruby-on-rails-performance-tips</link>
		<comments>http://punk.tw/2007/03/14/top-10-ruby-on-rails-performance-tips#comments</comments>
		<pubDate>Wed, 14 Mar 2007 06:01:15 +0000</pubDate>
		<dc:creator>deduce</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://punk.tw/2007/03/14/top-10-ruby-on-rails-performance-tips/</guid>
		<description><![CDATA[http://www.antoniocangiano.com/articles/2007/02/10/top-10-ruby-on-rails-performance-tips 作者提出十個針對Ruby on Rails Performance的小技巧，有興趣的可以到該網頁參考。 Optimize your Ruby code. Caching is good. Use your database to the full extent of the law. Finders are great but be careful. Group operations in a transaction. Control your controllers. Use HTML for you views. Logging. Patch the GC.(Garbage Collection) A final not, be "performance aware" and work on [...]]]></description>
			<content:encoded><![CDATA[<p><a title="http://www.antoniocangiano.com/articles/2007/02/10/top-10-ruby-on-rails-performance-tips" href="http://www.antoniocangiano.com/articles/2007/02/10/top-10-ruby-on-rails-performance-tips">http://www.antoniocangiano.com/articles/2007/02/10/top-10-ruby-on-rails-performance-tips</a></p>
<p>作者提出十個針對Ruby on Rails Performance的小技巧，有興趣的可以到該網頁參考。</p>
<ol>
<li>Optimize your Ruby code.</li>
<li>Caching is good.</li>
<li>Use your database to the full extent of the law.</li>
<li>Finders are great but be careful.</li>
<li>Group operations in a transaction.</li>
<li>Control your controllers.</li>
<li>Use HTML for you views.</li>
<li>Logging.</li>
<li>Patch the GC.(Garbage Collection)</li>
<li>A final not, be "performance aware" and work on your code with these principle in mind.</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://punk.tw/2007/03/14/top-10-ruby-on-rails-performance-tips/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rails for Designers</title>
		<link>http://punk.tw/2007/03/08/rails-for-designers</link>
		<comments>http://punk.tw/2007/03/08/rails-for-designers#comments</comments>
		<pubDate>Wed, 07 Mar 2007 17:14:33 +0000</pubDate>
		<dc:creator>deduce</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://punk.tw/2007/03/08/rails-for-designers/</guid>
		<description><![CDATA[http://glu.ttono.us/articles/2006/03/21/rails-for-designers 這是一篇寫給為Rails環境做視覺設計、美工設計的網頁設計師看的文章。作者簡單地介紹了MVC架構，也就是Ruby on Rails最基本的架構，隨後開始介紹Layouts的概念以及如何在網頁中插入Ruby on Rails所產生的動態網頁內容。 說實在文章的後半段對於"純"美工人員應該還是有點艱澀，不過還是有其參考價值。 P.S. 可能是我英文不好，說不定這篇文章是要讓那些有意從事程式設計的美工人員看的XD]]></description>
			<content:encoded><![CDATA[<p><a title="http://glu.ttono.us/articles/2006/03/21/rails-for-designers" href="http://glu.ttono.us/articles/2006/03/21/rails-for-designers">http://glu.ttono.us/articles/2006/03/21/rails-for-designers</a></p>
<p>這是一篇寫給為Rails環境做視覺設計、美工設計的網頁設計師看的文章。作者簡單地介紹了MVC架構，也就是Ruby on Rails最基本的架構，隨後開始介紹Layouts的概念以及如何在網頁中插入Ruby on Rails所產生的動態網頁內容。</p>
<p>說實在文章的後半段對於"純"美工人員應該還是有點艱澀，不過還是有其參考價值。</p>
<p>P.S. 可能是我英文不好，說不定這篇文章是要讓那些有意從事程式設計的美工人員看的XD</p>
]]></content:encoded>
			<wfw:commentRss>http://punk.tw/2007/03/08/rails-for-designers/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Flexible Rails</title>
		<link>http://punk.tw/2007/03/06/flexible-rails</link>
		<comments>http://punk.tw/2007/03/06/flexible-rails#comments</comments>
		<pubDate>Tue, 06 Mar 2007 03:28:15 +0000</pubDate>
		<dc:creator>deduce</dc:creator>
				<category><![CDATA[Books]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://punk.tw/2007/03/06/flexible-rails/</guid>
		<description><![CDATA[初次看到「Flexible Rails」的時候，還以為只是另外一本介紹Ruby on Rails的書，沒想到這裡的Flexible是Adobe的Flex。Flexible Rails的網站上可以看到「A PDF-only Book about using Ruby on Rails 1.1 or 1.2 and Adobe Flex 2 together to build next-generation rich Internet applications」，感覺上還滿有趣的，有機會應該搞一本來翻翻看。 網址：http://flexiblerails.com/]]></description>
			<content:encoded><![CDATA[<p>初次看到「Flexible Rails」的時候，還以為只是另外一本介紹Ruby on Rails的書，沒想到這裡的Flexible是Adobe的Flex。Flexible Rails的網站上可以看到「A PDF-only Book about using Ruby on <strong>Rails 1.1 or 1.2 and Adobe Flex 2 together to build next-generation rich Internet applications</strong>」，感覺上還滿有趣的，有機會應該搞一本來翻翻看。</p>
<p>網址：<a title="http://flexiblerails.com/" href="http://flexiblerails.com/">http://flexiblerails.com/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://punk.tw/2007/03/06/flexible-rails/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

