<?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>Aerialarts &#187; Rails</title>
	<atom:link href="http://aerial.st/tags/rails/feed/" rel="self" type="application/rss+xml" />
	<link>http://aerial.st</link>
	<description></description>
	<lastBuildDate>Mon, 16 Jan 2012 09:56:44 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.1</generator>
		<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://aerial.st/tags/rails/feed/" />
		<item>
		<title>Herokuの手始め</title>
		<link>http://aerial.st/archive/2011/08/12/heroku/</link>
		<comments>http://aerial.st/archive/2011/08/12/heroku/#comments</comments>
		<pubDate>Thu, 11 Aug 2011 17:20:55 +0000</pubDate>
		<dc:creator>ikm</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[heroku]]></category>
		<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://aerial.st/?p=1130</guid>
		<description><![CDATA[毎回Herokuを使ってみようと思うたびに手順を忘れるのでメモ。 アプリケーションの準備 hello_herokuというRailsアプリケーションを作ることにする。 Herokuの標準のDBがPostgreSQLなので今回もそれを指定する。 Test::UnitとPrototype.jsは要らないので-JTする。 $ rails -v #=> 3.0.9 $ rails new hello_heroku -JT -d postgresql $ cd hello_heroku いつもどおりgitリポジトリを作る $ git init $ git add . $ git commit -am "initial commit" 専用のRVM gemsetも作っておく。 $ rvm use 1.9.2@hello_heroku --create $ echo "rvm use 1.9.2@hello_heroku" > .rvmrc Gemfileにpgとherokuを追加してインストール。 （heroku gemは:require => falseでもいいのかな？） $ vim Gemfile + gem 'pg' + gem 'heroku' $ bundle $ git add . $ git commit -am "add pg and heroku gems" 念のためGitHubに上げておく。 （hubコマンドが便利なので、alias git=hubしてある。） [...]]]></description>
		<wfw:commentRss>http://aerial.st/archive/2011/08/12/heroku/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://aerial.st/archive/2011/08/12/heroku/" />
	</item>
		<item>
		<title>nginx + unicorn + Rails on Mac</title>
		<link>http://aerial.st/archive/2011/06/16/nginx-unicorn-rails-on-mac/</link>
		<comments>http://aerial.st/archive/2011/06/16/nginx-unicorn-rails-on-mac/#comments</comments>
		<pubDate>Thu, 16 Jun 2011 03:40:12 +0000</pubDate>
		<dc:creator>ikm</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[nginx]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[unicorn]]></category>

		<guid isPermaLink="false">http://aerial.st/?p=1098</guid>
		<description><![CDATA[とりあえず動かしてみたのでメモ。 unicornを動かす まずはgemをインストール。 $ gem install unicorn unicornの処理を設定する $ cd &#60;RAILS_ROOT&#62; $ vi config/unicorn.rb &#60;RAILS_ROOT&#62;/config/unicorn.rbはこんな感じ（nginx + unicorn を試してみたからほぼそのまま拝借）： # ワーカーの数 worker_processes 2 # ソケット経由で通信する listen File.expand_path('tmp/sockets/unicorn.sock', ENV['RAILS_ROOT']) # ログ stderr_path File.expand_path('log/unicorn.log', ENV['RAILS_ROOT']) stdout_path File.expand_path('log/unicorn.log', ENV['RAILS_ROOT']) # ダウンタイムなくす preload_app true before_fork do &#124;server, worker&#124; defined?(ActiveRecord::Base) and ActiveRecord::Base.connection.disconnect! old_pid = "#{ server.config[:pid] }.oldbin" unless old_pid == server.pid begin # SIGTTOU だと worker_processes が多いときおかしい気がする Process.kill :QUIT, File.read(old_pid).to_i rescue Errno::ENOENT, Errno::ESRCH end end end after_fork do &#124;server, worker&#124; defined?(ActiveRecord::Base) and ActiveRecord::Base.establish_connection end そして起動。 $ [...]]]></description>
		<wfw:commentRss>http://aerial.st/archive/2011/06/16/nginx-unicorn-rails-on-mac/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://aerial.st/archive/2011/06/16/nginx-unicorn-rails-on-mac/" />
	</item>
		<item>
		<title>has_manyな関連先をまとめてINSERTする</title>
		<link>http://aerial.st/archive/2011/06/11/insert-has-many-relations/</link>
		<comments>http://aerial.st/archive/2011/06/11/insert-has-many-relations/#comments</comments>
		<pubDate>Sat, 11 Jun 2011 03:00:51 +0000</pubDate>
		<dc:creator>ikm</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[actionview]]></category>
		<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://aerial.st/?p=1081</guid>
		<description><![CDATA[fields_for、accepts_nested_attributes_forを使って、has_manyな関連先をまとめてINSERTする方法。ソースはgithubに上げておいた。ちなみにRails 3.0.8。 Post has_many Tags through Taggingsというモデルがあったとする。 とりあえずscaffoldはこんな感じ。Taggingだけは画面が要らないのでmodelだけ。 $ rails g scaffold posts title:string text:text $ rails g scaffold tags name:string $ rails g model tagging post:references tag:references $ rake db:migrate コードの修正で重要なのは次の2点。 app/models/post.rb に accepts_nested_attributes_forを設定する。 class Post < ActiveRecord::Base has_many :taggings has_many :tags, :through => :taggings accepts_nested_attributes_for :taggings end これで次のようなコードが実行された時にTaggingもまとめて作ってくれるようになる。 Post.create( :title => "タイトル", :text => "本文", :taggings_attributes => [ { :tag_id => 1 }, { :tag_id => 2 } ] ) app/views/posts/_form.html に fields_for を使ってリレーション先についてのフォームを作る。第一引数がtaggings_attributes[]になっているのがポイント。これはaccepts_nested_attributes_forに合わせて設定する。 &#60;div class="field"&#62; Tags&#60;br /&#62; [...]]]></description>
		<wfw:commentRss>http://aerial.st/archive/2011/06/11/insert-has-many-relations/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://aerial.st/archive/2011/06/11/insert-has-many-relations/" />
	</item>
		<item>
		<title>Rails勉強会@東京第58回に行ってきた</title>
		<link>http://aerial.st/archive/2011/01/03/railstoyko-58/</link>
		<comments>http://aerial.st/archive/2011/01/03/railstoyko-58/#comments</comments>
		<pubDate>Mon, 03 Jan 2011 13:42:42 +0000</pubDate>
		<dc:creator>ikm</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[cucumber]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[railstokyo]]></category>
		<category><![CDATA[rspec]]></category>
		<category><![CDATA[test]]></category>

		<guid isPermaLink="false">http://aerial.st/?p=882</guid>
		<description><![CDATA[昨年12/18に行われたRails勉強会@東京第58回に行ってきた。 テストの話 「テストの話」で大まかに扱われるけど、細かく見ると次の3つの話題が出てくる。 どうやってテストを書くか うまいテストの仕方・資産としての残し方 テストの文化の浸透のさせ方・継続の仕方 今回は@moroさんをファシリテータとして、「rspecでテストをどう書くか」と「RelishでRspecの新機能を見る」の2点を行った。 話題がバラバラになりがちなので、進行をされた@moroさんはすごいなと思っていたんだけど、ここらへんをうまくカバーできるやり方を見つけたいと思う今日この頃。 で、その時の結論をうけた上で今覚えてる限りの結論。 どうやってテストを書くか （いつか書く） うまいテストの仕方・資産としての残し方 今回は出なかったけど、捨てるテスト、残すテストの話、かなぁ。 開発時の内部実装まで含めた、細かい挙動確認のためのテストで残しておくと後々リファクタリングすることができなくなるので、開発が一段落したらいっそ捨てるのもアリ。 それに対して外側からアプリケーションを叩いてそのINとOUTを見るためのテストは資産として残さなきゃいけない。 テストの文化の浸透のさせ方・継続の仕方 Hudsonを使って継続的にテストの実行とRCovによるカバレッジの集計をさせてそれを共有するのが良いらしい。Rubyではないけどサイボウズでの事例も興味深い。 あとはペアプロでTDDに慣らしていくとか。 あとこれはちょっと別かもしれないけど、「最初にテストを書かなきゃいけないという話だけど、なかなか難しい」という話が出てきた。それに対して@moroさんの話では、「最終的にテストを書いてチェックができればいいので、実装とテストのどちらが先になっても問題はない」ということだった。 自分の場合も、動作の確認がちゃんとできればいいやと思って実装してからテストを書くことは多い。もちろん実装の規模にもよるんだろうけど。 OmniAuthの話 （あとで書く） ActiveSupport 3系のソースを読む （あとで書く）]]></description>
		<wfw:commentRss>http://aerial.st/archive/2011/01/03/railstoyko-58/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://aerial.st/archive/2011/01/03/railstoyko-58/" />
	</item>
		<item>
		<title>Cloudant+ActiveResource</title>
		<link>http://aerial.st/archive/2010/11/07/cloudant-via-activeresource/</link>
		<comments>http://aerial.st/archive/2010/11/07/cloudant-via-activeresource/#comments</comments>
		<pubDate>Sun, 07 Nov 2010 12:39:11 +0000</pubDate>
		<dc:creator>ikm</dc:creator>
				<category><![CDATA[ActiveResource]]></category>
		<category><![CDATA[CouchDB]]></category>
		<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://aerial.st/?p=785</guid>
		<description><![CDATA[nosqlgogoに行ってきた！ そこでCouchDBについて初めて聴き、RESTful APIを持っているならActiveResourceで接続できるんじゃないか？と思い、Cloudantの2GB無料アカウントをもらったのでブログでも作ってみようと試してみた。 で、現在挫折中。残骸はgithubにあげてある。 実際はCouchRest Modelが使えそう（教えてくれた@d6rkaizさんありがとう！）。 一応作業のメモ。これでfind, firstは使えるようになる。 CloudantというActiveResource::Baseを継承した抽象クラスを作り、そこでCloudantにあるCouchDBとのやりとりを仲介することにした レスポンスはすべてJSONで返ってくるが、APIのURL末尾に.jsonが不要なのでcollection_path、element_path、new_element_pathから.jsonを除く さらにCouchDBでドキュメントの一覧を取得するには/some_documents/_all_docsというURLになるのでcollection_pathに_all_docsを書き加える またドキュメントの一覧を取得した場合には以下のように多段rowsというキーに対して配列 各ドキュメントごとに_idというプライマリキーを持つのでself.primary_keyを書き換える。ただしActiveResource::Baseのクラス変数のprimary_keyは継承したクラスごとに設定されてしまうため、そのままでは子クラスで共通して使うことができない。そこでcattr_accessorで上書きしてから使う。 class Cloudant < ActiveResource::Base self.site = "https://#{CLOUDANT_USER}.cloudant.com/" self.user = CLOUDANT_USER self.password = CLOUDANT_PASS self.format = :json # primary_key is for each model by default, # so override them by one class variable cattr_accessor :primary_key self.primary_key = "_id" def id_from_response(response) return nil if response.body.empty? ::JSON.parse(response.body)["id"].to_i end class row["id"] } } instantiate_collection_without_cloudant(rows, prefix_options) else instantiate_collection_without_cloudant(collection, prefix_options) end end alias_method_chain :instantiate_collection, :cloudant end end]]></description>
		<wfw:commentRss>http://aerial.st/archive/2010/11/07/cloudant-via-activeresource/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://aerial.st/archive/2010/11/07/cloudant-via-activeresource/" />
	</item>
		<item>
		<title>ActionMailerでShift_JISなメールを送る</title>
		<link>http://aerial.st/archive/2010/10/28/send-shift-jis-mail-by-acionmailer/</link>
		<comments>http://aerial.st/archive/2010/10/28/send-shift-jis-mail-by-acionmailer/#comments</comments>
		<pubDate>Wed, 27 Oct 2010 15:21:56 +0000</pubDate>
		<dc:creator>ikm</dc:creator>
				<category><![CDATA[ActionMailer]]></category>
		<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://aerial.st/?p=774</guid>
		<description><![CDATA[ActionMailerを使うとメールはデフォルトではUTF-8で送信されるけど、それをShift_JISで送信するには次のようにする。ちなみに2.3.8で確認済み。 ビューのファイルをShift_JISで記述する ActionMailer::BaseでShift_JISにするように設定する 2については以下のような感じ。 # -*- coding: utf-8 -*- class ShiftjisMailer < ActionMailer::Base self.default_charset = "Shift_JIS" # デフォルトの文字コード self.default_content_type = "text/plain" def test recipients "foo@example.com" from "bar@example.com" subject "てすと".tosjis # 件名はShift_JISに変換してから渡す end end]]></description>
		<wfw:commentRss>http://aerial.st/archive/2010/10/28/send-shift-jis-mail-by-acionmailer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://aerial.st/archive/2010/10/28/send-shift-jis-mail-by-acionmailer/" />
	</item>
		<item>
		<title>named_scopeの自動生成＋お手軽検索</title>
		<link>http://aerial.st/archive/2010/10/21/generate_named_scope_and_ease_to_search/</link>
		<comments>http://aerial.st/archive/2010/10/21/generate_named_scope_and_ease_to_search/#comments</comments>
		<pubDate>Wed, 20 Oct 2010 15:49:28 +0000</pubDate>
		<dc:creator>ikm</dc:creator>
				<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://aerial.st/?p=768</guid>
		<description><![CDATA[@ukstudioさんの「scopeでお手軽検索」 をRails2.3向けに少し修正1しつつ、カラムごとに一致検索をするようなnamed_scopeを自動生成するようにフック部分にしかけをした。 例えばnameカラムとageカラムを持ったUserモデルについて、 class User < ActiveRecord::Base extend Searchable end とすると、次のような感じでカラムの条件で簡単に検索が可能になる。 User.name("jsmith").first #=> User named jsmith User.search(:name => "jsmith", :age => 20..30) 追記 これだけだとwhereの導入された3.0では記法的なあまりメリットはないかもしれない。2.x系なら:conditionsをキーとしたハッシュが不要になるというメリットはあるけど。 やっぱり独自のnamed_scopeを作ってこそ便利になる気がする。 Rails2.3だとscopedに続けてnamed_scopeのメソッドを呼び出すとエラーが出るらしく、injectの引数にはselfのみを渡すようにした。&#160;&#8617;]]></description>
		<wfw:commentRss>http://aerial.st/archive/2010/10/21/generate_named_scope_and_ease_to_search/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://aerial.st/archive/2010/10/21/generate_named_scope_and_ease_to_search/" />
	</item>
		<item>
		<title>Rails3でTest::Unitを無効化</title>
		<link>http://aerial.st/archive/2010/10/05/throw-away-test-unit-using-rails3/</link>
		<comments>http://aerial.st/archive/2010/10/05/throw-away-test-unit-using-rails3/#comments</comments>
		<pubDate>Tue, 05 Oct 2010 02:46:30 +0000</pubDate>
		<dc:creator>ikm</dc:creator>
				<category><![CDATA[未分類]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[rails3]]></category>
		<category><![CDATA[rspec]]></category>

		<guid isPermaLink="false">http://aerial.st/?p=751</guid>
		<description><![CDATA[Rails3でTest::Unitを使わないようにする方法。rspecの導入方法については参考資料を参照。 まずrails newコマンドで新規プロジェクトを作成する際に-Tオプションを付けて、testディレクトリを作成させないようになる。 $ rails new new_project -T 次にconfig/application.rbを次のように編集して、rails/test_unit/railtieを読み込まないようにする。こうすることでTest::Unitのテンプレート自動生成等を行わないようにする。 --- config/application.rb.org 2010-10-05 11:39:47.000000000 +0900 +++ config/application.rb 2010-10-05 11:41:59.000000000 +0900 @@ -1,6 +1,9 @@ require File.expand_path('../boot', __FILE__) -require 'rails/all' +require 'active_record/railtie' +require 'action_controller/railtie' +require 'action_mailer/railtie' +require 'active_resource/railtie' # If you have a Gemfile, require the gems listed there, including any gems # you've limited to :test, :development, or :production. 参考資料 Rails 3 と RSpec 2 と rake spec Rails3 で RSpec + RCov 日本語対応]]></description>
		<wfw:commentRss>http://aerial.st/archive/2010/10/05/throw-away-test-unit-using-rails3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://aerial.st/archive/2010/10/05/throw-away-test-unit-using-rails3/" />
	</item>
		<item>
		<title>ActiveSupport::Concern</title>
		<link>http://aerial.st/archive/2010/10/04/activesupport-concern/</link>
		<comments>http://aerial.st/archive/2010/10/04/activesupport-concern/#comments</comments>
		<pubDate>Sun, 03 Oct 2010 16:09:45 +0000</pubDate>
		<dc:creator>ikm</dc:creator>
				<category><![CDATA[未分類]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[rails3]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://aerial.st/?p=746</guid>
		<description><![CDATA[以前書いたモジュールの特異メソッドをincludeして使うと同じことが、ActiveSupport::Concernを使うことでもうちょっと綺麗に書ける。 module A extend ActiveSupport::Concern included do # Aがincludeされた際に、includeしたクラスのコンテキストで実行される end module InstanceMethods def instance_method_of_a p "instance_method_of_a" end end module ClassMethods def class_method_of_a p "class_method_of_a" end end end class B include A end こんなモジュールをとあるクラスBでincludeすることで、 InstansMethods以下に定義されたメソッドがincludeによってインスタンスメソッドとして追加 ClassMethods以下に定義されたメソッドがextendによってクラスメソッドとして追加 される。 モジュールをincludeすることによって追加されるクラスメソッド、インスタンスメソッドが明確になるので嬉しい。 ただそれだけだとちょっとメリットが弱い気がしなくもない。きっとincludedや、今回触れなかったextendedあたりが活躍するんだろう。 参考資料 よたらぼ ActiveSupport::Concernを自分なりに調べてみた]]></description>
		<wfw:commentRss>http://aerial.st/archive/2010/10/04/activesupport-concern/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://aerial.st/archive/2010/10/04/activesupport-concern/" />
	</item>
		<item>
		<title>ActiveModel::Callbacks</title>
		<link>http://aerial.st/archive/2010/10/03/activemodel-callbacks/</link>
		<comments>http://aerial.st/archive/2010/10/03/activemodel-callbacks/#comments</comments>
		<pubDate>Sun, 03 Oct 2010 12:53:15 +0000</pubDate>
		<dc:creator>ikm</dc:creator>
				<category><![CDATA[activemodel]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[rails3]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://aerial.st/?p=740</guid>
		<description><![CDATA[ActiveModel::Callbacksを使えばafter_saveなどのコールバックを手軽に実装できる。 まずActiveRecord::Baseのような抽象クラスを次のように定義する。 class AbstractModel extend ActiveModel::Callbacks define_model_callbacks :save def save _run_save_callbacks do # 具体的なsaveメソッドの内容 # この前後にbefore_saveとafter_save、そしてaround_saveが呼ばれる end end end 重要なのはsaveメソッドの内容を_run_save_callbacksでラップすること。これはコールバックを利用するメソッドに応じて動的に生成され、createメソッドであれば_run_create_callbacksを利用する。 そしてコールバックを利用したい具象クラスでは次のようにする。 class SomeModel < AbstractModel before_save :foo protected def foo # before_saveで実行したい内容 end end これ以外にもbefore_saveにブロックを渡したり、before_saveをインスタンスメソッドとして定義しているオブジェクトを渡したりすることで処理を実装できる。 define_model_callbacksでは複数のコールバック対象のメソッドを指定することができる。 define_model_callbacks :save, :create :onlyオプションを使うことで例えばafter_*だけに制限することもできる。 define_model_callbacks :save, :only => :after]]></description>
		<wfw:commentRss>http://aerial.st/archive/2010/10/03/activemodel-callbacks/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://aerial.st/archive/2010/10/03/activemodel-callbacks/" />
	</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/


Served from: aerial.st @ 2012-02-09 02:07:38 -->
