<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
		xmlns:xhtml="http://www.w3.org/1999/xhtml"
>

<channel>
	<title>ディスコ探偵冬景色 &#187; Ruby on Rails</title>
	<atom:link href="http://blog.udzura.jp/categories/tech/ruby/ruby-on-rails/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.udzura.jp</link>
	<description>a coding class hero is something to be...</description>
	<lastBuildDate>Thu, 29 Jul 2010 05:48:23 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://blog.udzura.jp/categories/tech/ruby/ruby-on-rails/feed/" />
		<item>
		<title>検証したいRailsプラグインのメモ</title>
		<link>http://blog.udzura.jp/2010/07/29/rails-plugins-to-test/</link>
		<comments>http://blog.udzura.jp/2010/07/29/rails-plugins-to-test/#comments</comments>
		<pubDate>Thu, 29 Jul 2010 05:46:34 +0000</pubDate>
		<dc:creator>udzura</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://blog.udzura.jp/?p=556</guid>
		<description><![CDATA[Resque
Github謹製、Redisと連携するやつ。非同期ジョブ実行をサポートする。

GitHub製Resqueを使用したRubyでのバックグラウンド処理(バッチ処理) 
defunkt&#8217;s resque at master

ジョブ監視のWebUIがついてくる。Sinatraベースで、カスタマイズも楽そう。

New Resque Web UI

また、cron風の定期実行ジョブを仕掛けるプラグインもあるようだ。

bvandenbos&#8217;s resque-scheduler at master
resque-scheduler (resqueでcronみたいなことをする)

個人的に、webブラウザから簡単にジョブを登録したり監視したりする要件があるので、割と揃ってる感じが気になる。ぜひ試してみたいです。
DataFabric

mperham&#8217;s data_fabric at master
Getting started with data_fabric


DataFabric provides flexible database connection switching for ActiveRecord.

具体的にはshardingとreplicationを実現してくれる。一昔前はacts_as_readonlyableがメジャーだったのだが、最近はメンテナンスされていないようで、2.3.x系では動作が怪しいらしい（リンク先に貴重なパッチ情報があります）。
とはいってもこちらのDataFabricも、、最終更新が今年とは言え、1月だったりする。。。それでも、2.3系で動きやすいのがどっちかといえばDataFabricに軍配が上がるだろう。gemでインストールできるようだし。
なんにしても、実際動かしてみないことにはよくわからないので、こちらも検証記事でも書こうかなと思う。
]]></description>
			<content:encoded><![CDATA[<h3>Resque</h3>
<p>Github謹製、Redisと連携するやつ。非同期ジョブ実行をサポートする。</p>
<ul>
<li><a href="http://blog.madoro.org/mn/17">GitHub製Resqueを使用したRubyでのバックグラウンド処理(バッチ処理) </a></li>
<li><a href="http://github.com/defunkt/resque">defunkt&#8217;s resque at master</a></li>
</ul>
<p>ジョブ監視のWebUIがついてくる。Sinatraベースで、カスタマイズも楽そう。</p>
<ul>
<li><a href="http://github.com/blog/543-new-resque-web-ui">New Resque Web UI</a></li>
</ul>
<p>また、cron風の定期実行ジョブを仕掛けるプラグインもあるようだ。</p>
<ul>
<li><a href="http://github.com/bvandenbos/resque-scheduler">bvandenbos&#8217;s resque-scheduler at master</a></li>
<li><a href="http://blog.madoro.org/mn/56">resque-scheduler (resqueでcronみたいなことをする)</a></li>
</ul>
<p>個人的に、webブラウザから簡単にジョブを登録したり監視したりする要件があるので、割と揃ってる感じが気になる。ぜひ試してみたいです。</p>
<h3>DataFabric</h3>
<ul>
<li><a href="http://github.com/mperham/data_fabric">mperham&#8217;s data_fabric at master</a></li>
<li><a href="http://blog.agoragames.com/2010/01/29/getting-started-with-data_fabric/">Getting started with data_fabric</a></li>
</ul>
<blockquote><p>
DataFabric provides flexible database connection switching for ActiveRecord.
</p></blockquote>
<p>具体的にはshardingとreplicationを実現してくれる。一昔前はacts_as_readonlyableがメジャーだったのだが、最近はメンテナンスされていないようで、<a href="http://dontstopmusic.no-ip.org/diary/20091203.html">2.3.x系では動作が怪しいらしい</a>（リンク先に貴重なパッチ情報があります）。</p>
<p>とはいってもこちらのDataFabricも、、最終更新が今年とは言え、1月だったりする。。。それでも、2.3系で動きやすいのがどっちかといえばDataFabricに軍配が上がるだろう。gemでインストールできるようだし。</p>
<p>なんにしても、実際動かしてみないことにはよくわからないので、こちらも検証記事でも書こうかなと思う。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.udzura.jp/2010/07/29/rails-plugins-to-test/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://blog.udzura.jp/2010/07/29/rails-plugins-to-test/" />
	</item>
		<item>
		<title>Radiant CMSのExtensionを作る – タグ追加篇</title>
		<link>http://blog.udzura.jp/2010/07/28/how-to-create-radiant-extension-tags/</link>
		<comments>http://blog.udzura.jp/2010/07/28/how-to-create-radiant-extension-tags/#comments</comments>
		<pubDate>Wed, 28 Jul 2010 05:50:07 +0000</pubDate>
		<dc:creator>udzura</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://blog.udzura.jp/?p=550</guid>
		<description><![CDATA[目次

前回（モデルと管理画面追加）
今回（タグの追加）

タグ追加の参考サイト

http://wiki.github.com/radiant/radiant/adding-custom-radius-tags
http://github.com/radiant/radiant/blob/master/app/models/standard_tags.rb

後者は、Radiant内で実際に使われているタグ定義、要はソースの一部。これが一番参考になる。チラチラ見ながら読み進めていただきたい。

タグ用のDSLの概要
vendor/extensions/cash_memo/app/models/cash_item_tags.rb というものを作ってみよう。models以下に作成することが多いようだ。

1
2
3
4
5
6
module CashItemTags
  include Radiant::Taggable
&#160;
  class TagError &#60; StandardError; end
...
end

タグを作成するためには、以下の流れでモジュール内に宣言していく。

desc メソッド: 説明を書く。%{}とかで囲って書いてる場合が多い。また、Textile記法を用いたりできる。
tag メソッド: 実際の挙動をブロック内に記述する。メソッドの引数はタグ名。また、ブロック内には「tag」という変数を投げ込める

この「tag」オブジェクトは、正体は「Radius::TagBinding」というクラスのようだ。以下のようなメソッドが使える。

Radius::TagBinding#expand: そのタグで囲ったテキストを、その場で展開する。
Radius::TagBinding#attr: そのタグで指定した、「name=&#8221;value&#8221;」みたいな属性をパースし、ハッシュの形で

1
tag.attr&#91;&#34;name&#34;&#93;

のようにアクセスできるようにする。
Radius::TagBinding#locals: そのタグと、そのタグの子要素となるタグ内で共通に使用できる変数をセット/アクセスする。tag.local自体はOpenStructを継承したオブジェクトのようで、

1
2
tag.locals.hoges = Hoge.all
tag.locals.hoges.first

のように使える。

これは、実際のコードをみて行った方がわかりやすいかな。。。
ということで拙作のタグの一部を。。「CashItem」モデルは、前回からの続きということで。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
  # 何もしない、ネームスペースの定義だけのタグは、とりあえず「tag.expand」としておこう
  tag 'cash_items' do &#124;tag&#124;
    tag.expand
  end
&#160;
  tag 'cash_items:each' do &#124;tag&#124;
    begin
      # localsにモデルをセット
     [...]]]></description>
			<content:encoded><![CDATA[<h3>目次</h3>
<ul>
<li><a href="http://blog.udzura.jp/2010/07/27/howto-create-radiant-extension/">前回（モデルと管理画面追加）</a></li>
<li>今回（タグの追加）</li>
</ul>
<h3>タグ追加の参考サイト</h3>
<ul>
<li><a href="http://wiki.github.com/radiant/radiant/adding-custom-radius-tags">http://wiki.github.com/radiant/radiant/adding-custom-radius-tags</a></li>
<li><a href="http://github.com/radiant/radiant/blob/master/app/models/standard_tags.rb">http://github.com/radiant/radiant/blob/master/app/models/standard_tags.rb</a></li>
</ul>
<p>後者は、<strong>Radiant内で実際に使われているタグ定義</strong>、要はソースの一部。これが一番参考になる。チラチラ見ながら読み進めていただきたい。</p>
<p><span id="more-550"></span></p>
<h3>タグ用のDSLの概要</h3>
<p>vendor/extensions/cash_memo/app/models/cash_item_tags.rb というものを作ってみよう。models以下に作成することが多いようだ。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">module</span> CashItemTags
  <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">Radiant::Taggable</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">class</span> TagError <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#CC00FF; font-weight:bold;">StandardError</span>; <span style="color:#9966CC; font-weight:bold;">end</span>
...
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>タグを作成するためには、以下の流れでモジュール内に宣言していく。</p>
<ul>
<li><strong><tt>desc</tt> メソッド</strong>: 説明を書く。%{}とかで囲って書いてる場合が多い。また、Textile記法を用いたりできる。</li>
<li><strong><tt>tag</tt> メソッド</strong>: 実際の挙動をブロック内に記述する。メソッドの引数はタグ名。また、ブロック内には「tag」という変数を投げ込める</li>
</ul>
<p>この「tag」オブジェクトは、正体は「<tt>Radius::TagBinding</tt>」というクラスのようだ。以下のようなメソッドが使える。</p>
<ul>
<li><strong><tt>Radius::TagBinding#expand</tt></strong>: そのタグで囲ったテキストを、その場で展開する。</li>
<li><strong><tt>Radius::TagBinding#attr</tt></strong>: そのタグで指定した、「name=&#8221;value&#8221;」みたいな属性をパースし、ハッシュの形で

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">tag.<span style="color:#9900CC;">attr</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;name&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span></pre></td></tr></table></div>

<p>のようにアクセスできるようにする。</li>
<li><strong><tt>Radius::TagBinding#locals</tt></strong>: そのタグと、<strong>そのタグの子要素となるタグ内で共通に使用できる</strong>変数をセット/アクセスする。tag.local自体はOpenStructを継承したオブジェクトのようで、

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">tag.<span style="color:#9900CC;">locals</span>.<span style="color:#9900CC;">hoges</span> = Hoge.<span style="color:#9900CC;">all</span>
tag.<span style="color:#9900CC;">locals</span>.<span style="color:#9900CC;">hoges</span>.<span style="color:#9900CC;">first</span></pre></td></tr></table></div>

<p>のように使える。</li>
</ul>
<h3>これは、実際のコードをみて行った方がわかりやすいかな。。。</h3>
<p>ということで拙作のタグの一部を。。「CashItem」モデルは、<a href="http://blog.udzura.jp/2010/07/27/howto-create-radiant-extension/">前回から</a>の続きということで。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">  <span style="color:#008000; font-style:italic;"># 何もしない、ネームスペースの定義だけのタグは、とりあえず「tag.expand」としておこう</span>
  tag <span style="color:#996600;">'cash_items'</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>tag<span style="color:#006600; font-weight:bold;">|</span>
    tag.<span style="color:#9900CC;">expand</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  tag <span style="color:#996600;">'cash_items:each'</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>tag<span style="color:#006600; font-weight:bold;">|</span>
    <span style="color:#9966CC; font-weight:bold;">begin</span>
      <span style="color:#008000; font-style:italic;"># localsにモデルをセット</span>
      <span style="color:#008000; font-style:italic;"># ここでセットすると、 cash_items:each:* のネームスペースの子要素内で参照できる、ということ</span>
      tag.<span style="color:#9900CC;">locals</span>.<span style="color:#9900CC;">items</span> = CashItem.<span style="color:#9900CC;">all</span>
      ret = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
      iterated_items = tag.<span style="color:#9900CC;">locals</span>.<span style="color:#9900CC;">items</span>
      <span style="color:#008000; font-style:italic;"># モデルの個数だけ、 r:cash_items:each タグでくくった要素を繰り返し出力する</span>
      iterated_items.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>item<span style="color:#006600; font-weight:bold;">|</span>
        tag.<span style="color:#9900CC;">locals</span>.<span style="color:#9900CC;">item</span> = item
        ret <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> tag.<span style="color:#9900CC;">expand</span> 
      <span style="color:#9966CC; font-weight:bold;">end</span>
      ret
    <span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> e
      <span style="color:#996600;">&quot;タグに誤りがあるようです：#{e.class}, #{e.message}&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># 名前だけ</span>
  tag <span style="color:#996600;">'cash_items:each:item'</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>tag<span style="color:#006600; font-weight:bold;">|</span>
    tag.<span style="color:#9900CC;">expand</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># cash_items:each 内で、各モデルの各属性に参照する</span>
  tag <span style="color:#996600;">'cash_items:each:item:name'</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>tag<span style="color:#006600; font-weight:bold;">|</span>
    tag.<span style="color:#9900CC;">locals</span>.<span style="color:#9900CC;">item</span>.<span style="color:#9900CC;">name</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#008000; font-style:italic;"># tag.attr はこんな感じで使える</span>
  tag <span style="color:#996600;">'cash_items:each:item:paid_at'</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>tag<span style="color:#006600; font-weight:bold;">|</span>
    tag.<span style="color:#9900CC;">locals</span>.<span style="color:#9900CC;">item</span>.<span style="color:#9900CC;">paid_at</span>.<span style="color:#9900CC;">strftime</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#40;</span>tag.<span style="color:#9900CC;">attr</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;format&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">||</span> <span style="color:#996600;">&quot;%Y/%m/%d(%a) %H:%M:%S&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">strip</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#008000; font-style:italic;"># 以下、各属性へのアクセッサーを宣言……</span></pre></td></tr></table></div>

<p>タグの有効化は、 vendor/extensions/cash_memo/cash_memo_extension.rb に以下の要領で追記。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">    Page.<span style="color:#9900CC;">class_eval</span> <span style="color:#006600; font-weight:bold;">&#123;</span>
      <span style="color:#9966CC; font-weight:bold;">include</span> CashItemTags
    <span style="color:#006600; font-weight:bold;">&#125;</span></pre></td></tr></table></div>

<p>で、ある程度小遣い帳の項目を登録したら、CMS管理画面側で、こんな感じで記事を登録（Textile filter）。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;r:cash_items:each<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
h3. <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;r:item:name</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
 * 使用金額：<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;r:item:amount</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
 * 購入者：<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;r:item:user_name</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
 * 購入日時：<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;r:item:paid_at</span> <span style="color: #000066;">format</span>=<span style="color: #ff0000;">&quot;%Y年%m月%d日 (%H:%M)&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
bq. <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;r:item:note</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/r:cash_items:each<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>すると。。。。無事、こういう風に展開される。</p>
<p><a href="http://blog.udzura.jp/wp-content/uploads/2010/07/ext03.png"><img src="http://blog.udzura.jp/wp-content/uploads/2010/07/ext03-300x262.png" alt="ext03" title="ext03" width="300" height="262" class="alignnone size-medium wp-image-552" /></a></p>
<p>あとは、cash_item:each タグに属性を設定し、順番なり抽出条件なりを変えたりとか、いろいろできる。とにかく standard_tag の書き方が一番参考になるので、作るつもりの人はみておいた方がいいだろう。</p>
<h3>まとめ</h3>
<ul>
<li>Radiant Extensionとは言いながら、実際の作り方、コードの感じはほぼまったく素のRailsに近い。</li>
<li>Tagの記述DSLは多少コツがいるが、十分に柔軟。</li>
<li>Radiantは、CMSというか、Railsの上にさらに覆い被さるウェブサイトフレームワークといった感じがする。Extension次第でかなり柔軟なサイト構築が出来そう。</li>
</ul>
<p>ということで、日本語化も無事されつつあるということで、もっと色々Extensionが増えるといいですね～。</p>
<p>今回作ったExtension、今後の課題は：</p>
<ul>
<li>Spec全然書いてない。</li>
<li>例外処理が非常に甘いと思う。</li>
</ul>
<h3>ソースコード is here</h3>
<ul>
<li><a href="http://github.com/udzura/radiant-cash_memo-extension">http://github.com/udzura/radiant-cash_memo-extension</a></li>
</ul>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;">git clone git:<span style="color: #000000; font-weight: bold;">//</span>github.com<span style="color: #000000; font-weight: bold;">/</span>udzura<span style="color: #000000; font-weight: bold;">/</span>radiant-cash_memo-extension.git vendor<span style="color: #000000; font-weight: bold;">/</span>extensions<span style="color: #000000; font-weight: bold;">/</span>cash_memo</pre></td></tr></table></div>

<p>で、他のRadiantにもインストールできるでしょう。動作保障はしません。。コードの参考までに、です。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.udzura.jp/2010/07/28/how-to-create-radiant-extension-tags/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://blog.udzura.jp/2010/07/28/how-to-create-radiant-extension-tags/" />
	</item>
		<item>
		<title>Radiant CMSのExtensionを作る &#8211; モデル追加湯けむり旅情篇</title>
		<link>http://blog.udzura.jp/2010/07/27/howto-create-radiant-extension/</link>
		<comments>http://blog.udzura.jp/2010/07/27/howto-create-radiant-extension/#comments</comments>
		<pubDate>Tue, 27 Jul 2010 10:45:06 +0000</pubDate>
		<dc:creator>udzura</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://blog.udzura.jp/?p=541</guid>
		<description><![CDATA[目次

今回（モデルと管理画面追加）
次回（タグの追加）

セットアップ
radiantはgemで入れる。2010年7月現在、バージョン0.9.1が入るはず。

1
sudo gem install radiant

0.9.1は比較的安定している模様なので、今回はこれをベースに開発する。
今回は「お小遣い帳をCMSに追加する」エクステンションを開発する。

で、

http://wiki.github.com/radiant/radiant/how-to-create-an-extension

のとおり（おおむね）の手順で、必要なモデル「小遣い帳の項目」を作成する。
Radiant自身と、Extensionの雛形を作る。

1
2
3
4
5
6
7
8
9
10
11
radiant -d mysql cash-book
&#160;
mysql&#62; create database cash_book_development
default charset utf8
default collate utf8_general_ci
;
#=&#62;Query OK, 1 row affected (0.02 sec)
&#160;
cd cash-book/
rake db:bootstrap
ruby script/generate extension cash-memo

ここで、Radiant自身はscaffoldジェネレータを用意してくれない……ので、別のRailsアプリを一旦作成し、そっちのscaffoldで雛形を作成、コピーして使用する。

1
2
3
4
5
6
7
8
9
10
11
12
13
cd ..
rails cash-book-tmp
cd cash-book-tmp/
ruby script/generate scaffold Admin::CashItem \
  name:string paid_at:datetime \
  user_id:integer amount:integer \
  note:text
cp -rv app/views/admin ../cash-book/vendor/extensions/cash_memo/app/views/
cp -rv app/controllers/admin ../cash-book/vendor/extensions/cash_memo/app/controllers/
cp -rv app/models/admin ../cash-book/vendor/extensions/cash_memo/app/models/
cp -rv db/migrate/* ../cash-book/vendor/extensions/cash_memo/db/migrate/
mv -v [...]]]></description>
			<content:encoded><![CDATA[<h3>目次</h3>
<ul>
<li>今回（モデルと管理画面追加）</li>
<li><a href="http://blog.udzura.jp/2010/07/28/how-to-create-radiant-extension-tags/">次回（タグの追加）</a></li>
</ul>
<h3>セットアップ</h3>
<p>radiantはgemで入れる。2010年7月現在、バージョン0.9.1が入るはず。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> gem <span style="color: #c20cb9; font-weight: bold;">install</span> radiant</pre></td></tr></table></div>

<p>0.9.1は比較的安定している模様なので、今回はこれをベースに開発する。</p>
<p>今回は<strong>「お小遣い帳をCMSに追加する」</strong>エクステンションを開発する。</p>
<p><span id="more-541"></span><br />
で、</p>
<ul>
<li><a href="http://wiki.github.com/radiant/radiant/how-to-create-an-extension">http://wiki.github.com/radiant/radiant/how-to-create-an-extension</a></li>
</ul>
<p>のとおり（おおむね）の手順で、必要なモデル「小遣い帳の項目」を作成する。</p>
<p>Radiant自身と、Extensionの雛形を作る。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;">radiant <span style="color: #660033;">-d</span> mysql cash-book
&nbsp;
mysql<span style="color: #000000; font-weight: bold;">&gt;</span> create database cash_book_development
default charset utf8
default collate utf8_general_ci
;
<span style="color: #666666; font-style: italic;">#=&gt;Query OK, 1 row affected (0.02 sec)</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">cd</span> cash-book<span style="color: #000000; font-weight: bold;">/</span>
rake db:bootstrap
ruby script<span style="color: #000000; font-weight: bold;">/</span>generate extension cash-memo</pre></td></tr></table></div>

<p>ここで、Radiant自身はscaffoldジェネレータを用意してくれない……ので、別のRailsアプリを一旦作成し、そっちのscaffoldで雛形を作成、コピーして使用する。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">cd</span> ..
rails cash-book-tmp
<span style="color: #7a0874; font-weight: bold;">cd</span> cash-book-tmp<span style="color: #000000; font-weight: bold;">/</span>
ruby script<span style="color: #000000; font-weight: bold;">/</span>generate scaffold Admin::CashItem \
  name:string paid_at:datetime \
  user_id:integer amount:integer \
  note:text
<span style="color: #c20cb9; font-weight: bold;">cp</span> <span style="color: #660033;">-rv</span> app<span style="color: #000000; font-weight: bold;">/</span>views<span style="color: #000000; font-weight: bold;">/</span>admin ..<span style="color: #000000; font-weight: bold;">/</span>cash-book<span style="color: #000000; font-weight: bold;">/</span>vendor<span style="color: #000000; font-weight: bold;">/</span>extensions<span style="color: #000000; font-weight: bold;">/</span>cash_memo<span style="color: #000000; font-weight: bold;">/</span>app<span style="color: #000000; font-weight: bold;">/</span>views<span style="color: #000000; font-weight: bold;">/</span>
<span style="color: #c20cb9; font-weight: bold;">cp</span> <span style="color: #660033;">-rv</span> app<span style="color: #000000; font-weight: bold;">/</span>controllers<span style="color: #000000; font-weight: bold;">/</span>admin ..<span style="color: #000000; font-weight: bold;">/</span>cash-book<span style="color: #000000; font-weight: bold;">/</span>vendor<span style="color: #000000; font-weight: bold;">/</span>extensions<span style="color: #000000; font-weight: bold;">/</span>cash_memo<span style="color: #000000; font-weight: bold;">/</span>app<span style="color: #000000; font-weight: bold;">/</span>controllers<span style="color: #000000; font-weight: bold;">/</span>
<span style="color: #c20cb9; font-weight: bold;">cp</span> <span style="color: #660033;">-rv</span> app<span style="color: #000000; font-weight: bold;">/</span>models<span style="color: #000000; font-weight: bold;">/</span>admin ..<span style="color: #000000; font-weight: bold;">/</span>cash-book<span style="color: #000000; font-weight: bold;">/</span>vendor<span style="color: #000000; font-weight: bold;">/</span>extensions<span style="color: #000000; font-weight: bold;">/</span>cash_memo<span style="color: #000000; font-weight: bold;">/</span>app<span style="color: #000000; font-weight: bold;">/</span>models<span style="color: #000000; font-weight: bold;">/</span>
<span style="color: #c20cb9; font-weight: bold;">cp</span> <span style="color: #660033;">-rv</span> db<span style="color: #000000; font-weight: bold;">/</span>migrate<span style="color: #000000; font-weight: bold;">/*</span> ..<span style="color: #000000; font-weight: bold;">/</span>cash-book<span style="color: #000000; font-weight: bold;">/</span>vendor<span style="color: #000000; font-weight: bold;">/</span>extensions<span style="color: #000000; font-weight: bold;">/</span>cash_memo<span style="color: #000000; font-weight: bold;">/</span>db<span style="color: #000000; font-weight: bold;">/</span>migrate<span style="color: #000000; font-weight: bold;">/</span>
<span style="color: #c20cb9; font-weight: bold;">mv</span> <span style="color: #660033;">-v</span> ..<span style="color: #000000; font-weight: bold;">/</span>cash-book<span style="color: #000000; font-weight: bold;">/</span>vendor<span style="color: #000000; font-weight: bold;">/</span>extensions<span style="color: #000000; font-weight: bold;">/</span>cash_memo<span style="color: #000000; font-weight: bold;">/</span>app<span style="color: #000000; font-weight: bold;">/</span>models<span style="color: #000000; font-weight: bold;">/</span>admin<span style="color: #000000; font-weight: bold;">/</span>cash_item.rb ..<span style="color: #000000; font-weight: bold;">/</span>cash-book<span style="color: #000000; font-weight: bold;">/</span>vendor<span style="color: #000000; font-weight: bold;">/</span>extensions<span style="color: #000000; font-weight: bold;">/</span>cash_memo<span style="color: #000000; font-weight: bold;">/</span>app<span style="color: #000000; font-weight: bold;">/</span>models<span style="color: #000000; font-weight: bold;">/</span>
<span style="color: #c20cb9; font-weight: bold;">rmdir</span> ..<span style="color: #000000; font-weight: bold;">/</span>cash-book<span style="color: #000000; font-weight: bold;">/</span>vendor<span style="color: #000000; font-weight: bold;">/</span>extensions<span style="color: #000000; font-weight: bold;">/</span>cash_memo<span style="color: #000000; font-weight: bold;">/</span>app<span style="color: #000000; font-weight: bold;">/</span>models<span style="color: #000000; font-weight: bold;">/</span>admin<span style="color: #000000; font-weight: bold;">/</span></pre></td></tr></table></div>

<h4>詰まったところ</h4>
<ul>
<li>ここで、間違えて application_controller.rb までコピーしてしまうと、Radiant自体が起動しなくなる。。。</li>
</ul>
<h3>動くように改修</h3>
<p>さて、Railsは、Scaffoldで作成された各種スクリプトについて、ネームスペース付きの場合はそのままでは使用できない。。。以下の要件で、何ヶ所か修正する。sedでもGUIテキストエディタでもいいけど、一括変換で問題ないだろう。</p>
<ul>
<li><strong>View</strong>: <code>cash_item</code>と名の付くものを<code>admin_cash_item</code>に変更する。path名、変数名など。また、form_forの中など、モデルをそのままpathに変換している箇所は、

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">admin_cash_item_path<span style="color:#006600; font-weight:bold;">&#40;</span>@admin_cash_item<span style="color:#006600; font-weight:bold;">&#41;</span></pre></td></tr></table></div>

<p>のようにしないと動作しない。</li>
<li><strong>Controller</strong>: クラス名を変更、あとはViewと同じ要領で変更。特に、createやupdateでのリダイレクト先指定などが引っかかる。また、params[:cash_item]は変更してはいけないことを留意する</li>
<li><strong>Model</strong>:

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

<p>を宣言。ふつうにRadiantで使用しているモデルに対して、has_many/belongs_toを宣言できる。</li>
<li><strong>Migration file</strong>: ファイル名を「*_create_cash_items.rb」に、クラス名を「CreateCashItems」に、テーブル名を「:cash_items」にする</li>
</ul>
<p>完成したscaffoldファイルは、<a href="http://github.com/udzura/radiant-cash_memo-extension/tree/master/app/">一応githubにアップしてある</a>。まあ、エラーが出るたびに潰していくのがいいと思う。</p>
<p>また、 vendor/extensions/cash_memo/config/routes.rb を以下のように作成。define_routesは消える予定なので使わない。警告が出ます。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#6666ff; font-weight:bold;">ActionController::Routing::Routes</span>.<span style="color:#9900CC;">draw</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>map<span style="color:#006600; font-weight:bold;">|</span>
  map.<span style="color:#9900CC;">namespace</span> <span style="color:#ff3333; font-weight:bold;">:admin</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>admin<span style="color:#006600; font-weight:bold;">|</span>
    admin.<span style="color:#9900CC;">resources</span> <span style="color:#ff3333; font-weight:bold;">:cash_items</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>また、管理画面にタブを作成するため、 vendor/extensions/cash_memo/cash_memo_extension.rb 内の activate メソッドに以下の記述を追加する。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;">  def activate
    tab <span style="color: #ff0000;">'Content'</span> <span style="color: #000000; font-weight: bold;">do</span>
      add_item <span style="color: #ff0000;">&quot;小遣い帳&quot;</span>, <span style="color: #ff0000;">&quot;/admin/cash_items&quot;</span>, :after =<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #ff0000;">&quot;Pages&quot;</span>
    end
  end</pre></td></tr></table></div>

<p>これで、</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;">rake radiant:extensions:cash_memo:migrate</pre></td></tr></table></div>

<p>テーブルが追加されたのを確認して</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;">ruby script<span style="color: #000000; font-weight: bold;">/</span>server <span style="color: #660033;">-e</span> development</pre></td></tr></table></div>

<p><a href="http://blog.udzura.jp/wp-content/uploads/2010/07/ext01.png"><img src="http://blog.udzura.jp/wp-content/uploads/2010/07/ext01-300x195.png" alt="ext01" title="ext01" width="300" height="195" class="alignnone size-medium wp-image-542" /></a></p>
<p><a href="http://blog.udzura.jp/wp-content/uploads/2010/07/ext02.png"><img src="http://blog.udzura.jp/wp-content/uploads/2010/07/ext02-300x199.png" alt="ext02" title="ext02" width="300" height="199" class="alignnone size-medium wp-image-543" /></a></p>
<p>これで、小遣い帳の項目をRadiantに記録できるようになった。</p>
<p>今回、Userはプルダウンから選べる方がいいに決まっているので、scaffoldを以下のように修正している。</p>
<h4>vendor/extensions/cash_memo/cash_memo_extension.rb に追記</h4>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">  <span style="color:#9966CC; font-weight:bold;">def</span> activate
<span style="color:#006600; font-weight:bold;">+</span>    User.<span style="color:#9900CC;">class_eval</span> <span style="color:#006600; font-weight:bold;">&#123;</span>
<span style="color:#006600; font-weight:bold;">+</span>      has_many <span style="color:#ff3333; font-weight:bold;">:cash_items</span>
<span style="color:#006600; font-weight:bold;">+</span>      <span style="color:#9966CC; font-weight:bold;">def</span> to_option
<span style="color:#006600; font-weight:bold;">+</span>        <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">name</span>, <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">id</span><span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#006600; font-weight:bold;">+</span>      <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#006600; font-weight:bold;">+</span>      
<span style="color:#006600; font-weight:bold;">+</span>      <span style="color:#9966CC; font-weight:bold;">def</span> User.<span style="color:#9900CC;">all_as_options</span>
<span style="color:#006600; font-weight:bold;">+</span>        User.<span style="color:#9900CC;">all</span>.<span style="color:#9900CC;">map</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span>:to_option<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#006600; font-weight:bold;">+</span>      <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#006600; font-weight:bold;">+</span>    <span style="color:#006600; font-weight:bold;">&#125;</span>
...</pre></td></tr></table></div>

<h4>vendor/extensions/cash_memo/app/views/admin/cash_items/(edit|new).html.erb に追記</h4>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&lt;%</span> form_for<span style="color:#006600; font-weight:bold;">&#40;</span>@admin_cash_item, <span style="color:#ff3333; font-weight:bold;">:url</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> ...<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>f<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
...
-     <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">text_field</span> <span style="color:#ff3333; font-weight:bold;">:user_id</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
+     <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#CC0066; font-weight:bold;">select</span> <span style="color:#ff3333; font-weight:bold;">:user_id</span>, User.<span style="color:#9900CC;">all_as_options</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
...</pre></td></tr></table></div>

<p>管理画面がいまいちと思うなら、Railsと同様にViewを編集して見栄えよくすることも出来る。</p>
<h3>次回</h3>
<p>モデルの追加と管理画面だけなのに、長くなってしまった、、、<a href="http://blog.udzura.jp/2010/07/28/how-to-create-radiant-extension-tags/">次回は、タグを追加し、小遣い帳の記録をページに表示するところ</a>をやります。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.udzura.jp/2010/07/27/howto-create-radiant-extension/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://blog.udzura.jp/2010/07/27/howto-create-radiant-extension/" />
	</item>
		<item>
		<title>returning は死んだ。 Object#tap を使おう</title>
		<link>http://blog.udzura.jp/2010/07/26/returning-is-dead-use-tap/</link>
		<comments>http://blog.udzura.jp/2010/07/26/returning-is-dead-use-tap/#comments</comments>
		<pubDate>Mon, 26 Jul 2010 04:19:56 +0000</pubDate>
		<dc:creator>udzura</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://blog.udzura.jp/?p=537</guid>
		<description><![CDATA[標記のとおり、死ぬみたいです。

http://github.com/rails/rails/commit/b0b9bf320409b66c6c6b680371aca590297cd4cc


crankharder
I&#8217;m curious to know what the rational is behind this?
rohit
You can now use tap.

正論過ぎて返す言葉もありませんね。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
require &#34;rubygems&#34;
require &#34;active_support&#34;
&#160;
a1 = returning &#91;&#93; do &#124;ary&#124;
  ary &#60;&#60; 'hoge'
  ary &#60;&#60; 'fuga'
end
&#160;
a2 = &#91;&#93;.tap do &#124;ary&#124;
  ary &#60;&#60; 'hoge'
  ary &#60;&#60; 'fuga'
end
&#160;
require &#34;spec&#34;
a1.should == a2
#=&#62; true

確かに、タイピング量的にも、tapの方が少なくなりますね。でも、1.8.6みたいにtapのバックポートが無い場合とか、あとreturningを使いまくってる場合は…。
そもそもtapもreturningも4行ぐらいで実装できるので、以下のような config/initializers/returning_strikes_back.rb を作成すれば互換性を確保できるんじゃあないでしょうか。あまり薦めませんが。。

1
2
3
4
5
6
7
8
9
10
module ReturningStrikesBack
  def returning&#40;value&#41;
    yield&#40;value&#41;
 [...]]]></description>
			<content:encoded><![CDATA[<p>標記のとおり、死ぬみたいです。</p>
<ul>
<li><a href="http://github.com/rails/rails/commit/b0b9bf320409b66c6c6b680371aca590297cd4cc">http://github.com/rails/rails/commit/b0b9bf320409b66c6c6b680371aca590297cd4cc</a></li>
</ul>
<blockquote>
<h5>crankharder</h5>
<p>I&#8217;m curious to know what the rational is behind this?</p>
<h5>rohit</h5>
<p>You can now use tap.
</p></blockquote>
<p>正論過ぎて返す言葉もありませんね。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">&quot;rubygems&quot;</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">&quot;active_support&quot;</span>
&nbsp;
a1 = returning <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>ary<span style="color:#006600; font-weight:bold;">|</span>
  ary <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#996600;">'hoge'</span>
  ary <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#996600;">'fuga'</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
a2 = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">tap</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>ary<span style="color:#006600; font-weight:bold;">|</span>
  ary <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#996600;">'hoge'</span>
  ary <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#996600;">'fuga'</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">&quot;spec&quot;</span>
a1.<span style="color:#9900CC;">should</span> == a2
<span style="color:#008000; font-style:italic;">#=&gt; true</span></pre></td></tr></table></div>

<p>確かに、タイピング量的にも、tapの方が少なくなりますね。でも、1.8.6みたいにtapのバックポートが無い場合とか、あとreturningを使いまくってる場合は…。</p>
<p>そもそもtapもreturningも4行ぐらいで実装できるので、以下のような<code> config/initializers/returning_strikes_back.rb </code>を作成すれば互換性を確保できるんじゃあないでしょうか。あまり薦めませんが。。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">module</span> ReturningStrikesBack
  <span style="color:#9966CC; font-weight:bold;">def</span> returning<span style="color:#006600; font-weight:bold;">&#40;</span>value<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">yield</span><span style="color:#006600; font-weight:bold;">&#40;</span>value<span style="color:#006600; font-weight:bold;">&#41;</span>
    value
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#CC00FF; font-weight:bold;">Object</span>
  <span style="color:#9966CC; font-weight:bold;">include</span> ReturningStrikesBack
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.udzura.jp/2010/07/26/returning-is-dead-use-tap/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://blog.udzura.jp/2010/07/26/returning-is-dead-use-tap/" />
	</item>
		<item>
		<title>Nginx+Passengerで、 / と /hoge に別々のRails appをデプロイする</title>
		<link>http://blog.udzura.jp/2010/07/08/deploying-apps-on-root-and-root_hoge-each/</link>
		<comments>http://blog.udzura.jp/2010/07/08/deploying-apps-on-root-and-root_hoge-each/#comments</comments>
		<pubDate>Thu, 08 Jul 2010 05:10:18 +0000</pubDate>
		<dc:creator>udzura</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[nginx]]></category>

		<guid isPermaLink="false">http://blog.udzura.jp/?p=491</guid>
		<description><![CDATA[単純に /hoge と /fuga に別々のアプリケーションをdeployするのなら、話は単純で、以下のチュートリアルのとおりなんですが。

3.2. Deploying to a sub URI

問題は passenger_base_uri に「/」を指定できないこと。
方針としては、「/」のアプリケーションと「/hoge」のアプリケーションは別々のポートで配備して、かつ「/」をデプロイした側から、リバースプロキシで「/hoge」に飛ばしてあげる、という感じです。Pound辺りと組み合わせてもいいんですが、Nginxだけで完結できます。


1
2
# base_uri対応の前準備(例)
ln -s /var/www/rails-apps/hoge/public /var/www/passenger/hoge


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
http &#123;
#....
    passenger_root /usr/lib/ruby/gems/1.8/gems/passenger-2.2.15;
    passenger_ruby /usr/bin/ruby1.8;
    server &#123; # for &#34;/&#34; setting
        listen 8080;
        server_name dev.local.udzura.jp;
  [...]]]></description>
			<content:encoded><![CDATA[<p>単純に /hoge と /fuga に別々のアプリケーションをdeployするのなら、話は単純で、以下のチュートリアルのとおりなんですが。</p>
<ul>
<li><a href="http://www.modrails.com/documentation/Users%20guide%20Nginx.html#deploying_rails_to_sub_uri">3.2. Deploying to a sub URI</a></li>
</ul>
<p>問題は <code>passenger_base_uri</code> に「/」を指定できないこと。</p>
<p>方針としては、「/」のアプリケーションと「/hoge」のアプリケーションは別々のポートで配備して、かつ「/」をデプロイした側から、リバースプロキシで「/hoge」に飛ばしてあげる、という感じです。<a href="http://www.apsis.ch/pound/">Pound</a>辺りと組み合わせてもいいんですが、Nginxだけで完結できます。</p>
<p><span id="more-491"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># base_uri対応の前準備(例)</span>
<span style="color: #c20cb9; font-weight: bold;">ln</span> <span style="color: #660033;">-s</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/</span>rails-apps<span style="color: #000000; font-weight: bold;">/</span>hoge<span style="color: #000000; font-weight: bold;">/</span>public <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/</span>passenger<span style="color: #000000; font-weight: bold;">/</span>hoge</pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;">http <span style="color: #7a0874; font-weight: bold;">&#123;</span>
<span style="color: #666666; font-style: italic;">#....</span>
    passenger_root <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>ruby<span style="color: #000000; font-weight: bold;">/</span>gems<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">1.8</span><span style="color: #000000; font-weight: bold;">/</span>gems<span style="color: #000000; font-weight: bold;">/</span>passenger-2.2.15;
    passenger_ruby <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>ruby1.8;
    server <span style="color: #7a0874; font-weight: bold;">&#123;</span> <span style="color: #666666; font-style: italic;"># for &quot;/&quot; setting</span>
        listen <span style="color: #000000;">8080</span>;
        server_name dev.local.udzura.jp;
        passenger_enabled on;
        rails_env production;
        root <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/</span>rails-apps<span style="color: #000000; font-weight: bold;">/</span>root-app<span style="color: #000000; font-weight: bold;">/</span>public;
        error_page  <span style="color: #000000;">404</span>              <span style="color: #000000; font-weight: bold;">/</span>404.html;
        error_page  <span style="color: #000000;">500</span> <span style="color: #000000;">502</span> <span style="color: #000000;">503</span> <span style="color: #000000;">504</span>  <span style="color: #000000; font-weight: bold;">/</span>error.html;
        location <span style="color: #000000; font-weight: bold;">/</span>hoge <span style="color: #7a0874; font-weight: bold;">&#123;</span>
            proxy_set_header X-Real-IP  <span style="color: #007800;">$remote_addr</span>;
            proxy_set_header Host <span style="color: #007800;">$host</span>:<span style="color: #007800;">$server_port</span>;
            <span style="color: #666666; font-style: italic;"># その他、継承したいヘッダーを追加</span>
            proxy_pass http:<span style="color: #000000; font-weight: bold;">//</span>127.0.0.1:<span style="color: #000000;">8081</span><span style="color: #000000; font-weight: bold;">/</span>hoge;
        <span style="color: #7a0874; font-weight: bold;">&#125;</span>
    <span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
    server <span style="color: #7a0874; font-weight: bold;">&#123;</span> <span style="color: #666666; font-style: italic;"># for &quot;/hoge&quot; setting</span>
        listen <span style="color: #000000;">8081</span>;
        server_name dev.local.udzura.jp;
        <span style="color: #666666; font-style: italic;"># 8081番ポートへの直接アクセスを禁止</span>
        allow 127.0.0.1;
        deny  all;
        passenger_enabled on;
        rails_env production;
        root <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/</span>passenger;
        passenger_base_uri <span style="color: #000000; font-weight: bold;">/</span>hoge;
        error_page  <span style="color: #000000;">404</span>              <span style="color: #000000; font-weight: bold;">/</span>hoge<span style="color: #000000; font-weight: bold;">/</span>404.html;
        error_page  <span style="color: #000000;">500</span> <span style="color: #000000;">502</span> <span style="color: #000000;">503</span> <span style="color: #000000;">504</span>  <span style="color: #000000; font-weight: bold;">/</span>hoge<span style="color: #000000; font-weight: bold;">/</span>500.html;
    <span style="color: #7a0874; font-weight: bold;">&#125;</span>
<span style="color: #666666; font-style: italic;">#....</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></td></tr></table></div>

<p>これで、「/」と「/hoge」とでは別のアプリケーションを参照してくれます。</p>
<h3>参考</h3>
<ul>
<li><a href="http://wiki.nginx.org/NginxHttpProxyModule">NginxHttpProxyModule</a></li>
<li><a href="http://www.modrails.com/documentation/Users%20guide%20Nginx.html#_configuring_phusion_passenger">Configuring Phusion Passenger</a></li>
<li><a href="http://wiki.nginx.org/NginxHttpCoreModule#Variables">使用可能な変数</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.udzura.jp/2010/07/08/deploying-apps-on-root-and-root_hoge-each/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://blog.udzura.jp/2010/07/08/deploying-apps-on-root-and-root_hoge-each/" />
	</item>
		<item>
		<title>Ubuntuに、passenger-install-nginx-moduleした</title>
		<link>http://blog.udzura.jp/2010/07/06/passenger-install-nginx-module-on-ubuntu/</link>
		<comments>http://blog.udzura.jp/2010/07/06/passenger-install-nginx-module-on-ubuntu/#comments</comments>
		<pubDate>Tue, 06 Jul 2010 03:26:33 +0000</pubDate>
		<dc:creator>udzura</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[nginx]]></category>

		<guid isPermaLink="false">http://blog.udzura.jp/?p=485</guid>
		<description><![CDATA[Ubuntuは、aptitudeでNginxが入るんだけど、Passenger対応しているわけではない。ビルド時に指定しないとPassengerのモジュールが組み込まれないから。
以下は、passenger対応させた上でinitスクリプトの登録までする手順。

１．ビルド＋インストール
gem経由でpassengerを入れる

1
sudo gem install passenger

passenger-install-nginx-moduleする。依存するパッケージは、apacheの時と比べて多くない。C++コンパイラぐらい。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
sudo passenger-install-nginx-module
&#160;
... # 以下が依存関係。通らないときは頑張って入れること
 * GNU C++ compiler... found at /usr/bin/g++
 * Ruby development headers... found
 * OpenSSL support for Ruby... found
 * RubyGems... found
 * Rake... found at /usr/bin/rake
 * rack... found
 * OpenSSL development headers... found
 * Zlib development headers... found
...
&#160;
Enter your choice &#40;1 or 2&#41; or press Ctrl-C to [...]]]></description>
			<content:encoded><![CDATA[<p>Ubuntuは、aptitudeでNginxが入るんだけど、Passenger対応しているわけではない。ビルド時に指定しないとPassengerのモジュールが組み込まれないから。</p>
<p>以下は、passenger対応させた上でinitスクリプトの登録までする手順。</p>
<p><span id="more-485"></span></p>
<h3>１．ビルド＋インストール</h3>
<p>gem経由でpassengerを入れる</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> gem <span style="color: #c20cb9; font-weight: bold;">install</span> passenger</pre></td></tr></table></div>

<p>passenger-install-nginx-moduleする。依存するパッケージは、apacheの時と比べて多くない。C++コンパイラぐらい。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> passenger-install-nginx-module
&nbsp;
... <span style="color: #666666; font-style: italic;"># 以下が依存関係。通らないときは頑張って入れること</span>
 <span style="color: #000000; font-weight: bold;">*</span> GNU C++ compiler... found at <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span><span style="color: #c20cb9; font-weight: bold;">g++</span>
 <span style="color: #000000; font-weight: bold;">*</span> Ruby development headers... found
 <span style="color: #000000; font-weight: bold;">*</span> OpenSSL support <span style="color: #000000; font-weight: bold;">for</span> Ruby... found
 <span style="color: #000000; font-weight: bold;">*</span> RubyGems... found
 <span style="color: #000000; font-weight: bold;">*</span> Rake... found at <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>rake
 <span style="color: #000000; font-weight: bold;">*</span> rack... found
 <span style="color: #000000; font-weight: bold;">*</span> OpenSSL development headers... found
 <span style="color: #000000; font-weight: bold;">*</span> Zlib development headers... found
...
&nbsp;
Enter your choice <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">1</span> or <span style="color: #000000;">2</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> or press Ctrl-C to abort: <span style="color: #666666; font-style: italic;">#=&gt; 1</span>
...
&nbsp;
Please specify a prefix directory <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>nginx<span style="color: #7a0874; font-weight: bold;">&#93;</span>: <span style="color: #666666; font-style: italic;">#=&gt; デフォルトのままでいいでしょう</span>
...
&nbsp;
<span style="color: #660033;">--------------------------------------------</span>
&nbsp;
Nginx with Passenger support was successfully installed.
...</pre></td></tr></table></div>

<h3>２．コンフィギュアー</h3>
<p>以下を設定する。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span>gedit<span style="color: #000000; font-weight: bold;">|</span><span style="color: #c20cb9; font-weight: bold;">vi</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>nginx<span style="color: #000000; font-weight: bold;">/</span>conf<span style="color: #000000; font-weight: bold;">/</span>nginx.conf</pre></td></tr></table></div>


<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;">...
# init script的に必要なのでpid fileを指定
pid /var/run/nginx.pid<span style="color: #666666; font-style: italic;">;</span>
&nbsp;
# passenger_root 等は最初から設定済みになっている
...
# 
server <span style="">&#123;</span>
  listen <span style="">8080</span><span style="color: #666666; font-style: italic;">;</span>
  server_name dev.local.udzura.jp<span style="color: #666666; font-style: italic;">;</span>
  # publicを指定してね
  root /var/www/dev-app/public<span style="color: #666666; font-style: italic;">;</span>
  passenger_enabled on<span style="color: #666666; font-style: italic;">;</span>
  # 必要であれば
  rails_env development<span style="color: #666666; font-style: italic;">;</span>
  #rails_env production<span style="color: #666666; font-style: italic;">;</span>
  ...
  # tips: error_page とかも指定しとくとそれっぽい
<span style="">&#125;</span></pre></div></div>

<h3>３．イニット・スクリプトの作成</h3>
<ul>
<li><a href="http://wiki.nginx.org/Nginx-init-ubuntu">Ubuntu Nginx Init Script Nginx-init-ubuntu </a></li>
</ul>
<p>以上のサイトをコピペし、 <code>/etc/init.d/nginx-passenger</code> に保存しましょう。</p>
<p>変更箇所：</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">...
<span style="color: #007800;">DAEMON</span>=<span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>nginx<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>nginx
...
<span style="color: #007800;">NGINX_CONF_FILE</span>=<span style="color: #ff0000;">&quot;/opt/nginx/conf/nginx.conf&quot;</span>
...
<span style="color: #666666; font-style: italic;"># 万一、あなたがaptで入れたgemのバージョンをアップデートして、</span>
<span style="color: #666666; font-style: italic;"># そのせいで「gemが見つからない」とか言われてRailsが起動しない場合は、</span>
<span style="color: #666666; font-style: italic;"># 以下の魔法の一行で解決する可能性があります。</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">GEM_HOME</span>=<span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>gems<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">1.8</span>
...</pre></div></div>

<p>そして、</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">chmod</span> a+x <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span>nginx-passenger
<span style="color: #c20cb9; font-weight: bold;">sudo</span> update-rc.d <span style="color: #660033;">-f</span> nginx-passenger defaults</pre></td></tr></table></div>

<p>あと、ディレクトリ構成がDebianっぽくないので、そこは好みで自分で設定しましょう。</p>
<p>以上です。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.udzura.jp/2010/07/06/passenger-install-nginx-module-on-ubuntu/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://blog.udzura.jp/2010/07/06/passenger-install-nginx-module-on-ubuntu/" />
	</item>
		<item>
		<title>[Rails]url_forで後ろに何か「#」で始まる奴をつける</title>
		<link>http://blog.udzura.jp/2010/06/18/rails-anchor-with-url_for/</link>
		<comments>http://blog.udzura.jp/2010/06/18/rails-anchor-with-url_for/#comments</comments>
		<pubDate>Fri, 18 Jun 2010 03:53:54 +0000</pubDate>
		<dc:creator>udzura</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://blog.udzura.jp/?p=454</guid>
		<description><![CDATA[http://udzura.jp/index.html#hoge みたいな後ろの「#hoge」を、url_forで追加する。
:anchor と言うオプションをつける。

http://api.rubyonrails.org/classes/ActionController/Base.html#M000455


1
2
p url_for :controller =&#62; 'udzura', :action =&#62; 'bird', :format =&#62; 'html', :anchor =&#62; 'para2'
#=&#62; '/udzura/bird.html#para2'

あの「#anchor」には「フラグメント識別子」と言う立派な名前があるのだが、まったく普及してなくて通じないのでこの名前には実質意味が無い。「アンカー」って言うとアンカータグみたいだし。。。むずかしいよ。。。
あと、:anchorに限らず他のurl_for向けオプション（:hostとか）に使われているパラメータ名も、普通のQUERY_STRINGなURLパラメータ名に使わない方がいいんじゃないかと思ったよ。何かすごい不自由な日本語になった。要するに params[:anchor] みたいなことが起こらない方がいいと思う。
]]></description>
			<content:encoded><![CDATA[<p>http://udzura.jp/index.html#hoge みたいな後ろの「#hoge」を、url_forで追加する。</p>
<p><code>:anchor</code> と言うオプションをつける。</p>
<ul>
<li><a href="http://api.rubyonrails.org/classes/ActionController/Base.html#M000455">http://api.rubyonrails.org/classes/ActionController/Base.html#M000455</a></li>
</ul>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">p</span> url_for <span style="color:#ff3333; font-weight:bold;">:controller</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'udzura'</span>, <span style="color:#ff3333; font-weight:bold;">:action</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'bird'</span>, <span style="color:#ff3333; font-weight:bold;">:format</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'html'</span>, <span style="color:#ff3333; font-weight:bold;">:anchor</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'para2'</span>
<span style="color:#008000; font-style:italic;">#=&gt; '/udzura/bird.html#para2'</span></pre></td></tr></table></div>

<p>あの「#anchor」には「フラグメント識別子」と言う立派な名前があるのだが、まったく普及してなくて通じないのでこの名前には実質意味が無い。「アンカー」って言うとアンカータグみたいだし。。。むずかしいよ。。。</p>
<p>あと、<code>:anchor</code>に限らず他のurl_for向けオプション（<code>:host</code>とか）に使われているパラメータ名も、普通のQUERY_STRINGなURLパラメータ名に使わない方がいいんじゃないかと思ったよ。何かすごい不自由な日本語になった。要するに <code>params[:anchor]</code> みたいなことが起こらない方がいいと思う。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.udzura.jp/2010/06/18/rails-anchor-with-url_for/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://blog.udzura.jp/2010/06/18/rails-anchor-with-url_for/" />
	</item>
		<item>
		<title>Ruby+heroku.comを利用して、コストと手間をなるべくかけずにサイトを作る: draft</title>
		<link>http://blog.udzura.jp/2010/05/10/how-long-way-to-go-with-smallest-cost__draft/</link>
		<comments>http://blog.udzura.jp/2010/05/10/how-long-way-to-go-with-smallest-cost__draft/#comments</comments>
		<pubDate>Mon, 10 May 2010 10:14:50 +0000</pubDate>
		<dc:creator>udzura</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://blog.udzura.jp/?p=390</guid>
		<description><![CDATA[目標
小規模サイト（中小企業のコーポレートサイトを想定）をほとんどタダみたいなコストで、なるべく手間をかけずに作れないか？
方法論
前提
Rubyを使いたい。
ホスティング
heroku.comを利用する。

Pros

No Cost(&#8221;Blossom&#8221; Planならば)
Ruby使用可能
デプロイが楽 &#8211; git
最低限のプランとはいえ、クラウドなので可用性は保証される
独自ドメインも使える（「年」額で500円とかなので、ここは買ってしまうのがいいだろう）




Cons

Rubyしか使えない
バックエンドはほとんど手をかけられない
&#8220;Blossom&#8221;で使えるDB容量はわずか「5MB」

月15ドルで20GBにアップグレードできるけど、その額を払うならVPSを使いたくなる。。


レイテンシは時々気になる程度



CMS
Ruby on Rails製の二つのアプリケーションが選択肢になる。

Radiant CMS

多機能CMS
割とカスタマイズできる
日本語化はそこそこ（それも、0.9.0限定）
CMSで使うタグの追加程度なら楽にできそう
エクステンション豊富
0.9.0はまだ安定感にかける（特にエクステンションの対応状況）




Typo

ブログ
実はテーマが豊富
まだ試していないけど、機能的に充分じゃないか？



まとめ
5MBの制約があるので、あまりに大規模だったりあまりに画像を使用するサイトは無理だと思う。でも、普通にコーポレートサイトなら出来るんじゃない？　って思ったりもする。5MBつっても素材とかは入らないわけだから。。
GAEとかの方が楽だったりするんかいなー。。。とも思うけど。RadiantやTypoはある程度ハックする前提で。
次回からは、実際に構築してみる。と言うか、その前にTypoを試したりRadiantを試した感想とかを書く。
]]></description>
			<content:encoded><![CDATA[<h3>目標</h3>
<p>小規模サイト（中小企業のコーポレートサイトを想定）をほとんどタダみたいなコストで、なるべく手間をかけずに作れないか？</p>
<h3>方法論</h3>
<h3>前提</h3>
<p>Rubyを使いたい。</p>
<h4>ホスティング</h4>
<p><a href="http://heroku.com/">heroku.com</a>を利用する。</p>
<ul>
<li>Pros
<ul>
<li>No Cost(&#8221;Blossom&#8221; Planならば)</li>
<li>Ruby使用可能</li>
<li>デプロイが楽 &#8211; git</li>
<li>最低限のプランとはいえ、クラウドなので可用性は保証される</li>
<li>独自ドメインも使える（「年」額で500円とかなので、ここは買ってしまうのがいいだろう）</li>
</ul>
</li>
</ul>
<ul>
<li>Cons
<ul>
<li>Rubyしか使えない</li>
<li>バックエンドはほとんど手をかけられない</li>
<li>&#8220;Blossom&#8221;で使えるDB容量はわずか「5MB」
<ul>
<li>月15ドルで20GBにアップグレードできるけど、その額を払うならVPSを使いたくなる。。</li>
</ul>
</li>
<li>レイテンシは時々気になる程度</li>
</ul>
</li>
</ul>
<h4>CMS</h4>
<p>Ruby on Rails製の二つのアプリケーションが選択肢になる。</p>
<ul>
<li><a href="http://radiantcms.org/">Radiant CMS</a>
<ul>
<li>多機能CMS</li>
<li>割とカスタマイズできる</li>
<li>日本語化はそこそこ（それも、0.9.0限定）</li>
<li>CMSで使うタグの追加程度なら楽にできそう</li>
<li>エクステンション豊富</li>
<li>0.9.0はまだ安定感にかける（特にエクステンションの対応状況）</li>
</ul>
</li>
</ul>
<ul>
<li><a href="http://typosphere.org/">Typo</a>
<ul>
<li>ブログ</li>
<li><a href="http://typogarden.org/">実はテーマが豊富</a></li>
<li>まだ試していないけど、機能的に充分じゃないか？</li>
</ul>
</li>
</ul>
<h3>まとめ</h3>
<p>5MBの制約があるので、あまりに大規模だったりあまりに画像を使用するサイトは無理だと思う。でも、普通にコーポレートサイトなら出来るんじゃない？　って思ったりもする。5MBつっても素材とかは入らないわけだから。。</p>
<p>GAEとかの方が楽だったりするんかいなー。。。とも思うけど。RadiantやTypoはある程度ハックする前提で。</p>
<p>次回からは、実際に構築してみる。と言うか、その前にTypoを試したりRadiantを試した感想とかを書く。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.udzura.jp/2010/05/10/how-long-way-to-go-with-smallest-cost__draft/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://blog.udzura.jp/2010/05/10/how-long-way-to-go-with-smallest-cost__draft/" />
	</item>
		<item>
		<title>emacs23始めました on Ubuntu 9.10</title>
		<link>http://blog.udzura.jp/2010/02/15/rails-settings-with-emacs23-on-ubuntu-910/</link>
		<comments>http://blog.udzura.jp/2010/02/15/rails-settings-with-emacs23-on-ubuntu-910/#comments</comments>
		<pubDate>Mon, 15 Feb 2010 11:17:41 +0000</pubDate>
		<dc:creator>udzura</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://blog.udzura.jp/?p=305</guid>
		<description><![CDATA[標記のとおり。遅まきながら，ですが。
普段，NetBeansのRailsモードで開発してたんですが，Rinariなる凄く便利なelispができたようなので，試すついでに色々設定してみました。emacsは23.1.1です。パッケージで言うとemacs23、と明示的に指定します。単純に23のほうが日本語フォントの設定がしやすいからです。。
インストールと設定
一部，emacsとRinariで快適Rails開発！ と言う素晴らしい記事を激しくコピペしつつ，以下の手順です。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
sudo aptitude install \
  emacs23 emacs23-common emacs23-bin-common emacs23-el \
  emacs-goodies-el \
  ruby1.8-elisp \
  yaml-mode
cd ~/.emacs.d
&#160;
# install rinari
git clone git://github.com/eschulte/rinari.git
cd rinari
git submodule init
git submodule update
cd -
&#160;
# install rhtml mode
git clone git://github.com/eschulte/rhtml.git

「emacs-goodies-el」と言うパッケージをいれています。テーマが色々選べるようになって快適です。emacs22、23どちらでも使えてます。配色のサンプルページ（重め）もあったりします。
手動で変えるときは M-x color-theme-select です。その日の気分で変えてもいいぐらい手軽に変更できます。
~/.emacs は、こちらもコピペをしまくった結果、以下の設定で満足しました。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
;; 言語設定とか
&#40;set-language-environment &#34;Japanese&#34;&#41;
&#40;set-terminal-coding-system 'utf-8&#41;
&#40;set-keyboard-coding-system 'utf-8&#41;
&#40;set-buffer-file-coding-system 'utf-8&#41;
&#40;setq default-buffer-file-coding-system 'utf-8&#41;
&#160;
;; フォント設定
;; ここが、emacs23じゃないとうまいこと行かなかった。。
&#40;set-face-attribute 'default nil
  :family &#34;VL Gothic&#34;
  [...]]]></description>
			<content:encoded><![CDATA[<p>標記のとおり。遅まきながら，ですが。</p>
<p>普段，NetBeansのRailsモードで開発してたんですが，<a href="http://rinari.rubyforge.org/">Rinari</a>なる凄く便利なelispができたようなので，試すついでに色々設定してみました。emacsは23.1.1です。パッケージで言うとemacs23、と明示的に指定します。単純に23のほうが日本語フォントの設定がしやすいからです。。</p>
<h3>インストールと設定</h3>
<p>一部，<a href="http://d.hatena.ne.jp/willnet/20090110/1231595231/">emacsとRinariで快適Rails開発！</a> と言う素晴らしい記事を激しくコピペしつつ，以下の手順です。</p>
<p><span id="more-305"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">aptitude</span> <span style="color: #c20cb9; font-weight: bold;">install</span> \
  emacs23 emacs23-common emacs23-bin-common emacs23-el \
  emacs-goodies-el \
  ruby1.8-elisp \
  yaml-mode
<span style="color: #7a0874; font-weight: bold;">cd</span> ~<span style="color: #000000; font-weight: bold;">/</span>.emacs.d
&nbsp;
<span style="color: #666666; font-style: italic;"># install rinari</span>
git clone git:<span style="color: #000000; font-weight: bold;">//</span>github.com<span style="color: #000000; font-weight: bold;">/</span>eschulte<span style="color: #000000; font-weight: bold;">/</span>rinari.git
<span style="color: #7a0874; font-weight: bold;">cd</span> rinari
git submodule init
git submodule update
<span style="color: #7a0874; font-weight: bold;">cd</span> -
&nbsp;
<span style="color: #666666; font-style: italic;"># install rhtml mode</span>
git clone git:<span style="color: #000000; font-weight: bold;">//</span>github.com<span style="color: #000000; font-weight: bold;">/</span>eschulte<span style="color: #000000; font-weight: bold;">/</span>rhtml.git</pre></td></tr></table></div>

<p>「emacs-goodies-el」と言うパッケージをいれています。テーマが色々選べるようになって快適です。emacs22、23どちらでも使えてます。<a href="http://code.google.com/p/gnuemacscolorthemetest/">配色のサンプルページ（重め）</a>もあったりします。</p>
<p>手動で変えるときは <code>M-x color-theme-select</code> です。その日の気分で変えてもいいぐらい手軽に変更できます。</p>
<p><code>~/.emacs</code> は、こちらもコピペをしまくった結果、以下の設定で満足しました。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
</pre></td><td class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">;; 言語設定とか</span>
<span style="color: #66cc66;">&#40;</span>set-language-environment <span style="color: #ff0000;">&quot;Japanese&quot;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>set-terminal-coding-system 'utf-<span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>set-keyboard-coding-system 'utf-<span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>set-buffer-file-coding-system 'utf-<span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setq</span> default-buffer-file-coding-system 'utf-<span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">;; フォント設定</span>
<span style="color: #808080; font-style: italic;">;; ここが、emacs23じゃないとうまいこと行かなかった。。</span>
<span style="color: #66cc66;">&#40;</span>set-face-attribute 'default <span style="color: #b1b100;">nil</span>
  <span style="color: #66cc66;">:</span><span style="color: #555;">family</span> <span style="color: #ff0000;">&quot;VL Gothic&quot;</span>
  <span style="color: #66cc66;">:</span><span style="color: #555;">height</span> <span style="color: #cc66cc;">130</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">;; 起動時のフレームサイズ</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setq</span> initial-frame-alist '<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>width <span style="color: #66cc66;">.</span> <span style="color: #cc66cc;">120</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>height <span style="color: #66cc66;">.</span> <span style="color: #cc66cc;">45</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">;; テーマ読み込み</span>
<span style="color: #66cc66;">&#40;</span>require 'color-theme<span style="color: #66cc66;">&#41;</span>
<span style="color: #808080; font-style: italic;">;; ここでテーマ設定、好きなものに変更</span>
<span style="color: #66cc66;">&#40;</span>color-theme-hober<span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">;; Interactively Do Things (highly recommended, but not strictly required)</span>
<span style="color: #66cc66;">&#40;</span>require 'ido<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>ido-mode t<span style="color: #66cc66;">&#41;</span>
<span style="color: #808080; font-style: italic;">;; Rinari</span>
<span style="color: #66cc66;">&#40;</span>add-to-<span style="color: #b1b100;">list</span> 'load-path <span style="color: #ff0000;">&quot;~/.emacs.d/rinari&quot;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>require 'rinari<span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">;; rhtml-mode</span>
<span style="color: #66cc66;">&#40;</span>add-to-<span style="color: #b1b100;">list</span> 'load-path <span style="color: #ff0000;">&quot;~/.emacs.d/rhtml&quot;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>require 'rhtml-mode<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>add-hook 'rhtml-mode-hook
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>rinari-launch<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">;; 何か最後がいいらしい</span>
<span style="color: #66cc66;">&#40;</span>set-default-coding-systems 'utf-<span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>で，なんでまたこんなエントリをぶち上げたかといいますと，近いうちに開発環境をセットアップする必要があり，その時のためのメモです（w</p>
<h3>追記＠2010-03-05</h3>
<p>最新の.emacsファイルは以下にアップすることにしました。</p>
<p>＊ <a href="http://github.com/udzura/my-emacs/blob/master/.emacs">http://github.com/udzura/my-emacs/blob/master/.emacs</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.udzura.jp/2010/02/15/rails-settings-with-emacs23-on-ubuntu-910/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://blog.udzura.jp/2010/02/15/rails-settings-with-emacs23-on-ubuntu-910/" />
	</item>
		<item>
		<title>またRails: たくさんincludeしてるときの検索</title>
		<link>http://blog.udzura.jp/2010/02/06/conditions-param-style-with-nested-include/</link>
		<comments>http://blog.udzura.jp/2010/02/06/conditions-param-style-with-nested-include/#comments</comments>
		<pubDate>Sat, 06 Feb 2010 04:44:07 +0000</pubDate>
		<dc:creator>udzura</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://blog.udzura.jp/?p=276</guid>
		<description><![CDATA[以下のようなモデルがあります（適当に考えたんですが、SNS的な何かを想定してみましょう）。

1
2
3
4
5
6
7
8
9
10
11
12
class Post &#60; ActiveRecord::Base
  belongs_to :member
end
&#160;
class Member &#60; ActiveRecord::Base
  belongs_to :job
  has_many :posts
end
&#160;
class Job &#60; ActiveRecord::Base
  has_many :members
end

「日付が2010年2月6日で、かつ書いた人の職業名が&#8221;Engineer”の人の日記一覧を抽出したい」んですけど、どうしましょう。なお、date属性はPostクラスに、job_name属性はJobクラスにくっついています。
なので、以下では全然ダメです。

1
2
3
4
5
6
Post.find&#40;:all,
  :conditions =&#62; &#123;
    :date =&#62; Date.new&#40;2010, 2, 6&#41;,
    :job_name =&#62; &#34;Engineer&#34;
  &#125;,
  :include =&#62; &#123;:member =&#62; :job&#125;&#41;

  SQL (0.2ms)   SET NAMES [...]]]></description>
			<content:encoded><![CDATA[<p>以下のようなモデルがあります（適当に考えたんですが、SNS的な何かを想定してみましょう）。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td 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>
  belongs_to <span style="color:#ff3333; font-weight:bold;">:member</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> Member <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  belongs_to <span style="color:#ff3333; font-weight:bold;">:job</span>
  has_many <span style="color:#ff3333; font-weight:bold;">:posts</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> Job <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  has_many <span style="color:#ff3333; font-weight:bold;">:members</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>「日付が2010年2月6日で、かつ書いた人の職業名が&#8221;Engineer”の人の日記一覧を抽出したい」んですけど、どうしましょう。なお、date属性はPostクラスに、job_name属性はJobクラスにくっついています。</p>
<p>なので、以下では全然ダメです。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">Post.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:all</span>,
  <span style="color:#ff3333; font-weight:bold;">: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;">:date</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#CC00FF; font-weight:bold;">Date</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">2010</span>, <span style="color:#006666;">2</span>, <span style="color:#006666;">6</span><span style="color:#006600; font-weight:bold;">&#41;</span>,
    <span style="color:#ff3333; font-weight:bold;">:job_name</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;Engineer&quot;</span>
  <span style="color:#006600; font-weight:bold;">&#125;</span>,
  <span style="color:#ff3333; font-weight:bold;">:include</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span>:member <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:job</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></td></tr></table></div>

<blockquote><p><code>  <strong style="text-decoration:underline;">SQL (0.2ms)</strong>   SET NAMES 'utf8'<br />
  <strong style="text-decoration:underline;">SQL (0.6ms)</strong>   SET SQL_AUTO_IS_NULL=0<br />
  <strong style="text-decoration:underline;">Post Load (0.0ms)</strong>   Mysql::Error: Unknown column 'posts.job_name' in 'where clause': SELECT * FROM `posts` WHERE (`posts`.`date` = '2010-01-31' AND `posts`.`job_name` = 'Engineer')<br />
</code></p></blockquote>
<p><span id="more-276"></span></p>
<p>正解は、</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">Post.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:all</span>,
  <span style="color:#ff3333; font-weight:bold;">:conditions</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span>
    <span style="color:#996600;">&quot;posts.date&quot;</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#CC00FF; font-weight:bold;">Date</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">2010</span>, <span style="color:#006666;">2</span>, <span style="color:#006666;">6</span><span style="color:#006600; font-weight:bold;">&#41;</span>,
    <span style="color:#996600;">&quot;jobs.job_name&quot;</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;Engineer&quot;</span>
  <span style="color:#006600; font-weight:bold;">&#125;</span>,
  <span style="color:#ff3333; font-weight:bold;">:include</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span>:member <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:job</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></td></tr></table></div>

<p>これで適切なフィールドを条件としたSQLが生成され、正しく抽出できます。</p>
<blockquote><p><code>  <strong style="text-decoration:underline;">Post Load Including Associations (0.4ms)</strong>   SELECT `posts`.`id` AS t0_r0, `posts`.`date` AS t0_r1, `posts`.`content` AS t0_r2, `posts`.`member_id` AS t0_r3, `posts`.`created_at` AS t0_r4, `posts`.`updated_at` AS t0_r5, `members`.`id` AS t1_r0, `members`.`name` AS t1_r1, `members`.`job_id` AS t1_r2, `members`.`created_at` AS t1_r3, `members`.`updated_at` AS t1_r4, `jobs`.`id` AS t2_r0, `jobs`.`job_name` AS t2_r1, `jobs`.`created_at` AS t2_r2, `jobs`.`updated_at` AS t2_r3 FROM `posts` LEFT OUTER JOIN `members` ON `members`.id = `posts`.member_id LEFT OUTER JOIN `jobs` ON `jobs`.id = `members`.job_id WHERE (`posts`.`date` = '2010-01-31' AND `jobs`.`job_name` = 'Engineer') </code></p></blockquote>
<p>「以下の書き方でダメなの？」　って最初思ったんですが、結論を言うとRails 2.3.5ではOKです。でも、Rails（というかactiverecord）2.2.2ではダメだった。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># RAILS_VERSION == 2.3.5</span>
Post.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:all</span>,
  <span style="color:#ff3333; font-weight:bold;">: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;">:date</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#CC00FF; font-weight:bold;">Date</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">2010</span>, <span style="color:#006666;">2</span>, <span style="color:#006666;">6</span><span style="color:#006600; font-weight:bold;">&#41;</span>,
    <span style="color:#996600;">&quot;jobs.job_name&quot;</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;Engineer&quot;</span>
  <span style="color:#006600; font-weight:bold;">&#125;</span>,
  <span style="color:#ff3333; font-weight:bold;">:include</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span>:member <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:job</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></td></tr></table></div>

<p>バージョンはなるべく上げたいものです。</p>
<h3>おまけ: script/consoleで、ActiveRecordが生成するSQLを見たい</h3>
<p>script/consoleに入って、<a href="http://www.neeraj.name/blog/articles/738-show_sql-method-to-see-sql-statements-in-script-console">show_sql method to see sql statements in script/console</a> と言うページで紹介されているコードを適切にぶち込めば見られます。下のやつ。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.udzura.jp/2010/02/06/conditions-param-style-with-nested-include/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://blog.udzura.jp/2010/02/06/conditions-param-style-with-nested-include/" />
	</item>
		<item>
		<title>ActiveRecord::Base#find(_by_id)? のお話</title>
		<link>http://blog.udzura.jp/2010/01/29/rails-diff-between-find-and-find-by-id/</link>
		<comments>http://blog.udzura.jp/2010/01/29/rails-diff-between-find-and-find-by-id/#comments</comments>
		<pubDate>Fri, 29 Jan 2010 06:38:52 +0000</pubDate>
		<dc:creator>udzura</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://blog.udzura.jp/?p=270</guid>
		<description><![CDATA[最近Ruby on Railsばかりなので、今回もRailsのお話。
何も考えずに特定のIDのレコードを見つける際は、「ActiveRecord::Base#find」という超基本メソッドを使うけれど、同時に、XXXというフィールドに対して「ActiveRecord::Base#find_by_XXX」なるメソッドも定義されているので、「ActiveRecord::Base#find_by_id」でも同じようにidに紐付けて見つけ出せる。
以上は当たり前の話だが、この同じような２つ、重要なところが違う。すなわち、そのIDに紐付くレコードが存在しない場合。
「ActiveRecord::Base#find」は例外が生じる。

1
2
3
4
5
6
7
Post.find&#40;1&#41;
#=&#62; ActiveRecord::RecordNotFound: Couldn't find Post with ID=1
#	from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:1586:in `find_one'
#	from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:1569:in `find_from_ids'
#	from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:616:in `find'
#	from (irb):6
#	from :0

「ActiveRecord::Base#find_by_id」は nil を返す。

1
2
Post.find_by_id&#40;1&#41;
#=&#62; nil

このお話はそれなりに有名なようですが、find_by_idを使えば、例えばこういうコードが書けて気分がいいですね。


1
&#40;m = ItemMaster.find_by_id&#40;params&#91;:id&#93;&#41;&#41; ? m.item_name : params&#91;:id&#93;

このワンライナーが多発するぐらいなら、item_master.rb にクラスメソッドを定義するべきだとは思うけど。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class PostController &#60; ApplicationController
  #例えばブログで
  def show
    @post = Post.find_by_id&#40;params&#91;:id&#93;&#41;
    if !@post
      flash&#91;:notice&#93; = &#34;ブログ記事が存在しません: #{params[:id]}&#34;
 [...]]]></description>
			<content:encoded><![CDATA[<p>最近Ruby on Railsばかりなので、今回もRailsのお話。</p>
<p>何も考えずに特定のIDのレコードを見つける際は、「<code>ActiveRecord::Base#find</code>」という超基本メソッドを使うけれど、同時に、XXXというフィールドに対して「<code>ActiveRecord::Base#find_by_XXX</code>」なるメソッドも定義されているので、「<code>ActiveRecord::Base#find_by_id</code>」でも同じようにidに紐付けて見つけ出せる。</p>
<p>以上は当たり前の話だが、この同じような２つ、重要なところが違う。すなわち、そのIDに紐付くレコードが<strong>存在しない</strong>場合。</p>
<p>「<code>ActiveRecord::Base#find</code>」は例外が生じる。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">Post.<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>
<span style="color:#008000; font-style:italic;">#=&gt; ActiveRecord::RecordNotFound: Couldn't find Post with ID=1</span>
<span style="color:#008000; font-style:italic;">#	from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:1586:in `find_one'</span>
<span style="color:#008000; font-style:italic;">#	from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:1569:in `find_from_ids'</span>
<span style="color:#008000; font-style:italic;">#	from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:616:in `find'</span>
<span style="color:#008000; font-style:italic;">#	from (irb):6</span>
<span style="color:#008000; font-style:italic;">#	from :0</span></pre></td></tr></table></div>

<p>「<code>ActiveRecord::Base#find_by_id</code>」は <code>nil</code> を返す。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">Post.<span style="color:#9900CC;">find_by_id</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>
<span style="color:#008000; font-style:italic;">#=&gt; nil</span></pre></td></tr></table></div>

<p><a href="http://www.google.co.jp/search?q=rails+find+find_by_id">このお話はそれなりに有名なようです</a>が、find_by_idを使えば、例えばこういうコードが書けて気分がいいですね。</p>
<p><span id="more-270"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&#40;</span>m = ItemMaster.<span style="color:#9900CC;">find_by_id</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:id</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span> ? m.<span style="color:#9900CC;">item_name</span> : params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:id</span><span style="color:#006600; font-weight:bold;">&#93;</span></pre></td></tr></table></div>

<p>このワンライナーが多発するぐらいなら、<code>item_master.rb</code> にクラスメソッドを定義するべきだとは思うけど。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> PostController <span style="color:#006600; font-weight:bold;">&lt;</span> ApplicationController
  <span style="color:#008000; font-style:italic;">#例えばブログで</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> show
    <span style="color:#0066ff; font-weight:bold;">@post</span> = Post.<span style="color:#9900CC;">find_by_id</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:id</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">if</span> !@post
      flash<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:notice</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">&quot;ブログ記事が存在しません: #{params[:id]}&quot;</span>
      redirect_to <span style="color:#ff3333; font-weight:bold;">:action</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:index</span>
      <span style="color:#0000FF; font-weight:bold;">return</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    respond_to <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>format<span style="color:#006600; font-weight:bold;">|</span>
      <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">html</span> 
      <span style="color:#008000; font-style:italic;">#...</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>こっちは幾分か実用的な気がする。IDが存在しないだけで <del>503</del> 500 になって精神衛生状態が悪くなるのを回避できます。むしろ404を返してもいい（参考: <a href="http://http://d.hatena.ne.jp/NeoCat/20080604/1212599193">http://d.hatena.ne.jp/NeoCat/20080604/1212599193</a>）。</p>
<p>この挙動は、少なくともRails 2.2.2とRails 2.3.5でこうなることを確認しています、よしなに。</p>
<p>あと、ちゃんとソース読んでないのでなんですが、「find_by_XXX」系メソッドは内部でmethod_missingを呼んでるとかいう話なので、当然ただのfindよりオーバヘッドがかかります（特にRuby 1.9系）。なんでもfind_by_idに置き換えるべきではないでしょう。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.udzura.jp/2010/01/29/rails-diff-between-find-and-find-by-id/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://blog.udzura.jp/2010/01/29/rails-diff-between-find-and-find-by-id/" />
	</item>
		<item>
		<title>Railsのdb:migrateは、最新バージョンより古くてもさかのぼる</title>
		<link>http://blog.udzura.jp/2010/01/22/rails-db-migration-tips/</link>
		<comments>http://blog.udzura.jp/2010/01/22/rails-db-migration-tips/#comments</comments>
		<pubDate>Fri, 22 Jan 2010 05:06:56 +0000</pubDate>
		<dc:creator>udzura</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://blog.udzura.jp/?p=265</guid>
		<description><![CDATA[どういうことかと言うと、
rails(1) でつくる。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
&#91;udzura@udzra.jp rails&#93;$ rails -v
Rails 2.3.5
&#91;udzura@udzra.jp rails&#93;$ rails -d mysql migtest
      create
      create  app/controllers
      create  app/helpers
      create  app/models
      create  app/views/layouts
      create [...]]]></description>
			<content:encoded><![CDATA[<p>どういうことかと言うと、</p>
<p><code>rails(1)</code> でつくる。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>udzura<span style="color: #000000; font-weight: bold;">@</span>udzra.jp rails<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ rails <span style="color: #660033;">-v</span>
Rails 2.3.5
<span style="color: #7a0874; font-weight: bold;">&#91;</span>udzura<span style="color: #000000; font-weight: bold;">@</span>udzra.jp rails<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ rails <span style="color: #660033;">-d</span> mysql migtest
      create
      create  app<span style="color: #000000; font-weight: bold;">/</span>controllers
      create  app<span style="color: #000000; font-weight: bold;">/</span>helpers
      create  app<span style="color: #000000; font-weight: bold;">/</span>models
      create  app<span style="color: #000000; font-weight: bold;">/</span>views<span style="color: #000000; font-weight: bold;">/</span>layouts
      create  config<span style="color: #000000; font-weight: bold;">/</span>environments
...
      create  log<span style="color: #000000; font-weight: bold;">/</span>server.log
      create  log<span style="color: #000000; font-weight: bold;">/</span>production.log
      create  log<span style="color: #000000; font-weight: bold;">/</span>development.log
      create  log<span style="color: #000000; font-weight: bold;">/</span>test.log</pre></td></tr></table></div>

<p>マイグレーションを作る。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>udzura<span style="color: #000000; font-weight: bold;">@</span>udzra.jp rails<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ <span style="color: #7a0874; font-weight: bold;">cd</span> migtest<span style="color: #000000; font-weight: bold;">/</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span>udzura<span style="color: #000000; font-weight: bold;">@</span>udzra.jp migtest<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ script<span style="color: #000000; font-weight: bold;">/</span>generate model User name:string
      exists  app<span style="color: #000000; font-weight: bold;">/</span>models<span style="color: #000000; font-weight: bold;">/</span>
      exists  test<span style="color: #000000; font-weight: bold;">/</span>unit<span style="color: #000000; font-weight: bold;">/</span>
      exists  test<span style="color: #000000; font-weight: bold;">/</span>fixtures<span style="color: #000000; font-weight: bold;">/</span>
      create  app<span style="color: #000000; font-weight: bold;">/</span>models<span style="color: #000000; font-weight: bold;">/</span>user.rb
      create  test<span style="color: #000000; font-weight: bold;">/</span>unit<span style="color: #000000; font-weight: bold;">/</span>user_test.rb
      create  test<span style="color: #000000; font-weight: bold;">/</span>fixtures<span style="color: #000000; font-weight: bold;">/</span>users.yml
      create  db<span style="color: #000000; font-weight: bold;">/</span>migrate
      create  db<span style="color: #000000; font-weight: bold;">/</span>migrate<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">20100122041512</span>_create_users.rb
<span style="color: #7a0874; font-weight: bold;">&#91;</span>udzura<span style="color: #000000; font-weight: bold;">@</span>udzra.jp migtest<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ script<span style="color: #000000; font-weight: bold;">/</span>generate model Blog user_id:integer entry:text
      exists  app<span style="color: #000000; font-weight: bold;">/</span>models<span style="color: #000000; font-weight: bold;">/</span>
      exists  test<span style="color: #000000; font-weight: bold;">/</span>unit<span style="color: #000000; font-weight: bold;">/</span>
      exists  test<span style="color: #000000; font-weight: bold;">/</span>fixtures<span style="color: #000000; font-weight: bold;">/</span>
      create  app<span style="color: #000000; font-weight: bold;">/</span>models<span style="color: #000000; font-weight: bold;">/</span>blog.rb
      create  test<span style="color: #000000; font-weight: bold;">/</span>unit<span style="color: #000000; font-weight: bold;">/</span>blog_test.rb
      create  test<span style="color: #000000; font-weight: bold;">/</span>fixtures<span style="color: #000000; font-weight: bold;">/</span>blogs.yml
      exists  db<span style="color: #000000; font-weight: bold;">/</span>migrate
      create  db<span style="color: #000000; font-weight: bold;">/</span>migrate<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">20100122041555</span>_create_blogs.rb</pre></td></tr></table></div>

<p>2099年とかすっごい未来にリネームする。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>udzura<span style="color: #000000; font-weight: bold;">@</span>udzra.jp migtest<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ <span style="color: #c20cb9; font-weight: bold;">ls</span> <span style="color: #660033;">-l</span> db<span style="color: #000000; font-weight: bold;">/</span>migrate<span style="color: #000000; font-weight: bold;">/</span>
合計 <span style="color: #000000;">8</span>
<span style="color: #660033;">-rw-rw-r--</span>  <span style="color: #000000;">1</span> udzura udzura <span style="color: #000000;">193</span>  <span style="color: #000000;">1</span>月 <span style="color: #000000;">22</span> <span style="color: #000000;">13</span>:<span style="color: #000000;">15</span> <span style="color: #000000;">20100122041512</span>_create_users.rb
<span style="color: #660033;">-rw-rw-r--</span>  <span style="color: #000000;">1</span> udzura udzura <span style="color: #000000;">217</span>  <span style="color: #000000;">1</span>月 <span style="color: #000000;">22</span> <span style="color: #000000;">13</span>:<span style="color: #000000;">15</span> <span style="color: #000000;">20100122041555</span>_create_blogs.rb
<span style="color: #7a0874; font-weight: bold;">&#91;</span>udzura<span style="color: #000000; font-weight: bold;">@</span>udzra.jp migtest<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ <span style="color: #c20cb9; font-weight: bold;">mv</span> db<span style="color: #000000; font-weight: bold;">/</span>migrate<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">20100122041555</span>_create_blogs.rb \
  db<span style="color: #000000; font-weight: bold;">/</span>migrate<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">20990122041555</span>_create_blogs.rb</pre></td></tr></table></div>

<p>マイグレートする。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>udzura<span style="color: #000000; font-weight: bold;">@</span>udzra.jp migtest<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ rake db:create
<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>web-devel<span style="color: #000000; font-weight: bold;">/</span>rails<span style="color: #000000; font-weight: bold;">/</span>migtest<span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span>udzura<span style="color: #000000; font-weight: bold;">@</span>udzra.jp migtest<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ rake db:migrate
<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>web-devel<span style="color: #000000; font-weight: bold;">/</span>rails<span style="color: #000000; font-weight: bold;">/</span>migtest<span style="color: #7a0874; font-weight: bold;">&#41;</span>
==  CreateUsers: migrating ====================================================
<span style="color: #660033;">--</span> create_table<span style="color: #7a0874; font-weight: bold;">&#40;</span>:<span style="color: #c20cb9; font-weight: bold;">users</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
   -<span style="color: #000000; font-weight: bold;">&gt;</span> 0.0038s
==  CreateUsers: migrated <span style="color: #7a0874; font-weight: bold;">&#40;</span>0.0041s<span style="color: #7a0874; font-weight: bold;">&#41;</span> ===========================================
&nbsp;
==  CreateBlogs: migrating ====================================================
<span style="color: #660033;">--</span> create_table<span style="color: #7a0874; font-weight: bold;">&#40;</span>:blogs<span style="color: #7a0874; font-weight: bold;">&#41;</span>
   -<span style="color: #000000; font-weight: bold;">&gt;</span> 0.0032s
==  CreateBlogs: migrated <span style="color: #7a0874; font-weight: bold;">&#40;</span>0.0035s<span style="color: #7a0874; font-weight: bold;">&#41;</span> ===========================================
&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#91;</span>udzura<span style="color: #000000; font-weight: bold;">@</span>udzra.jp migtest<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ mysql <span style="color: #660033;">-uroot</span> <span style="color: #660033;">-pxxxx</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;SHOW TABLES&quot;</span> migtest_development
+-------------------------------+
<span style="color: #000000; font-weight: bold;">|</span> Tables_in_migtest_development <span style="color: #000000; font-weight: bold;">|</span>
+-------------------------------+
<span style="color: #000000; font-weight: bold;">|</span> blogs                         <span style="color: #000000; font-weight: bold;">|</span>
<span style="color: #000000; font-weight: bold;">|</span> schema_migrations             <span style="color: #000000; font-weight: bold;">|</span>
<span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">users</span>                         <span style="color: #000000; font-weight: bold;">|</span>
+-------------------------------+
<span style="color: #7a0874; font-weight: bold;">&#91;</span>udzura<span style="color: #000000; font-weight: bold;">@</span>udzra.jp migtest<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ rake db:version
<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>web-devel<span style="color: #000000; font-weight: bold;">/</span>rails<span style="color: #000000; font-weight: bold;">/</span>migtest<span style="color: #7a0874; font-weight: bold;">&#41;</span>
Current version: <span style="color: #000000;">20990122041555</span></pre></td></tr></table></div>

<p>マイグレーションを加える。当然、さっき作った二つのマイグレーションの間に入ります。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>udzura<span style="color: #000000; font-weight: bold;">@</span>udzra.jp migtest<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ script<span style="color: #000000; font-weight: bold;">/</span>generate model UserFriend user_id:integer friend_user_id:integer
      exists  app<span style="color: #000000; font-weight: bold;">/</span>models<span style="color: #000000; font-weight: bold;">/</span>
      exists  test<span style="color: #000000; font-weight: bold;">/</span>unit<span style="color: #000000; font-weight: bold;">/</span>
      exists  test<span style="color: #000000; font-weight: bold;">/</span>fixtures<span style="color: #000000; font-weight: bold;">/</span>
      create  app<span style="color: #000000; font-weight: bold;">/</span>models<span style="color: #000000; font-weight: bold;">/</span>user_friend.rb
      create  test<span style="color: #000000; font-weight: bold;">/</span>unit<span style="color: #000000; font-weight: bold;">/</span>user_friend_test.rb
      create  test<span style="color: #000000; font-weight: bold;">/</span>fixtures<span style="color: #000000; font-weight: bold;">/</span>user_friends.yml
      exists  db<span style="color: #000000; font-weight: bold;">/</span>migrate
      create  db<span style="color: #000000; font-weight: bold;">/</span>migrate<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">20100122041954</span>_create_user_friends.rb
<span style="color: #7a0874; font-weight: bold;">&#91;</span>udzura<span style="color: #000000; font-weight: bold;">@</span>udzra.jp migtest<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ <span style="color: #c20cb9; font-weight: bold;">ls</span> <span style="color: #660033;">-l</span> db<span style="color: #000000; font-weight: bold;">/</span>migrate<span style="color: #000000; font-weight: bold;">/</span>
合計 <span style="color: #000000;">12</span>
<span style="color: #660033;">-rw-rw-r--</span>  <span style="color: #000000;">1</span> udzura udzura <span style="color: #000000;">193</span>  <span style="color: #000000;">1</span>月 <span style="color: #000000;">22</span> <span style="color: #000000;">13</span>:<span style="color: #000000;">15</span> <span style="color: #000000;">20100122041512</span>_create_users.rb
<span style="color: #660033;">-rw-rw-r--</span>  <span style="color: #000000;">1</span> udzura udzura <span style="color: #000000;">249</span>  <span style="color: #000000;">1</span>月 <span style="color: #000000;">22</span> <span style="color: #000000;">13</span>:<span style="color: #000000;">19</span> <span style="color: #000000;">20100122041954</span>_create_user_friends.rb
<span style="color: #660033;">-rw-rw-r--</span>  <span style="color: #000000;">1</span> udzura udzura <span style="color: #000000;">217</span>  <span style="color: #000000;">1</span>月 <span style="color: #000000;">22</span> <span style="color: #000000;">13</span>:<span style="color: #000000;">15</span> <span style="color: #000000;">20990122041555</span>_create_blogs.rb</pre></td></tr></table></div>

<p>こういう場合でもさかのぼってマイグレーションを実行してくれます。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>udzura<span style="color: #000000; font-weight: bold;">@</span>udzra.jp migtest<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ rake db:migrate
<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>web-devel<span style="color: #000000; font-weight: bold;">/</span>rails<span style="color: #000000; font-weight: bold;">/</span>migtest<span style="color: #7a0874; font-weight: bold;">&#41;</span>
==  CreateUserFriends: migrating ==============================================
<span style="color: #660033;">--</span> create_table<span style="color: #7a0874; font-weight: bold;">&#40;</span>:user_friends<span style="color: #7a0874; font-weight: bold;">&#41;</span>
   -<span style="color: #000000; font-weight: bold;">&gt;</span> 0.0067s
==  CreateUserFriends: migrated <span style="color: #7a0874; font-weight: bold;">&#40;</span>0.0071s<span style="color: #7a0874; font-weight: bold;">&#41;</span> =====================================
&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#91;</span>udzura<span style="color: #000000; font-weight: bold;">@</span>udzra.jp migtest<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ mysql <span style="color: #660033;">-uroot</span> <span style="color: #660033;">-pxxxx</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;SHOW TABLES&quot;</span> migtest_development
+-------------------------------+
<span style="color: #000000; font-weight: bold;">|</span> Tables_in_migtest_development <span style="color: #000000; font-weight: bold;">|</span>
+-------------------------------+
<span style="color: #000000; font-weight: bold;">|</span> blogs                         <span style="color: #000000; font-weight: bold;">|</span>
<span style="color: #000000; font-weight: bold;">|</span> schema_migrations             <span style="color: #000000; font-weight: bold;">|</span>
<span style="color: #000000; font-weight: bold;">|</span> user_friends                  <span style="color: #000000; font-weight: bold;">|</span>
<span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">users</span>                         <span style="color: #000000; font-weight: bold;">|</span>
+-------------------------------+</pre></td></tr></table></div>

<h3>何が嬉しいの？</h3>
<p>例えば、実表のマイグレーションを通常の作成時の日次で、ビュー表を作成するマイグレーションを「2099年」の日次で、という風にしておくと、「必ずすべての実表が作られてからビュー表が定義される」ことが保障され、なおかつ、ビュー表の定義を変更した際、作り直しが <code>rake(1)</code> で容易にできます。</p>
<p>ポイントは <code>rake db:migrate:down VERSION=2099XXXXXXXXXX</code> 。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>udzura<span style="color: #000000; font-weight: bold;">@</span>udzra.jp migtest<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ rake db:migrate:down <span style="color: #007800;">VERSION</span>=<span style="color: #000000;">20990122041555</span>
<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>web-devel<span style="color: #000000; font-weight: bold;">/</span>rails<span style="color: #000000; font-weight: bold;">/</span>migtest<span style="color: #7a0874; font-weight: bold;">&#41;</span>
==  CreateViewBlogs: reverting ====================================================
<span style="color: #660033;">--</span> drop_table<span style="color: #7a0874; font-weight: bold;">&#40;</span>:view_blogs<span style="color: #7a0874; font-weight: bold;">&#41;</span>
   -<span style="color: #000000; font-weight: bold;">&gt;</span> 0.0277s
==  CreateViewBlogs: reverted <span style="color: #7a0874; font-weight: bold;">&#40;</span>0.0280s<span style="color: #7a0874; font-weight: bold;">&#41;</span> ===========================================
&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#91;</span>udzura<span style="color: #000000; font-weight: bold;">@</span>udzra.jp migtest<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ rake db:migrate
<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>web-devel<span style="color: #000000; font-weight: bold;">/</span>rails<span style="color: #000000; font-weight: bold;">/</span>migtest<span style="color: #7a0874; font-weight: bold;">&#41;</span>
==  CreateViewBlogs: migrating ====================================================
<span style="color: #660033;">--</span> create_table<span style="color: #7a0874; font-weight: bold;">&#40;</span>:view_blogs<span style="color: #7a0874; font-weight: bold;">&#41;</span>
   -<span style="color: #000000; font-weight: bold;">&gt;</span> 0.0472s
==  CreateViewBlogs: migrated <span style="color: #7a0874; font-weight: bold;">&#40;</span>0.0475s<span style="color: #7a0874; font-weight: bold;">&#41;</span> ===========================================
&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#91;</span>udzura<span style="color: #000000; font-weight: bold;">@</span>udzra.jp migtest<span style="color: #7a0874; font-weight: bold;">&#93;</span>$</pre></td></tr></table></div>

<p>RailsでCreate Viewしたい際は、普通に <code>execute</code> しないといけません。なので、そもそも「RailsでView表使うとかあほか」って方もいると思う。引き出しの一つとして。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.udzura.jp/2010/01/22/rails-db-migration-tips/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://blog.udzura.jp/2010/01/22/rails-db-migration-tips/" />
	</item>
		<item>
		<title>Gitorious入れたメモ</title>
		<link>http://blog.udzura.jp/2010/01/06/gitorious-installation-memo/</link>
		<comments>http://blog.udzura.jp/2010/01/06/gitorious-installation-memo/#comments</comments>
		<pubDate>Wed, 06 Jan 2010 13:26:16 +0000</pubDate>
		<dc:creator>udzura</dc:creator>
				<category><![CDATA[Git]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://blog.udzura.jp/?p=232</guid>
		<description><![CDATA[日ごろgithubさんにお世話になっておりますが、Gitoriousというサービスは自分のコードを公開し、個人でインストールできるようなので入れてみたメモ。
概要
ホストするOSはUbuntu 9.10。
手順は、9.04の場合のものが公式Wikiにあったりする（http://gitorious.org/gitorious/pages/UbuntuInstallation）のでそれを大幅に参照した。基本的にコレの通りで問題ないのだが、Gitorious自体のバージョンが上がったこともあって微妙に違う点もある。
以下、日本語Remix CDを入れたてのまっさらな状態からの構築を想定。

aptで入れたもの

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
sudo aptitude install build-essential\
apache2 apache2-dev\
mysql-server mysql-client libmysqlclient15-dev\
git-core git-svn git-doc git-cvs\
apg\
geoip-bin libgeoip1 libgeoip-dev\
sqlite3 libsqlite3-dev\
imagemagick libmagickcore2 libmagickwand2 libmagickcore-dev libmagickwand-dev\
libpcre3 libpcre3-dev\
zlib1g zlib1g-dev\
libyaml-dev\
sendmail\
zip unzip\
memcached\
openssh-server

ログを見る限りこれらを入れていた。Synapticで入れてしまったものもあるかも。。デスクトップ版はデフォルトだとsshdも入ってないのよね。。
ソースインストール
onigurumaはソースインストールしないと、rubyバインディングをコンパイルできない。libonig2はヘッダが入らん。
（http://www.geocities.jp/kosako3/oniguruma/）
あと、sphinxという検索エンジンを使用するのでそれも入れる。
（http://www.sphinxsearch.com/downloads.html）
ImageMagickは、aptでMagick-configやヘッダファイルが入ってくれるのでソースは要らない。
あと、他の記事ではActiveMQを入れるとかいう手順もありますけど、今のバージョンでは使ってないようです。Rubyのstompserverとかいうものにメッセージングを任せてる、のかな。
Ruby周りとgemで入れたもの
rubyは、Ruby Enterprise Edition（http://www.rubyenterpriseedition.com/download.html）。ちょうど、1.8.7互換版が出ている。Ubuntuだとdebもあるので圧倒的に楽。
で、rubyバイナリのインストール先が /usr/local/bin になり、gem、gemで入れた実行ファイル（rake、stompserver）等もそこに行くので、実行ファイルを決め打ちにしている幾つかのスクリプトを修正する必要がある。なんかよく分からんけどコケた際はまずその辺を疑ってみよう。
gemは最終的に以下の如し。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
*** LOCAL GEMS ***
&#160;
actionmailer &#40;2.3.5&#41;
actionpack &#40;2.3.5&#41;
activerecord &#40;2.3.5&#41;
activeresource &#40;2.3.5&#41;
activesupport &#40;2.3.5&#41;
BlueCloth &#40;1.0.1&#41;
chronic &#40;0.2.3&#41;
daemons &#40;1.0.10&#41;
diff-lcs &#40;1.1.2&#41;
echoe &#40;4.1&#41;
eventmachine &#40;0.12.10&#41;
fastthread &#40;1.0.7&#41;
gemcutter &#40;0.2.1&#41;
geoip &#40;0.8.6&#41;
hoe &#40;2.4.0&#41;
json &#40;1.2.0&#41;
json_pure &#40;1.2.0&#41;
mime-types &#40;1.16&#41;
mysql &#40;2.8.1&#41;
oniguruma &#40;1.1.0&#41;
passenger &#40;2.2.5&#41;
pg &#40;0.8.0&#41;
plist &#40;3.0.0&#41;
rack &#40;1.0.1&#41;
rails &#40;2.3.5&#41;
rake &#40;0.8.7&#41;
rdiscount [...]]]></description>
			<content:encoded><![CDATA[<p>日ごろgithubさんにお世話になっておりますが、<a href="http://gitorious.org/">Gitorious</a>というサービスは<a href="http://gitorious.org/gitorious/">自分のコードを公開</a>し、個人でインストールできるようなので入れてみたメモ。</p>
<h3>概要</h3>
<p>ホストするOSはUbuntu 9.10。</p>
<p>手順は、9.04の場合のものが公式Wikiにあったりする（<a href="http://gitorious.org/gitorious/pages/UbuntuInstallation">http://gitorious.org/gitorious/pages/UbuntuInstallation</a>）のでそれを大幅に参照した。基本的にコレの通りで問題ないのだが、Gitorious自体のバージョンが上がったこともあって微妙に違う点もある。</p>
<p>以下、日本語Remix CDを入れたてのまっさらな状態からの構築を想定。</p>
<p><span id="more-232"></span></p>
<h3>aptで入れたもの</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">aptitude</span> <span style="color: #c20cb9; font-weight: bold;">install</span> build-essential\
apache2 apache2-dev\
mysql-server mysql-client libmysqlclient15-dev\
git-core git-svn git-doc git-cvs\
apg\
geoip-bin libgeoip1 libgeoip-dev\
sqlite3 libsqlite3-dev\
imagemagick libmagickcore2 libmagickwand2 libmagickcore-dev libmagickwand-dev\
libpcre3 libpcre3-dev\
zlib1g zlib1g-dev\
libyaml-dev\
<span style="color: #c20cb9; font-weight: bold;">sendmail</span>\
<span style="color: #c20cb9; font-weight: bold;">zip</span> <span style="color: #c20cb9; font-weight: bold;">unzip</span>\
memcached\
openssh-server</pre></td></tr></table></div>

<p>ログを見る限りこれらを入れていた。Synapticで入れてしまったものもあるかも。。デスクトップ版はデフォルトだとsshdも入ってないのよね。。</p>
<h3>ソースインストール</h3>
<p>onigurumaはソースインストールしないと、rubyバインディングをコンパイルできない。libonig2はヘッダが入らん。<br />
（<a href="http://www.geocities.jp/kosako3/oniguruma/">http://www.geocities.jp/kosako3/oniguruma/</a>）</p>
<p>あと、sphinxという検索エンジンを使用するのでそれも入れる。<br />
（<a href="http://www.sphinxsearch.com/downloads.html">http://www.sphinxsearch.com/downloads.html</a>）</p>
<p>ImageMagickは、aptでMagick-configやヘッダファイルが入ってくれるのでソースは要らない。</p>
<p>あと、他の記事ではActiveMQを入れるとかいう手順もありますけど、今のバージョンでは使ってないようです。Rubyのstompserverとかいうものにメッセージングを任せてる、のかな。</p>
<h3>Ruby周りとgemで入れたもの</h3>
<p>rubyは、Ruby Enterprise Edition（<a href="http://www.rubyenterpriseedition.com/download.html">http://www.rubyenterpriseedition.com/download.html</a>）。ちょうど、1.8.7互換版が出ている。Ubuntuだとdebもあるので圧倒的に楽。</p>
<p>で、rubyバイナリのインストール先が /usr/local/bin になり、gem、gemで入れた実行ファイル（rake、stompserver）等もそこに行くので、実行ファイルを決め打ちにしている幾つかのスクリプトを修正する必要がある。なんかよく分からんけどコケた際はまずその辺を疑ってみよう。</p>
<p>gemは最終的に以下の如し。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">***</span> LOCAL GEMS <span style="color: #000000; font-weight: bold;">***</span>
&nbsp;
actionmailer <span style="color: #7a0874; font-weight: bold;">&#40;</span>2.3.5<span style="color: #7a0874; font-weight: bold;">&#41;</span>
actionpack <span style="color: #7a0874; font-weight: bold;">&#40;</span>2.3.5<span style="color: #7a0874; font-weight: bold;">&#41;</span>
activerecord <span style="color: #7a0874; font-weight: bold;">&#40;</span>2.3.5<span style="color: #7a0874; font-weight: bold;">&#41;</span>
activeresource <span style="color: #7a0874; font-weight: bold;">&#40;</span>2.3.5<span style="color: #7a0874; font-weight: bold;">&#41;</span>
activesupport <span style="color: #7a0874; font-weight: bold;">&#40;</span>2.3.5<span style="color: #7a0874; font-weight: bold;">&#41;</span>
BlueCloth <span style="color: #7a0874; font-weight: bold;">&#40;</span>1.0.1<span style="color: #7a0874; font-weight: bold;">&#41;</span>
chronic <span style="color: #7a0874; font-weight: bold;">&#40;</span>0.2.3<span style="color: #7a0874; font-weight: bold;">&#41;</span>
daemons <span style="color: #7a0874; font-weight: bold;">&#40;</span>1.0.10<span style="color: #7a0874; font-weight: bold;">&#41;</span>
diff-lcs <span style="color: #7a0874; font-weight: bold;">&#40;</span>1.1.2<span style="color: #7a0874; font-weight: bold;">&#41;</span>
echoe <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">4.1</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
eventmachine <span style="color: #7a0874; font-weight: bold;">&#40;</span>0.12.10<span style="color: #7a0874; font-weight: bold;">&#41;</span>
fastthread <span style="color: #7a0874; font-weight: bold;">&#40;</span>1.0.7<span style="color: #7a0874; font-weight: bold;">&#41;</span>
gemcutter <span style="color: #7a0874; font-weight: bold;">&#40;</span>0.2.1<span style="color: #7a0874; font-weight: bold;">&#41;</span>
geoip <span style="color: #7a0874; font-weight: bold;">&#40;</span>0.8.6<span style="color: #7a0874; font-weight: bold;">&#41;</span>
hoe <span style="color: #7a0874; font-weight: bold;">&#40;</span>2.4.0<span style="color: #7a0874; font-weight: bold;">&#41;</span>
json <span style="color: #7a0874; font-weight: bold;">&#40;</span>1.2.0<span style="color: #7a0874; font-weight: bold;">&#41;</span>
json_pure <span style="color: #7a0874; font-weight: bold;">&#40;</span>1.2.0<span style="color: #7a0874; font-weight: bold;">&#41;</span>
mime-types <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">1.16</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
mysql <span style="color: #7a0874; font-weight: bold;">&#40;</span>2.8.1<span style="color: #7a0874; font-weight: bold;">&#41;</span>
oniguruma <span style="color: #7a0874; font-weight: bold;">&#40;</span>1.1.0<span style="color: #7a0874; font-weight: bold;">&#41;</span>
passenger <span style="color: #7a0874; font-weight: bold;">&#40;</span>2.2.5<span style="color: #7a0874; font-weight: bold;">&#41;</span>
pg <span style="color: #7a0874; font-weight: bold;">&#40;</span>0.8.0<span style="color: #7a0874; font-weight: bold;">&#41;</span>
plist <span style="color: #7a0874; font-weight: bold;">&#40;</span>3.0.0<span style="color: #7a0874; font-weight: bold;">&#41;</span>
rack <span style="color: #7a0874; font-weight: bold;">&#40;</span>1.0.1<span style="color: #7a0874; font-weight: bold;">&#41;</span>
rails <span style="color: #7a0874; font-weight: bold;">&#40;</span>2.3.5<span style="color: #7a0874; font-weight: bold;">&#41;</span>
rake <span style="color: #7a0874; font-weight: bold;">&#40;</span>0.8.7<span style="color: #7a0874; font-weight: bold;">&#41;</span>
rdiscount <span style="color: #7a0874; font-weight: bold;">&#40;</span>1.3.1.1<span style="color: #7a0874; font-weight: bold;">&#41;</span>
RedCloth <span style="color: #7a0874; font-weight: bold;">&#40;</span>4.2.2<span style="color: #7a0874; font-weight: bold;">&#41;</span>
rmagick <span style="color: #7a0874; font-weight: bold;">&#40;</span>2.12.2<span style="color: #7a0874; font-weight: bold;">&#41;</span>
rspec <span style="color: #7a0874; font-weight: bold;">&#40;</span>1.2.9<span style="color: #7a0874; font-weight: bold;">&#41;</span>
rspec-rails <span style="color: #7a0874; font-weight: bold;">&#40;</span>1.2.9<span style="color: #7a0874; font-weight: bold;">&#41;</span>
ruby-hmac <span style="color: #7a0874; font-weight: bold;">&#40;</span>0.3.2<span style="color: #7a0874; font-weight: bold;">&#41;</span>
ruby-openid <span style="color: #7a0874; font-weight: bold;">&#40;</span>2.1.7<span style="color: #7a0874; font-weight: bold;">&#41;</span>
ruby-yadis <span style="color: #7a0874; font-weight: bold;">&#40;</span>0.3.4<span style="color: #7a0874; font-weight: bold;">&#41;</span>
rubyforge <span style="color: #7a0874; font-weight: bold;">&#40;</span>2.0.3<span style="color: #7a0874; font-weight: bold;">&#41;</span>
sqlite3-ruby <span style="color: #7a0874; font-weight: bold;">&#40;</span>1.2.5<span style="color: #7a0874; font-weight: bold;">&#41;</span>
stomp <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">1.1</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
stompserver <span style="color: #7a0874; font-weight: bold;">&#40;</span>0.9.9<span style="color: #7a0874; font-weight: bold;">&#41;</span>
textpow <span style="color: #7a0874; font-weight: bold;">&#40;</span>0.10.1<span style="color: #7a0874; font-weight: bold;">&#41;</span>
ultrasphinx <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">1.11</span><span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></td></tr></table></div>

<h3>service関連の設定</h3>
<p>「<a href="http://gitorious.org/gitorious/pages/UbuntuInstallation">UbuntuInstallation</a>」の通り。git-daemon、git-ultrasphinxの起動スクリプトのrubyの場所をなどを変更。</p>
<p>stompは、Wikiのスクリプトではまったく動かなかったので、やっつけで以下のようなものを作成。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/sh</span>
<span style="color: #666666; font-style: italic;"># Start/stop the stompserver</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;">### BEGIN INIT INFO</span>
<span style="color: #666666; font-style: italic;"># Provides: stomp</span>
<span style="color: #666666; font-style: italic;"># Required-Start: $local_fs $remote_fs $network $syslog</span>
<span style="color: #666666; font-style: italic;"># Required-Stop:</span>
<span style="color: #666666; font-style: italic;"># Default-Start: 2 3 4 5</span>
<span style="color: #666666; font-style: italic;"># Default-Stop: 1</span>
<span style="color: #666666; font-style: italic;"># Short-Description: Stomp</span>
<span style="color: #666666; font-style: italic;"># Description: Stomp</span>
<span style="color: #666666; font-style: italic;">### END INIT INFO</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">test</span> <span style="color: #660033;">-f</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>stompserver <span style="color: #000000; font-weight: bold;">||</span> <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">0</span>
&nbsp;
. <span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>lsb<span style="color: #000000; font-weight: bold;">/</span>init-functions
&nbsp;
<span style="color: #000000; font-weight: bold;">case</span> $<span style="color: #000000;">1</span> <span style="color: #000000; font-weight: bold;">in</span>
start<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #666666; font-style: italic;"># log_daemon_msg “Starting stompserver” “stomp”</span>
&nbsp;
     <span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/</span>gitorious <span style="color: #000000; font-weight: bold;">&amp;</span>amp;<span style="color: #000000; font-weight: bold;">&amp;</span>amp; <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #660033;">-u</span> git <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>stompserver <span style="color: #000000; font-weight: bold;">&amp;</span>amp;
     log_end_msg <span style="color: #007800;">$?</span>
    <span style="color: #000000; font-weight: bold;">;;</span>
&nbsp;
stop<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #666666; font-style: italic;"># log_daemon_msg “Stopping stompserver” “stomp”</span>
&nbsp;
     <span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/</span>gitorious <span style="color: #000000; font-weight: bold;">&amp;</span>amp;<span style="color: #000000; font-weight: bold;">&amp;</span>amp; <span style="color: #c20cb9; font-weight: bold;">kill</span> <span style="color: #660033;">-INT</span> <span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">cat</span> log<span style="color: #000000; font-weight: bold;">/</span>stompserver.pid<span style="color: #000000; font-weight: bold;">`</span>
     log_end_msg <span style="color: #007800;">$?</span>
     <span style="color: #000000; font-weight: bold;">;;</span>
&nbsp;
restart<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #666666; font-style: italic;"># log_daemon_msg “Restarting stompserver” “stomp”</span>
&nbsp;
    <span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/</span>gitorious <span style="color: #000000; font-weight: bold;">&amp;</span>amp;<span style="color: #000000; font-weight: bold;">&amp;</span>amp; <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #660033;">-u</span> git <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>stompserver <span style="color: #000000; font-weight: bold;">&amp;</span>amp;
    <span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/</span>gitorious <span style="color: #000000; font-weight: bold;">&amp;</span>amp;<span style="color: #000000; font-weight: bold;">&amp;</span>amp; <span style="color: #c20cb9; font-weight: bold;">kill</span> <span style="color: #660033;">-INT</span> <span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">cat</span> log<span style="color: #000000; font-weight: bold;">/</span>stompserver.pid<span style="color: #000000; font-weight: bold;">`</span>
    log_end_msg <span style="color: #007800;">$?</span>
    <span style="color: #000000; font-weight: bold;">;;</span>
&nbsp;
status<span style="color: #7a0874; font-weight: bold;">&#41;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-f</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/</span>gitorious<span style="color: #000000; font-weight: bold;">/</span>log<span style="color: #000000; font-weight: bold;">/</span>stompserver.pid <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;stompserver is running. pid: <span style="color: #780078;">`cat /var/www/gitorious/log/stompserver.pid`</span>&quot;</span>
    <span style="color: #000000; font-weight: bold;">else</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;stompserver is not running.&quot;</span>
    <span style="color: #000000; font-weight: bold;">fi</span>
    <span style="color: #000000; font-weight: bold;">;;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">*</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #666666; font-style: italic;"># log_action_msg “Usage: /etc/init.d/stomp {start|stop|restart|status}”</span>
&nbsp;
    <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">2</span>
    <span style="color: #000000; font-weight: bold;">;;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">esac</span>
<span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">0</span></pre></td></tr></table></div>

<p>大変酷いスクリプトであることは自分でも知ってますが、まあ用には足ります。</p>
<p>gistに上げてるので、以下で落とせます。</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">wget</span> <span style="color: #660033;">-O</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span>stomp http:<span style="color: #000000; font-weight: bold;">//</span>gist.github.com<span style="color: #000000; font-weight: bold;">/</span>raw<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">269240</span><span style="color: #000000; font-weight: bold;">/</span>ce6507d4387004040e73667586ee2b1ced1de9fa<span style="color: #000000; font-weight: bold;">/</span>stomp.sh <span style="color: #000000; font-weight: bold;">&amp;</span>amp;<span style="color: #000000; font-weight: bold;">&amp;</span>amp; <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">chown</span> a+x <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span>stomp</pre></div></div>

<p>そしてpollerは、起動スクリプトはもちろん動かなくて、それも上手いこと直せない。。</p>
<p>とにかく使いたいなら、サーバ起動直後に以下のワンライナーで手動起動する運用で、対応できるかと思う。</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">su</span> git <span style="color: #660033;">-c</span> <span style="color: #ff0000;">&quot;cd /var/www/gitorious &amp;amp;&amp;amp; RAILS_ENV=production ruby script/poller start &amp;amp;&amp;amp; sleep 10&quot;</span></pre></div></div>

<p>このコマンドを起動スクリプトに書いてもぜんぜんうまく行かないんだけど、何で？</p>
<p>Gitoriousの全機能が動く状態になっているか、各デーモンの状況を確認する際は以下のコマンドで。</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">ps-ef <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> git</pre></div></div>

<p>各デーモンは「<strong>git</strong>」ユーザで動いています。なお、Passengerの仕様として、Railsのプロセスは<code>$RAILS_ROOT/config/environment.rb</code>のオーナーなので、こっちのプロセスもgitになっているはず。</p>
<p>以下の4プロセスが存在すればOK。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;">git       <span style="color: #000000;">2243</span>     <span style="color: #000000;">1</span>  <span style="color: #000000;">0</span> <span style="color: #000000;">22</span>:<span style="color: #000000;">11</span> ?        00:00:00 <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>ruby <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/</span>gitorious<span style="color: #000000; font-weight: bold;">/</span>script<span style="color: #000000; font-weight: bold;">/</span>git-daemon <span style="color: #660033;">-d</span>
git       <span style="color: #000000;">2547</span>     <span style="color: #000000;">1</span>  <span style="color: #000000;">0</span> <span style="color: #000000;">22</span>:<span style="color: #000000;">12</span> ?        00:00:00 searchd <span style="color: #660033;">--config</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/</span>gitorious<span style="color: #000000; font-weight: bold;">/</span>config<span style="color: #000000; font-weight: bold;">/</span>ultrasphinx<span style="color: #000000; font-weight: bold;">/</span>production.conf
git       <span style="color: #000000;">2925</span>     <span style="color: #000000;">1</span>  <span style="color: #000000;">0</span> <span style="color: #000000;">22</span>:<span style="color: #000000;">18</span> ?        00:00:06 poller
git       <span style="color: #000000;">3213</span>     <span style="color: #000000;">1</span>  <span style="color: #000000;">0</span> <span style="color: #000000;">22</span>:<span style="color: #000000;">26</span> pts<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">0</span>    00:00:00 <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>ruby <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>stompserver</pre></td></tr></table></div>

<p>上がってなかったらserviceコマンドで上げましょう（結局手動かよ！！！）。</p>
<h3>その他</h3>
<p>Wikiの通りやっていけば、Ubuntuならそれほど詰まらないかとは思います。</p>
<h3>細かい点</h3>
<ul>
<li>どうせローカルでしか使わないのでSSL要らない</li>
</ul>
<p><code>config/environment/production.rb</code>に、</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">SslRequirement.<span style="color:#9900CC;">disable_ssl_check</span> = <span style="color:#0000FF; font-weight:bold;">true</span></pre></td></tr></table></div>

<p>を追記する</p>
<ul>
<li>cronがなんかコケる</li>
</ul>
<p>crontab -e する際に、環境変数 <code>PATH=/usr/local/bin:/usr/bin:/bin</code> を設定した方がいい気がする。あとgitユーザで設定してるか確認。</p>
<ul>
<li>ログインできない</li>
</ul>
<p>gitorious.ymlでのホスト名と、実際にブラウザでアクセスするホスト名が、一致してないとダメっぽいです。なので「localhost」とかで動作検証しようとするとダメだった。/etc/hosts使うとか、192.168.hoge.fugaをgitorious.ymlにちゃんと書くとか。</p>
<ul>
<li>tarballがダウンロードできない</li>
</ul>
<p>gitorious.ymlで設定してる<code>archive_cache_dir</code>の場所に、ちゃんとtarballはできてるんですけど、いざブラウザから落とそうとすると1kbの空ファイルになってしまう。。。</p>
<p>ソース見る限り「X-SendFile」という手段でファイルをダウンロードさせてるようなので、Apacheなら「mod_xsendfile」を入れなければいけない模様。</p>
<ul>
<li><a href="http://tn123.ath.cx/mod_xsendfile/">http://tn123.ath.cx/mod_xsendfile/</a></li>
<li><a href="http://packages.debian.org/ja/squeeze/i386/libapache2-mod-xsendfile/download">http://packages.debian.org/ja/squeeze/i386/libapache2-mod-xsendfile/download</a> (debもあった)</li>
<li><a href="http://www.ideaxidea.com/archives/2009/11/mod_xsendfile_installed.html">http://www.ideaxidea.com/archives/2009/11/mod_xsendfile_installed.html</a></li>
</ul>
<blockquote><p>ちなみに個人的には「XSendFile on」の記述を忘れていたため、ダウンロードはされるが空のファイル、というみっともないハマリ方をしちゃいましたよ。ご注意あれ。</p></blockquote>
<p>はい、私もでした。ということでソースがtarballでも落とせて便利。</p>
<h3>未解決</h3>
<ul>
<li>日本語検索できなくね</li>
</ul>
<p>sphinx search自体が日本語対応しているか怪しい。。と思ったが。</p>
<p>UltraSphinxプラグインが起動する検索デーモンの共通設定は、</p>
<p><code>${RAILS_ROOR}/config/ultrasphinx/default.base</code></p>
<p>via: <a href="http://blog.evanweaver.com/files/doc/fauna/ultrasphinx/classes/Ultrasphinx.html">http://blog.evanweaver.com/files/doc/fauna/ultrasphinx/classes/Ultrasphinx.html</a></p>
<p>見た感じ*.baseにはERB記法が使えそう。</p>
<p>そして、なにやら日本語対応のための記事っぽいものが英語であった。</p>
<ul>
<li><a href="http://crunchytoast.com/2009/04/14/sphinx-search-in-japanese/">http://crunchytoast.com/2009/04/14/sphinx-search-in-japanese/</a></li>
<li><a href="http://crunchytoast.com/2009/05/01/japanese-sphinx-explained/">http://crunchytoast.com/2009/05/01/japanese-sphinx-explained/</a></li>
</ul>
<p>この辺とか、あと <code>/usr/local/etc/sphinx.conf.dist</code> にもサンプルがあったりするのでそれを見ながら試行錯誤すれば出来るようになるのかも。でも時間無い。</p>
<p>取り急ぎは「カテゴリーは英語にする」とかそういう運用ルールでがんばるしかないか。</p>
<p>もしくは、UltraSphinx自体をハックする必要もあるかもしれない。<br />
<a href="http://blog.evanweaver.com/files/doc/fauna/ultrasphinx/files/README.html">http://blog.evanweaver.com/files/doc/fauna/ultrasphinx/files/README.html</a></p>
<h3>まとめ</h3>
<p>知らないライブラリを沢山使ってるので勉強になった。</p>
<p>日本語情報少なくて困惑する。</p>
<p>でも、トラブルがあるたびに、ソースを追っかけたりして対処していけば、なんとなく入れられるんではないでしょうか。というか毎日のようにPUSHがあるので、一ヵ月後ぐらいにはここに書いたtips意味無くなるんじゃあないか。</p>
<h3>大変参考になった先達</h3>
<p>Wikiの他、以下の方々</p>
<ul>
<li><a href="http://d.hatena.ne.jp/bannyan/20090131/1233399237">http://d.hatena.ne.jp/bannyan/20090131/1233399237</a></li>
<li><a href="http://d.hatena.ne.jp/Hirohiro/20090522/1243016921">http://d.hatena.ne.jp/Hirohiro/20090522/1243016921</a></li>
<li><a href="http://www.araki.net/wiki/araki-wiliki.cgi?araki%3AGitorious">http://www.araki.net/wiki/araki-wiliki.cgi?araki%3AGitorious</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.udzura.jp/2010/01/06/gitorious-installation-memo/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://blog.udzura.jp/2010/01/06/gitorious-installation-memo/" />
	</item>
		<item>
		<title>RailsでExcelをダウンロードさせる</title>
		<link>http://blog.udzura.jp/2009/12/16/download-xls-files-on-rails/</link>
		<comments>http://blog.udzura.jp/2009/12/16/download-xls-files-on-rails/#comments</comments>
		<pubDate>Wed, 16 Dec 2009 08:52:19 +0000</pubDate>
		<dc:creator>udzura</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://blog.udzura.jp/?p=185</guid>
		<description><![CDATA[色々試してて、多少は形になったのでメモしときます。
まず、RubyでExcelを作らないといけません。色々方法はありますが、というか素直な方法はJRuby＋POIなんじゃないかとも思いますが、今回は「spreadsheet」を使います。
spreadsheet ライブラリとは
spreadsheetは普通にgemでインストールできます。

1
sudo gem install spreadsheet

spreadsheet自体には、 .to_blob に当たるインスタンスメソッドがありません（ようです）が、書き込み先にFile IOを指定できます。なので、いったんTempfileに書き込んで、 read して send_data する方法で出来るんじゃないかと思いました。
emvironment.rbで require 'spreadsheet' すると、一緒にtempfile.rbも読み込まれる模様です。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class ExcelExportController &#60; ApplicationController
  def download
    birds = Bird.find&#40;:all&#41;
    legend = &#91;&#34;名前&#34;, &#34;色&#34;, &#34;鳴き声&#34;&#93;
&#160;
    book = Spreadsheet::Workbook.new
    sheet1 = book.create_worksheet&#40;:name =&#62; '鳥類'&#41;
&#160;
    sheet1.row&#40;0&#41;.concat legend
  [...]]]></description>
			<content:encoded><![CDATA[<p>色々試してて、多少は形になったのでメモしときます。</p>
<p>まず、RubyでExcelを作らないといけません。色々方法はありますが、というか素直な方法はJRuby＋POIなんじゃないかとも思いますが、今回は「<strong>spreadsheet</strong>」を使います。</p>
<h3>spreadsheet ライブラリとは</h3>
<p>spreadsheetは普通にgemでインストールできます。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> gem <span style="color: #c20cb9; font-weight: bold;">install</span> spreadsheet</pre></td></tr></table></div>

<p>spreadsheet自体には、 <code>.to_blob</code> に当たるインスタンスメソッドがありません（ようです）が、<strong>書き込み先にFile IOを指定できます</strong>。なので、いったんTempfileに書き込んで、 <code>read</code> して <code>send_data</code> する方法で出来るんじゃないかと思いました。</p>
<p>emvironment.rbで <code>require 'spreadsheet'</code> すると、一緒にtempfile.rbも読み込まれる模様です。</p>
<p><span id="more-185"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> ExcelExportController <span style="color:#006600; font-weight:bold;">&lt;</span> ApplicationController
  <span style="color:#9966CC; font-weight:bold;">def</span> download
    birds = Bird.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:all</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    legend = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;名前&quot;</span>, <span style="color:#996600;">&quot;色&quot;</span>, <span style="color:#996600;">&quot;鳴き声&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
    book = <span style="color:#6666ff; font-weight:bold;">Spreadsheet::Workbook</span>.<span style="color:#9900CC;">new</span>
    sheet1 = book.<span style="color:#9900CC;">create_worksheet</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;">'鳥類'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
    sheet1.<span style="color:#9900CC;">row</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">concat</span> legend
    sheet1.<span style="color:#9900CC;">row</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">default_format</span> = <span style="color:#6666ff; font-weight:bold;">Spreadsheet::Format</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>
                                     <span style="color:#ff3333; font-weight:bold;">:color</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:red</span>,
                                     <span style="color:#ff3333; font-weight:bold;">:weight</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:bold</span>,
                                     <span style="color:#ff3333; font-weight:bold;">:horizontal_align</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:center</span>
                                   <span style="color:#006600; font-weight:bold;">&#41;</span>
    sheet1.<span style="color:#9900CC;">row</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">height</span> = <span style="color:#006666;">12</span>
&nbsp;
    rownum = <span style="color:#006666;">1</span>
    birds.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>bird<span style="color:#006600; font-weight:bold;">|</span>
      sheet1.<span style="color:#9900CC;">row</span><span style="color:#006600; font-weight:bold;">&#40;</span>rownum<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">concat</span> <span style="color:#006600; font-weight:bold;">&#91;</span>bird.<span style="color:#9900CC;">name</span>, bird.<span style="color:#9900CC;">color</span>, bird.<span style="color:#9900CC;">twitter</span><span style="color:#006600; font-weight:bold;">&#93;</span>
      sheet1.<span style="color:#9900CC;">row</span><span style="color:#006600; font-weight:bold;">&#40;</span>rownum<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">height</span> = <span style="color:#006666;">12</span>
      rownum <span style="color:#006600; font-weight:bold;">+</span>= <span style="color:#006666;">1</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#008000; font-style:italic;"># 横幅を自動調整</span>
    legend.<span style="color:#9900CC;">size</span>.<span style="color:#9900CC;">times</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>i<span style="color:#006600; font-weight:bold;">|</span>
      sheet1.<span style="color:#9900CC;">column</span><span style="color:#006600; font-weight:bold;">&#40;</span>i<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">width</span> = sheet1.<span style="color:#9900CC;">column</span><span style="color:#006600; font-weight:bold;">&#40;</span>i<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">max_by</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span>:size<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">size</span> <span style="color:#006600; font-weight:bold;">*</span> <span style="color:#006666;">0.85</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    tmpfile = <span style="color:#CC00FF; font-weight:bold;">Tempfile</span>.<span style="color:#9900CC;">new</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;excel_tmp&quot;</span>, <span style="color:#996600;">&quot;.xls&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    book.<span style="color:#9900CC;">write</span> tmpfile
&nbsp;
    tmpfile.<span style="color:#CC0066; font-weight:bold;">open</span> <span style="color:#008000; font-style:italic;"># reopen</span>
&nbsp;
    send_data<span style="color:#006600; font-weight:bold;">&#40;</span>
      tmpfile.<span style="color:#9900CC;">read</span>,
      <span style="color:#ff3333; font-weight:bold;">:disposition</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'attachment'</span>,
      <span style="color:#ff3333; font-weight:bold;">:type</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'application/octet-stream'</span>,
      <span style="color:#ff3333; font-weight:bold;">:filename</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;excel-#{Time.now.strftime('%y%m%d%H%M%S')}-#{'%03d' % rand(999)}.xls&quot;</span>
    <span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
    tmpfile.<span style="color:#9900CC;">close</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">true</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></td></tr></table></div>

<p>こんな感じでしょうか。ちなみに、動作検証してません。。特に <code>:type</code> の指定とか怪しいと思う。</p>
<p>色々と読み替えてお使い下さい。</p>
<h3>参考になるサイト</h3>
<ul>
<li><a href="http://rubyforge.org/projects/spreadsheet/">spreadsheet プロジェクトページ</a></li>
<li><a href="http://spreadsheet.rubyforge.org/">spreadsheet-0.6.4.1 Documentation</a></li>
<li><a href="http://d.hatena.ne.jp/takihiro/20090710/1247233280">Linux 上の Ruby で Excel ファイルを作成する</a></li>
<li><a href="http://www.ruby-lang.org/ja/man/html/tempfile.html">tempfile &#8211; Ruby（旧）リファレンスマニュアル</a></li>
</ul>
<h3 id="omake">tips: Tempfile</h3>
<p>マニュアルのどこにも書いてませんが、newの引数basenameに配列を渡すと、[元となるファイル名, 拡張子]みたいなTempfileを作れます。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">t = <span style="color:#CC00FF; font-weight:bold;">Tempfile</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;temp&quot;</span>, <span style="color:#996600;">&quot;.xls&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></td></tr></table></div>

<p>ソースはこんな感じでした。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#CC00FF; font-weight:bold;">Tempfile</span> <span style="color:#006600; font-weight:bold;">&lt;</span> DelegateClass<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">File</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#008000; font-style:italic;">#...</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> make_tmpname<span style="color:#006600; font-weight:bold;">&#40;</span>basename, n<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">case</span> basename
    <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#CC0066; font-weight:bold;">Array</span>
      prefix, suffix = <span style="color:#006600; font-weight:bold;">*</span>basename
    <span style="color:#9966CC; font-weight:bold;">else</span>
      prefix, suffix = basename, <span style="color:#996600;">''</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    t = <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span>.<span style="color:#9900CC;">strftime</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;%Y%m%d&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    path = <span style="color:#996600;">&quot;#{prefix}#{t}-#{$$}-#{rand(0x100000000).to_s(36)}-#{n}#{suffix}&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  private <span style="color:#ff3333; font-weight:bold;">:make_tmpname</span>
  <span style="color:#008000; font-style:italic;">#...</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.udzura.jp/2009/12/16/download-xls-files-on-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://blog.udzura.jp/2009/12/16/download-xls-files-on-rails/" />
	</item>
		<item>
		<title>Railsでブログパーツ</title>
		<link>http://blog.udzura.jp/2009/11/11/snippets-on-ails/</link>
		<comments>http://blog.udzura.jp/2009/11/11/snippets-on-ails/#comments</comments>
		<pubDate>Wed, 11 Nov 2009 11:21:28 +0000</pubDate>
		<dc:creator>udzura</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://blog.udzura.jp/?p=62</guid>
		<description><![CDATA[gist.githubは、右上の「embed」をクリックすると、
(PCで見るとプログラムコードが表示されます)
のようなコード片が出てきて、それをブログ等に貼り付けるとシンタクスハイライトされたコードが載せられる。
こんな感じのブログパーツをRuby on Railsで作るとどうなるかなーとか考えた。


今読ミのブログパーツをJavaScriptでやってみる

上記ブログでやっておられることがまさに僕のやりたいことだったんだけど、もうちょっとシンプルにしてみた。prototype.js非依存で。
まずはコントローラー。
(PCで見るとプログラムコードが表示されます)
そしてrjsファイル。gistで拡張子が「rb」になってるのはシンタクスハイライト対策です、、（.rjs だと効かないでやんの）
(PCで見るとプログラムコードが表示されます)
色々考えた結論としては、こんな感じで document.write を直書きでおkなんじゃないかと思った。この際気をつけることとして、僕はJavaScriptが久しぶり過ぎて引数のカッコを省略してしまったので、皆さんも気をつけましょうね。
あと、RJSテンプレート内部でも普通に url_for とか render とか使えるし、render は究極的に文字列を返すだけなので、あとは良きに計らいましょう。そうそう、なんか render(:partial) が、拡張子までちゃんと記述しとかないとうまくいかなかったのでそこも注意。
というか、ちらほら目につく上記コードの String#gsub とかはヘルパーメソッドにまとめた方が断然いいと思うので、ちゃんとするときはそうしましょう。
一度アクセスでもして軽く動作確認したら、ブログに以下の記述を。
(PCで見るとプログラムコードが表示されます)
この辺はサンプルなので、適宜読み替えてほしいかも。
あと、ふと思ったんだけど、ブログパーツ提供側がUTF-8で、ブログ自体はEUC-JPとかだと困ったことになるなって思った。rjsにパラメータでエンコード情報を渡すか、JavaScript側でエンコードを変換するか、そんな感じになるのかね。
]]></description>
			<content:encoded><![CDATA[<p>gist.githubは、右上の「embed」をクリックすると、</p>
<p><script src="http://gist.github.com/231518.js"></script><noscript>(PCで見るとプログラムコードが表示されます)</noscript></p>
<p>のようなコード片が出てきて、それをブログ等に貼り付けるとシンタクスハイライトされたコードが載せられる。</p>
<p>こんな感じのブログパーツをRuby on Railsで作るとどうなるかなーとか考えた。<br />
<span id="more-62"></span></p>
<ul>
<li><a href="http://www.iwazer.com/~iwazawa/diary/2008/05/javascript-1.html">今読ミのブログパーツをJavaScriptでやってみる</a></li>
</ul>
<p>上記ブログでやっておられることがまさに僕のやりたいことだったんだけど、もうちょっとシンプルにしてみた。prototype.js非依存で。</p>
<p>まずはコントローラー。<br />
<script src="http://gist.github.com/231797.js"></script><noscript>(PCで見るとプログラムコードが表示されます)</noscript></p>
<p>そしてrjsファイル。gistで拡張子が「rb」になってるのはシンタクスハイライト対策です、、（.rjs だと効かないでやんの）<br />
<script src="http://gist.github.com/231799.js"></script><noscript>(PCで見るとプログラムコードが表示されます)</noscript></p>
<p>色々考えた結論としては、こんな感じで document.write を直書きでおkなんじゃないかと思った。この際気をつけることとして、僕はJavaScriptが久しぶり過ぎて引数のカッコを省略してしまったので、皆さんも気をつけましょうね。</p>
<p>あと、RJSテンプレート内部でも普通に url_for とか render とか使えるし、render は究極的に文字列を返すだけなので、あとは良きに計らいましょう。そうそう、なんか render(:partial) が、拡張子までちゃんと記述しとかないとうまくいかなかったのでそこも注意。</p>
<p>というか、ちらほら目につく上記コードの String#gsub とかはヘルパーメソッドにまとめた方が断然いいと思うので、ちゃんとするときはそうしましょう。</p>
<p>一度アクセスでもして軽く動作確認したら、ブログに以下の記述を。<br />
<script src="http://gist.github.com/231801.js"></script><noscript>(PCで見るとプログラムコードが表示されます)</noscript></p>
<p>この辺はサンプルなので、適宜読み替えてほしいかも。</p>
<p>あと、ふと思ったんだけど、ブログパーツ提供側がUTF-8で、ブログ自体はEUC-JPとかだと困ったことになるなって思った。rjsにパラメータでエンコード情報を渡すか、JavaScript側でエンコードを変換するか、そんな感じになるのかね。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.udzura.jp/2009/11/11/snippets-on-ails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://blog.udzura.jp/2009/11/11/snippets-on-ails/" />
	</item>
	</channel>
</rss>
