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

<channel>
	<title>変なヤバいもんログ &#187; Rails</title>
	<atom:link href="http://ecpplus.net/weblog/tag/rails/feed/" rel="self" type="application/rss+xml" />
	<link>http://ecpplus.net/weblog</link>
	<description>平日プログラミング休日ツーリング</description>
	<lastBuildDate>Sat, 03 Dec 2011 16:17:32 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>unit test の fixtures で関連がないテーブルの primary key が取りたい</title>
		<link>http://ecpplus.net/weblog/unit-test-%e3%81%ae-fixtures-%e3%81%a7%e9%96%a2%e9%80%a3%e3%81%8c%e3%81%aa%e3%81%84%e3%83%86%e3%83%bc%e3%83%96%e3%83%ab%e3%81%ae-primary-key-%e3%81%8c%e5%8f%96%e3%82%8a%e3%81%9f%e3%81%84/</link>
		<comments>http://ecpplus.net/weblog/unit-test-%e3%81%ae-fixtures-%e3%81%a7%e9%96%a2%e9%80%a3%e3%81%8c%e3%81%aa%e3%81%84%e3%83%86%e3%83%bc%e3%83%96%e3%83%ab%e3%81%ae-primary-key-%e3%81%8c%e5%8f%96%e3%82%8a%e3%81%9f%e3%81%84/#comments</comments>
		<pubDate>Thu, 11 Aug 2011 05:17:15 +0000</pubDate>
		<dc:creator>ちゅう</dc:creator>
				<category><![CDATA[1]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://ecpplus.net/weblog/?p=1171</guid>
		<description><![CDATA[Rails の unit test を書いていて、関連がないテーブルの primary key が知りたい状況があった。 関連があるテーブルだと、 class User &#38;lt; ActiveRecord::Base has_many :posts end class Post &#38;lt; ActiveRecord::Base belongs_to :user end みたいなときに test/fixtures/users.yml chihaya: name: 千早 test/fixtures/posts.yml first: user: chihaya title: くっ…！ body: 先日フェスがあったのですが… みたいな感じにすると関連が作れる。 今回は、単純なログみたいなもので、色んなテーブルの id を、target_id みたいなところにしまって、クラス名と合わせてユニークになるように管理してた。数が多かったのと関連付けて取る必要が無かったので、関連を作ってなかったんだけれども、fixture を書く時に、外部の id の取得方法に迷った。 つまり、先ほどの例で、has_many, belongs_to が無かった時にどうするかということです。 users.yml に、 id:1 とか書くというのも1つの方法です。 id を指定せずに rake fixtures:load [...]<div class="addthis_toolbox addthis_default_style " addthis:url='http://ecpplus.net/weblog/unit-test-%e3%81%ae-fixtures-%e3%81%a7%e9%96%a2%e9%80%a3%e3%81%8c%e3%81%aa%e3%81%84%e3%83%86%e3%83%bc%e3%83%96%e3%83%ab%e3%81%ae-primary-key-%e3%81%8c%e5%8f%96%e3%82%8a%e3%81%9f%e3%81%84/' addthis:title='unit test の fixtures で関連がないテーブルの primary key が取りたい '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>]]></description>
			<content:encoded><![CDATA[<p>Rails の unit test を書いていて、関連がないテーブルの primary key が知りたい状況があった。</p>
<p>関連があるテーブルだと、</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> User <span style="color:#006600; font-weight:bold;">&amp;</span>lt; <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
has_many <span style="color:#ff3333; font-weight:bold;">:posts</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">class</span> Post <span style="color:#006600; font-weight:bold;">&amp;</span>lt; <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
belongs_to <span style="color:#ff3333; font-weight:bold;">:user</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>みたいなときに</p>
<p>test/fixtures/users.yml</p>

<div class="wp_syntax"><div class="code"><pre class="yml" style="font-family:monospace;">chihaya:
name: 千早</pre></div></div>

<p>test/fixtures/posts.yml</p>

<div class="wp_syntax"><div class="code"><pre class="yml" style="font-family:monospace;">first:
user: chihaya
title: くっ…！
body: 先日フェスがあったのですが…</pre></div></div>

<p>みたいな感じにすると関連が作れる。</p>
<p>今回は、単純なログみたいなもので、色んなテーブルの id を、target_id みたいなところにしまって、クラス名と合わせてユニークになるように管理してた。数が多かったのと関連付けて取る必要が無かったので、関連を作ってなかったんだけれども、fixture を書く時に、外部の id の取得方法に迷った。</p>
<p>つまり、先ほどの例で、has_many, belongs_to が無かった時にどうするかということです。<br />
users.yml に、 id:1 とか書くというのも1つの方法です。</p>
<p>id を指定せずに rake fixtures:load ってすると、id が結構大きな数字になりますが、あれはラベルの文字列によって計算された値みたいですね。</p>
<p><a href="http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html#method-c-identify" title="Fixtures#identify" target="_blank">ActiveRecord::Fixtures.identify(label)</a> というメソッドで計算されていて、プラットフォーム依存で、同じ文字列を与えれば同じ値が返ってくる仕組みになっているようです。</p>
<p>そうすると、先ほどの例だと</p>
<p>test/fixtures/posts.yml</p>

<div class="wp_syntax"><div class="code"><pre class="yml" style="font-family:monospace;">first:
user_id: &amp;lt;%= ActiveRecord::Fixtures.identify(:chihaya) %&amp;gt;
title: くっ…！
body: 先日フェスがあったのですが…</pre></div></div>

<p>とすると、常に千早の id が取れるようになります。ActiveRecord::Fixtures.identify(:chihaya) の値は、見て分かるようにテーブルに依存することはなく、別テーブルで chihaya というラベルを使うと、同じ id になります。ActiveRecord::Fixtures が見つからないときは、 require &#8216;active_record/fixtures&#8217; とします。</p>
<p>なんだろうなーと思ってた巨大な整数の id の計算方法がわかってすっきりしました。</p>
<div class="addthis_toolbox addthis_default_style " addthis:url='http://ecpplus.net/weblog/unit-test-%e3%81%ae-fixtures-%e3%81%a7%e9%96%a2%e9%80%a3%e3%81%8c%e3%81%aa%e3%81%84%e3%83%86%e3%83%bc%e3%83%96%e3%83%ab%e3%81%ae-primary-key-%e3%81%8c%e5%8f%96%e3%82%8a%e3%81%9f%e3%81%84/' addthis:title='unit test の fixtures で関連がないテーブルの primary key が取りたい '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>]]></content:encoded>
			<wfw:commentRss>http://ecpplus.net/weblog/unit-test-%e3%81%ae-fixtures-%e3%81%a7%e9%96%a2%e9%80%a3%e3%81%8c%e3%81%aa%e3%81%84%e3%83%86%e3%83%bc%e3%83%96%e3%83%ab%e3%81%ae-primary-key-%e3%81%8c%e5%8f%96%e3%82%8a%e3%81%9f%e3%81%84/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Nginx の X-Accel-Redirect を Rails で使う</title>
		<link>http://ecpplus.net/weblog/nginx-%e3%81%ae-x-accel-redirect-%e3%82%92-rails-%e3%81%a7%e4%bd%bf%e3%81%86/</link>
		<comments>http://ecpplus.net/weblog/nginx-%e3%81%ae-x-accel-redirect-%e3%82%92-rails-%e3%81%a7%e4%bd%bf%e3%81%86/#comments</comments>
		<pubDate>Sun, 21 Nov 2010 18:11:08 +0000</pubDate>
		<dc:creator>ちゅう</dc:creator>
				<category><![CDATA[1]]></category>
		<category><![CDATA[nginx]]></category>
		<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://ecpplus.net/weblog/?p=1087</guid>
		<description><![CDATA[　Nginx で認証つきで大容量ファイルをダウンロードさせたい時は、X-Accel-Redirect を使います。Rails のファイル操作を介さず、Nginxで直接クライアントへデータを送れます。Lighttpd とか Apache でいうところの、X-sendfile と同じような感じでしょうか。 Lighttpd の場合はこちら -> Rails&#038;Lighttpdで大容量ファイルをダウンロード【変なヤバいもんログ内】 　ヘッダに、X-Accel-Redirect でパスを指定して、そのパスを Nginx 側で受けるだけですinternal って付けるのがポイントです。そうすると、このように内部からしか参照できなくなります。外からそのパスを叩いても見れないということになります。 　Rails2以前の場合は自分でヘッダに値を設定する必要があり、Rails3以降は設定を変更するだけでいけるようです。 # Rails2以前 def download # 認証処理などを挟むことができます。 path = &#34;/bigfiles/long_movie.mp4&#34; response.headers&#91;'X-Accel-Redirect'&#93; = path response.headers&#91;'Content-Type'&#93; = &#34;application/force-download&#34; response.headers&#91;'Content-Disposition'&#93; = &#34;attachment; filename=\&#34;#{File.basename(path)}\&#34;&#34; response.headers&#91;'Content-length'&#93; = File.size&#40;RAILS_ROOT + path&#41; render :nothing =&#62; true end &#160; # Rails3 def download # 認証処理などを挟むことができます。 path [...]<div class="addthis_toolbox addthis_default_style " addthis:url='http://ecpplus.net/weblog/nginx-%e3%81%ae-x-accel-redirect-%e3%82%92-rails-%e3%81%a7%e4%bd%bf%e3%81%86/' addthis:title='Nginx の X-Accel-Redirect を Rails で使う '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>]]></description>
			<content:encoded><![CDATA[<p>　Nginx で認証つきで大容量ファイルをダウンロードさせたい時は、X-Accel-Redirect を使います。Rails のファイル操作を介さず、Nginxで直接クライアントへデータを送れます。Lighttpd とか Apache でいうところの、X-sendfile と同じような感じでしょうか。<br />
Lighttpd の場合はこちら -> <a href="http://ecpplus.net/weblog/railslighttp%E3%81%A7%E5%A4%A7%E5%AE%B9%E9%87%8F%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB%E3%82%92%E3%83%80%E3%82%A6%E3%83%B3%E3%83%AD%E3%83%BC%E3%83%89/">Rails&#038;Lighttpdで大容量ファイルをダウンロード【変なヤバいもんログ内】</a></p>
<p>　ヘッダに、X-Accel-Redirect でパスを指定して、そのパスを Nginx 側で受けるだけですinternal って付けるのがポイントです。そうすると、このように内部からしか参照できなくなります。外からそのパスを叩いても見れないということになります。</p>
<p>　Rails2以前の場合は自分でヘッダに値を設定する必要があり、Rails3以降は設定を変更するだけでいけるようです。</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># Rails2以前</span>
<span style="color:#9966CC; font-weight:bold;">def</span> download
  <span style="color:#008000; font-style:italic;"># 認証処理などを挟むことができます。  </span>
  path = <span style="color:#996600;">&quot;/bigfiles/long_movie.mp4&quot;</span>
  response.<span style="color:#9900CC;">headers</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'X-Accel-Redirect'</span><span style="color:#006600; font-weight:bold;">&#93;</span> = path
  response.<span style="color:#9900CC;">headers</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'Content-Type'</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">&quot;application/force-download&quot;</span> 
  response.<span style="color:#9900CC;">headers</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'Content-Disposition'</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">&quot;attachment; filename=<span style="color:#000099;">\&quot;</span>#{File.basename(path)}<span style="color:#000099;">\&quot;</span>&quot;</span> 
  response.<span style="color:#9900CC;">headers</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'Content-length'</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#5A0A0A; font-weight:bold;">size</span><span style="color:#006600; font-weight:bold;">&#40;</span>RAILS_ROOT <span style="color:#006600; font-weight:bold;">+</span> path<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#5A0A0A; font-weight:bold;">render</span> <span style="color:#ff3333; font-weight:bold;">:nothing</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Rails3</span>
<span style="color:#9966CC; font-weight:bold;">def</span> download
  <span style="color:#008000; font-style:italic;"># 認証処理などを挟むことができます。  </span>
  path = <span style="color:#996600;">&quot;/bigfiles/long_movie.mp4&quot;</span>
  <span style="color:#5A0A0A; font-weight:bold;">send_file</span> path, 
             <span style="color:#ff3333; font-weight:bold;">:type</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;application/force-download&quot;</span>,
             <span style="color:#ff3333; font-weight:bold;">:disposition</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;attachment; filename=<span style="color:#000099;">\&quot;</span>#{File.basename(path)}<span style="color:#000099;">\&quot;</span>&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># config/environments/production.rb</span>
<span style="color:#008000; font-style:italic;"># 下記行をコメントアウト (Apache, lighttpd の場合はこの行を使う)</span>
<span style="color:#008000; font-style:italic;"># config.action_dispatch.x_sendfile_header = &quot;X-Sendfile&quot;</span>
<span style="color:#008000; font-style:italic;"># 下記の行を有効にする (nginxの場合はこの行を使う)</span>
config.<span style="color:#9900CC;">action_dispatch</span>.<span style="color:#9900CC;">x_sendfile_header</span> = <span style="color:#996600;">'X-Accel-Redirect'</span></pre></div></div>

<pre lang="">location / {
    client_max_body_size    200m;
    proxy_read_timeout 60;
    proxy_connect_timeout 60;
    proxy_pass http://railsapp;
}

location /bigfiles {
    # -> /path/to/railsroot/bigfiles
    root /path/to/railsroot/;
    internal;
}</pre>
<p>gist に上げてあります。<a href="https://gist.github.com/708959" target="_blank">https://gist.github.com/708959</a></p>
<h3>補足</h3>
<p>　逆に、ちょっとしたアプリを動かすときに、いちいち nginx の設定とかせずに Unicorn とかから直接ファイルを送りたい場合もあります。Rails3になってから、config/environments/productioin.rb がデフォルトのままだと send_file すると 0bytes になってしまいます。</p>
<p>　下記の2行をコメントアウトしておくと、Unicorn なりで直接ファイル送信ができるようになります。</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># config.action_dispatch.x_sendfile_header = &quot;X-Sendfile&quot;</span>
<span style="color:#008000; font-style:italic;"># config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect'</span></pre></div></div>

<p>　ついでに、静的ファイルも Unicorn なりで表示したいような手抜き構成のときは、下記の設定もお忘れなく。</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">config.<span style="color:#9900CC;">serve_static_assets</span> = <span style="color:#0000FF; font-weight:bold;">false</span></pre></div></div>

<div class="addthis_toolbox addthis_default_style " addthis:url='http://ecpplus.net/weblog/nginx-%e3%81%ae-x-accel-redirect-%e3%82%92-rails-%e3%81%a7%e4%bd%bf%e3%81%86/' addthis:title='Nginx の X-Accel-Redirect を Rails で使う '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>]]></content:encoded>
			<wfw:commentRss>http://ecpplus.net/weblog/nginx-%e3%81%ae-x-accel-redirect-%e3%82%92-rails-%e3%81%a7%e4%bd%bf%e3%81%86/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>accepts_nested_attributes_for のメモ</title>
		<link>http://ecpplus.net/weblog/accepts_nested_attributes_for-%e3%81%ae%e3%83%a1%e3%83%a2/</link>
		<comments>http://ecpplus.net/weblog/accepts_nested_attributes_for-%e3%81%ae%e3%83%a1%e3%83%a2/#comments</comments>
		<pubDate>Sat, 20 Nov 2010 14:18:36 +0000</pubDate>
		<dc:creator>ちゅう</dc:creator>
				<category><![CDATA[1]]></category>
		<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://ecpplus.net/weblog/?p=1078</guid>
		<description><![CDATA[　忘れやすいので、 accepts_nested_attributes_for の使い方についてメモ。 　Company に複数の Employees がいるとき。 class Company has_many :employees accepts_nested_attributes_for :employees end &#160; class Employee belongs_to :company end View で、下記のように fields_for を使う。Company作成時に、1人の Employee を同時に作る時の例 &#60; %= form_for @company do &#124;f&#124; %&#62; name &#60; %= f.text_field :name %&#62;&#60;!-- Company の name --&#62; &#60; %= f.fields_for :employees do &#124;employee_form&#124; %&#62; employees name %lt;%= employee_form.text_field :name [...]<div class="addthis_toolbox addthis_default_style " addthis:url='http://ecpplus.net/weblog/accepts_nested_attributes_for-%e3%81%ae%e3%83%a1%e3%83%a2/' addthis:title='accepts_nested_attributes_for のメモ '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>]]></description>
			<content:encoded><![CDATA[<p>　忘れやすいので、 accepts_nested_attributes_for の使い方についてメモ。</p>
<p>　Company に複数の Employees がいるとき。</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Company
has_many <span style="color:#ff3333; font-weight:bold;">:employees</span>
accepts_nested_attributes_for <span style="color:#ff3333; font-weight:bold;">:employees</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> Employee
  belongs_to <span style="color:#ff3333; font-weight:bold;">:company</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>View で、下記のように fields_for を使う。Company作成時に、1人の Employee  を同時に作る時の例</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt; %= form_for @company do |f| %&gt;
  name &lt; %= f.text_field :name %&gt;&lt;!-- Company の name --&gt;
  &lt; %= f.fields_for :employees do |employee_form| %&gt;
    employees name %lt;%= employee_form.text_field :name %&gt;&lt;!-- Employee の name --&gt;
  &lt; % end  %&gt;
  &lt; %= f.submit &quot;Create&quot; %&gt;
&lt; % end %&gt;</pre></div></div>

<p>コントローラでは、特に何もしなくていいです。</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> create
  <span style="color:#0066ff; font-weight:bold;">@company</span> = Company.<span style="color:#9900CC;">create</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;">:company</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  redirect_to <span style="color:#0066ff; font-weight:bold;">@company</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<div class="addthis_toolbox addthis_default_style " addthis:url='http://ecpplus.net/weblog/accepts_nested_attributes_for-%e3%81%ae%e3%83%a1%e3%83%a2/' addthis:title='accepts_nested_attributes_for のメモ '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>]]></content:encoded>
			<wfw:commentRss>http://ecpplus.net/weblog/accepts_nested_attributes_for-%e3%81%ae%e3%83%a1%e3%83%a2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rails の Dynamic Scopes での delete_all は外部キーを NULL にするだけ</title>
		<link>http://ecpplus.net/weblog/rails-%e3%81%ae-dynamic-scopes-%e3%81%a7%e3%81%ae-delete_all-%e3%81%af%e5%a4%96%e9%83%a8%e3%82%ad%e3%83%bc%e3%82%92-null-%e3%81%ab%e3%81%99%e3%82%8b%e3%81%a0%e3%81%91/</link>
		<comments>http://ecpplus.net/weblog/rails-%e3%81%ae-dynamic-scopes-%e3%81%a7%e3%81%ae-delete_all-%e3%81%af%e5%a4%96%e9%83%a8%e3%82%ad%e3%83%bc%e3%82%92-null-%e3%81%ab%e3%81%99%e3%82%8b%e3%81%a0%e3%81%91/#comments</comments>
		<pubDate>Tue, 23 Feb 2010 08:31:43 +0000</pubDate>
		<dc:creator>ちゅう</dc:creator>
				<category><![CDATA[1]]></category>
		<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://ecpplus.net/weblog/?p=918</guid>
		<description><![CDATA[Shop has_many Items という状況のとき、とある shop が持っている items を、下記のように取ることが出来ます。それに対して、条件を設定することも出来ます。 &#62;&#62; @shop.items =&#62; &#91;Item id:21 shop_id: 1, Item id: 22, shop_id: 1, ....&#93; &#62;&#62; @shop.item.find_all_by_category&#40;&#34;CPU&#34;&#41; =&#62; &#91;Item id:23 shop_id: 1, Item id: 26, shop_id: 1, ....&#93; そこで、同様にとある Shop の Items を全部 delete_all にしようとしたのですが、DELETE されるのではなく、外部キーにNULLがセットされるという挙動になっています。 &#62;&#62; @shop.items.delete_all とすると Item UPDATE &#40;3.0ms&#41; UPDATE `items` SET shop_id = NULL WHERE [...]<div class="addthis_toolbox addthis_default_style " addthis:url='http://ecpplus.net/weblog/rails-%e3%81%ae-dynamic-scopes-%e3%81%a7%e3%81%ae-delete_all-%e3%81%af%e5%a4%96%e9%83%a8%e3%82%ad%e3%83%bc%e3%82%92-null-%e3%81%ab%e3%81%99%e3%82%8b%e3%81%a0%e3%81%91/' addthis:title='Rails の Dynamic Scopes での delete_all は外部キーを NULL にするだけ '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>]]></description>
			<content:encoded><![CDATA[<p>Shop has_many Items という状況のとき、とある shop が持っている items を、下記のように取ることが出来ます。それに対して、条件を設定することも出来ます。</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&gt;&gt;</span> <span style="color:#0066ff; font-weight:bold;">@shop</span>.<span style="color:#9900CC;">items</span> 
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span>Item id:<span style="color:#006666;">21</span> shop_id: <span style="color:#006666;">1</span>, Item id: <span style="color:#006666;">22</span>, shop_id: <span style="color:#006666;">1</span>, ....<span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#006600; font-weight:bold;">&gt;&gt;</span> <span style="color:#0066ff; font-weight:bold;">@shop</span>.<span style="color:#9900CC;">item</span>.<span style="color:#9900CC;">find_all_by_category</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;CPU&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> 
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span>Item id:<span style="color:#006666;">23</span> shop_id: <span style="color:#006666;">1</span>, Item id: <span style="color:#006666;">26</span>, shop_id: <span style="color:#006666;">1</span>, ....<span style="color:#006600; font-weight:bold;">&#93;</span></pre></div></div>

<p>そこで、同様にとある Shop の Items を全部 delete_all にしようとしたのですが、DELETE されるのではなく、外部キーにNULLがセットされるという挙動になっています。</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&gt;&gt;</span> <span style="color:#0066ff; font-weight:bold;">@shop</span>.<span style="color:#9900CC;">items</span>.<span style="color:#9900CC;">delete_all</span></pre></div></div>

<p>とすると</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">  Item <span style="color: #993333; font-weight: bold;">UPDATE</span> <span style="color: #66cc66;">&#40;</span>3<span style="color: #66cc66;">.</span>0ms<span style="color: #66cc66;">&#41;</span>   <span style="color: #993333; font-weight: bold;">UPDATE</span> <span style="color: #ff0000;">`items`</span> <span style="color: #993333; font-weight: bold;">SET</span> shop_id <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">WHERE</span> <span style="color: #66cc66;">&#40;</span>shop_id <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span> <span style="color: #993333; font-weight: bold;">AND</span> id <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">22</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">23</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">24</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">25</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">26</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>のようになります。</p>
<p>Rails の API を見ると</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># File vendor/rails/activerecord/lib/active_record/base.rb, line 897</span>
<span style="color:#006666;">897</span>:       <span style="color:#9966CC; font-weight:bold;">def</span> delete_all<span style="color:#006600; font-weight:bold;">&#40;</span>conditions = <span style="color:#0000FF; font-weight:bold;">nil</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#006666;">898</span>:         sql = <span style="color:#996600;">&quot;DELETE FROM #{quoted_table_name} &quot;</span>
<span style="color:#006666;">899</span>:         add_conditions!<span style="color:#006600; font-weight:bold;">&#40;</span>sql, conditions, scope<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:find</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#006666;">900</span>:         connection.<span style="color:#9900CC;">delete</span><span style="color:#006600; font-weight:bold;">&#40;</span>sql, <span style="color:#996600;">&quot;#{name} Delete all&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#006666;">901</span>:       <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>みたいになっているのですが、Dynamic Scopes はまた別のところで定義されているっぽいです。</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&gt;&gt;</span> <span style="color:#0066ff; font-weight:bold;">@shop</span>.<span style="color:#9900CC;">items</span>.<span style="color:#9900CC;">destroy_all</span></pre></div></div>

<p>としてみると、下記のように DELETE になりますが、SQL が発行されまくってるのでだめぽいです。そもそも destroy は、destroy をトリガーにしている各種処理をしてから DELETE が行われるので、そもそも delete とは意味合いが違うというので仕方ないです。</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">Item Destroy <span style="color: #66cc66;">&#40;</span>3<span style="color: #66cc66;">.</span>0ms<span style="color: #66cc66;">&#41;</span>   <span style="color: #993333; font-weight: bold;">DELETE</span> <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #ff0000;">`items`</span> <span style="color: #993333; font-weight: bold;">WHERE</span> <span style="color: #993333; font-weight: bold;">WHERE</span> id <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">21</span>
Item Destroy <span style="color: #66cc66;">&#40;</span>3<span style="color: #66cc66;">.</span>0ms<span style="color: #66cc66;">&#41;</span>   <span style="color: #993333; font-weight: bold;">DELETE</span> <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #ff0000;">`items`</span> <span style="color: #993333; font-weight: bold;">WHERE</span> <span style="color: #993333; font-weight: bold;">WHERE</span> id <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">22</span>
Item Destroy <span style="color: #66cc66;">&#40;</span>3<span style="color: #66cc66;">.</span>0ms<span style="color: #66cc66;">&#41;</span>   <span style="color: #993333; font-weight: bold;">DELETE</span> <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #ff0000;">`items`</span> <span style="color: #993333; font-weight: bold;">WHERE</span> <span style="color: #993333; font-weight: bold;">WHERE</span> id <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">23</span></pre></div></div>

<p>ふつうに Item.delete_all を呼ぶかなぁ</p>
<div class="addthis_toolbox addthis_default_style " addthis:url='http://ecpplus.net/weblog/rails-%e3%81%ae-dynamic-scopes-%e3%81%a7%e3%81%ae-delete_all-%e3%81%af%e5%a4%96%e9%83%a8%e3%82%ad%e3%83%bc%e3%82%92-null-%e3%81%ab%e3%81%99%e3%82%8b%e3%81%a0%e3%81%91/' addthis:title='Rails の Dynamic Scopes での delete_all は外部キーを NULL にするだけ '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>]]></content:encoded>
			<wfw:commentRss>http://ecpplus.net/weblog/rails-%e3%81%ae-dynamic-scopes-%e3%81%a7%e3%81%ae-delete_all-%e3%81%af%e5%a4%96%e9%83%a8%e3%82%ad%e3%83%bc%e3%82%92-null-%e3%81%ab%e3%81%99%e3%82%8b%e3%81%a0%e3%81%91/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Twitterで食べたものを収集するbot作った</title>
		<link>http://ecpplus.net/weblog/twitter%e3%81%a7%e9%a3%9f%e3%81%b9%e3%81%9f%e3%82%82%e3%81%ae%e3%82%92%e5%8f%8e%e9%9b%86%e3%81%99%e3%82%8bbot%e4%bd%9c%e3%81%a3%e3%81%9f/</link>
		<comments>http://ecpplus.net/weblog/twitter%e3%81%a7%e9%a3%9f%e3%81%b9%e3%81%9f%e3%82%82%e3%81%ae%e3%82%92%e5%8f%8e%e9%9b%86%e3%81%99%e3%82%8bbot%e4%bd%9c%e3%81%a3%e3%81%9f/#comments</comments>
		<pubDate>Mon, 14 Dec 2009 05:06:10 +0000</pubDate>
		<dc:creator>ちゅう</dc:creator>
				<category><![CDATA[1]]></category>
		<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://ecpplus.net/weblog/?p=909</guid>
		<description><![CDATA[ようやく最近になって Twitter いいなとか思ってるんですが、毎日自炊してると食べたものを記録したくなって、Twitter から自動的に拾ってくるようなアプリを作ってみた。 食べたものログ(仮) で、@eat_log をフォローすると、フォローしかえされてそこから収集が始まる感じです。それと、よく食べるものを集計して出すっていう機能もついてます。 実装に使ったのは、Rubyの twitter のライブラリと、Yahoo! テキスト解析のキーフレーズ抽出 です。 Yahoo! テキスト解析は、JSONでレスポンス返してくれたりで、すごく簡単に使えます。ruby twitter のドキュメント を見れば、使い方はだいたい分かります。OAuth 認証も簡単でいいです。<div class="addthis_toolbox addthis_default_style " addthis:url='http://ecpplus.net/weblog/twitter%e3%81%a7%e9%a3%9f%e3%81%b9%e3%81%9f%e3%82%82%e3%81%ae%e3%82%92%e5%8f%8e%e9%9b%86%e3%81%99%e3%82%8bbot%e4%bd%9c%e3%81%a3%e3%81%9f/' addthis:title='Twitterで食べたものを収集するbot作った '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>]]></description>
			<content:encoded><![CDATA[<p>ようやく最近になって Twitter  いいなとか思ってるんですが、毎日自炊してると食べたものを記録したくなって、Twitter から自動的に拾ってくるようなアプリを作ってみた。</p>
<p><a href="http://eat.ecpplus.net/" target="_blank">食べたものログ(仮)</a> で、<a href="http://twitter.com/eat_log" target="_blank">@eat_log</a> をフォローすると、フォローしかえされてそこから収集が始まる感じです。それと、よく食べるものを集計して出すっていう機能もついてます。</p>
<p>実装に使ったのは、Rubyの <a href="http://twitter.rubyforge.org/" target="_blank">twitter</a> のライブラリと、<a href="http://developer.yahoo.co.jp/webapi/jlp/keyphrase/v1/extract.html" target="_blank">Yahoo! テキスト解析のキーフレーズ抽出</a> です。</p>
<p>Yahoo! テキスト解析は、JSONでレスポンス返してくれたりで、すごく簡単に使えます。<a href="http://twitter.rubyforge.org/twitter/">ruby twitter のドキュメント</a> を見れば、使い方はだいたい分かります。OAuth 認証も簡単でいいです。</p>
<div class="addthis_toolbox addthis_default_style " addthis:url='http://ecpplus.net/weblog/twitter%e3%81%a7%e9%a3%9f%e3%81%b9%e3%81%9f%e3%82%82%e3%81%ae%e3%82%92%e5%8f%8e%e9%9b%86%e3%81%99%e3%82%8bbot%e4%bd%9c%e3%81%a3%e3%81%9f/' addthis:title='Twitterで食べたものを収集するbot作った '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>]]></content:encoded>
			<wfw:commentRss>http://ecpplus.net/weblog/twitter%e3%81%a7%e9%a3%9f%e3%81%b9%e3%81%9f%e3%82%82%e3%81%ae%e3%82%92%e5%8f%8e%e9%9b%86%e3%81%99%e3%82%8bbot%e4%bd%9c%e3%81%a3%e3%81%9f/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

