-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
569 lines (359 loc) · 30.5 KB
/
index.html
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
<!DOCTYPE html>
<!--[if IEMobile 7 ]><html class="no-js iem7"><![endif]-->
<!--[if lt IE 9]><html class="no-js lte-ie8"><![endif]-->
<!--[if (gt IE 8)|(gt IEMobile 7)|!(IEMobile)|!(IE)]><!--><html class="no-js" lang="en"><!--<![endif]-->
<head>
<meta charset="utf-8">
<title>ねこじゃらし開発部ブログ</title>
<meta name="author" content="Nekojarashi Inc.">
<meta name="description" content="Backbone.Marionetteは Backbone.js用のライブラリです。
Backboneはその名の通りアプリケーションの背骨にあたる部分しか提供しません。
これによって開発者はBackboneの土台の上に自由にアプリを設計できるのですが、 …">
<!-- http://t.co/dKP3o1e -->
<meta name="HandheldFriendly" content="True">
<meta name="MobileOptimized" content="320">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="canonical" href="http://nekojarashi.github.io">
<link href="/favicon.png" rel="icon">
<link href="/stylesheets/screen.css" media="screen, projection" rel="stylesheet" type="text/css">
<link href="/atom.xml" rel="alternate" title="ねこじゃらし開発部ブログ" type="application/atom+xml">
<script src="/javascripts/modernizr-2.0.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>!window.jQuery && document.write(unescape('%3Cscript src="./javascripts/lib/jquery.min.js"%3E%3C/script%3E'))</script>
<script src="/javascripts/octopress.js" type="text/javascript"></script>
<!--Fonts from Google"s Web font directory at http://google.com/webfonts -->
<link href="http://fonts.googleapis.com/css?family=PT+Serif:regular,italic,bold,bolditalic" rel="stylesheet" type="text/css">
<link href="http://fonts.googleapis.com/css?family=PT+Sans:regular,italic,bold,bolditalic" rel="stylesheet" type="text/css">
</head>
<body >
<header role="banner"><hgroup>
<h1><a href="/">ねこじゃらし開発部ブログ</a></h1>
</hgroup>
</header>
<nav role="navigation"><ul class="subscription" data-subscription="rss">
<li><a href="/atom.xml" rel="subscribe-rss" title="subscribe via RSS">RSS</a></li>
</ul>
<form action="http://google.com/search" method="get">
<fieldset role="search">
<input type="hidden" name="q" value="site:nekojarashi.github.io" />
<input class="search" type="text" name="q" results="0" placeholder="Search"/>
</fieldset>
</form>
</nav>
<div id="main">
<div id="content">
<div class="blog-index">
<article>
<header>
<h1 class="entry-title"><a href="/blog/2013/11/27/marionette-dot-js/">Backbone.Marionette</a></h1>
<p class="meta">
<time datetime="2013-11-27T11:20:00+09:00" pubdate data-updated="true">2013年11月27日 11:20</time>
</p>
</header>
<div class="entry-content"><p><a href="http://marionettejs.com/">Backbone.Marionette</a>は Backbone.js用のライブラリです。
Backboneはその名の通りアプリケーションの背骨にあたる部分しか提供しません。
これによって開発者はBackboneの土台の上に自由にアプリを設計できるのですが、典型的なWebアプリ開発では多くの部分で同じようなコードを使いまわすケースが多く、規模が大きくなるとDRYに保つためにそれぞれをまとめる必要がでてきます。
Marionetteはこうした”Boilerplate”なコードの雛形を提供しますので、開発のスピードアップやコードの共通化に役立てることができます。</p>
<p><strong> Backbone.Marionette.ItemView </strong></p>
<p><code>ItemView</code>はBackbone.Viewの拡張です。Backbone.ViewがマリオだとしたらBackbone.Marionette.ItemViewはファイヤーマリオです。いくつかの典型的なプロセスをよしなにやってくれているのでコード量を減らすことができます。そのいくつかのプロセスとは、ざっくり言うと下記の3つです。</p>
<ul>
<li>テンプレートエンジンにモデルを埋め込んでDOMを構築する</li>
<li>構築したDOMを表示する</li>
<li>使われなくなったらゾンビ化しないようにしてくれる</li>
</ul>
<p>Backboneに限らず、そもそも「ビュー」というものが提供する典型的な役割とは、「モデル」という概念的なデータを現実世界に表現することです。(シャレではありませんが)ファッションでいうといろんなモデルさんに服を着せ、髪型をととのえ、ステージにたたせるスタイリストさんのような感じでしょうか。Backboneでの具体的なプロセスでいうと、「扱うモデルを決めてそのイベントを購読」し、「テンプレートエンジンにモデルのデータを埋め込み」、最後に「それを表示する」ということになります。<code>ItemView</code>は、この一連の流れがあらかじめ定義されていますので、開発者がすることはただ<code>ItemView</code>を作るだけになります。…これは具体例を見たほうがはやそうですね。つまり、</p>
<p>こんな感じのが</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="kd">var</span> <span class="nx">UserView</span> <span class="o">=</span> <span class="nx">Backbone</span><span class="p">.</span><span class="nx">View</span><span class="p">.</span><span class="nx">extend</span><span class="p">({</span>
</span><span class='line'> <span class="nx">template</span><span class="o">:</span> <span class="nx">$</span><span class="p">(</span><span class="s1">'#user-template'</span><span class="p">);</span>
</span><span class='line'> <span class="nx">render</span><span class="o">:</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'> <span class="kd">var</span> <span class="nx">compiledTemplate</span> <span class="o">=</span> <span class="nx">_</span><span class="p">.</span><span class="nx">template</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">template</span><span class="p">.</span><span class="nx">html</span><span class="p">());</span>
</span><span class='line'> <span class="kd">var</span> <span class="nx">user</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">model</span><span class="p">.</span><span class="nx">toJSON</span><span class="p">();</span>
</span><span class='line'> <span class="kd">var</span> <span class="nx">html</span> <span class="o">=</span> <span class="nx">compiledTemplate</span><span class="p">(</span><span class="nx">user</span><span class="p">);</span>
</span><span class='line'> <span class="k">this</span><span class="p">.</span><span class="nx">$el</span><span class="p">.</span><span class="nx">html</span><span class="p">(</span><span class="nx">html</span><span class="p">);</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'><span class="p">})</span>
</span></code></pre></td></tr></table></div></figure>
<p>こうなります。</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="kd">var</span> <span class="nx">UserView</span> <span class="o">=</span> <span class="nx">Backbone</span><span class="p">.</span><span class="nx">Marionette</span><span class="p">.</span><span class="nx">ItemView</span><span class="p">.</span><span class="nx">extend</span><span class="p">({</span>
</span><span class='line'> <span class="nx">template</span><span class="o">:</span> <span class="s1">'#user-template'</span>
</span><span class='line'><span class="p">});</span>
</span></code></pre></td></tr></table></div></figure>
<p>CoffeeScriptだとこんな感じかな</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='coffeescript'><span class='line'><span class="nv">UserView = </span><span class="nx">Backbone</span><span class="p">.</span><span class="nx">View</span><span class="p">.</span><span class="nx">extend</span><span class="p">(</span>
</span><span class='line'> <span class="nv">template: </span><span class="nx">$</span><span class="p">(</span><span class="s">'#user-template'</span><span class="p">)</span>
</span><span class='line'> <span class="nv">render: </span><span class="nf">-></span>
</span><span class='line'> <span class="nv">compiledTemplate = </span><span class="nx">_</span><span class="p">.</span><span class="nx">template</span><span class="p">(</span><span class="nx">@template</span><span class="p">.</span><span class="nx">html</span><span class="p">())</span>
</span><span class='line'> <span class="nv">user = </span><span class="nx">@model</span><span class="p">.</span><span class="nx">toJSON</span><span class="p">()</span>
</span><span class='line'> <span class="nv">html = </span><span class="nx">compiledTemplate</span><span class="p">(</span><span class="nx">user</span><span class="p">)</span>
</span><span class='line'> <span class="nx">@$el</span><span class="p">.</span><span class="nx">html</span> <span class="nx">html</span>
</span><span class='line'><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>
<p>↓</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='coffeescript'><span class='line'><span class="nv">UserView = </span><span class="nx">Backbone</span><span class="p">.</span><span class="nx">Marionette</span><span class="p">.</span><span class="nx">ItemView</span><span class="p">.</span><span class="nx">extend</span><span class="p">(</span><span class="nv">template: </span><span class="s">'#user-template'</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>
<p>うーんなかなかスッキリしたんじゃないでしょうか。</p>
<p><strong> Backbone.Marionette.Region </strong></p>
<p><strong> Backbone.Marionette.Layout </strong></p>
<p><code>Layout</code>は<code>ItemView</code>に<code>Region</code>管理機能がついたものです。以上。</p>
</div>
</article>
<article>
<header>
<h1 class="entry-title"><a href="/blog/2013/08/20/acts-as-paranoid/">Acts_as_paranoid</a></h1>
<p class="meta">
<time datetime="2013-08-20T11:19:00+09:00" pubdate data-updated="true">2013年08月20日 11:19</time>
</p>
</header>
<div class="entry-content"><p>CoreDrive開発チームの原です。</p>
<p>今回、論理削除されたレコードをデフォルトで取得しない <code>acts_as_paranoid</code> を導入しました。まずは一般的な使い方から。</p>
<p><strong> 1.Gem を追加 </strong></p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">gem</span> <span class="s2">"acts_as_paranoid"</span><span class="p">,</span> <span class="s2">"~>0.4.0"</span> <span class="c1"># Rails 3.2 の場合</span>
</span></code></pre></td></tr></table></div></figure>
<p><strong> 2.Gem をインストール </strong></p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">bundle</span> <span class="n">install</span>
</span></code></pre></td></tr></table></div></figure>
<p><strong> 3.論理削除するテーブルに <code>deleted_at</code> カラム(datetime型)を追加し、マイグレーションを実行する。 </strong></p>
<p>例:Postモデルに追加する場合</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">rails</span> <span class="n">generate</span> <span class="n">migration</span> <span class="no">AddDeletedAtToPosts</span> <span class="n">deleted_at</span><span class="ss">:datetime</span>
</span></code></pre></td></tr></table></div></figure>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">rake</span> <span class="ss">db</span><span class="p">:</span><span class="n">migrate</span>
</span></code></pre></td></tr></table></div></figure>
<p><strong> 4.対応するモデルに acts_as_paranoid を宣言する </strong></p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">Post</span> <span class="o"><</span> <span class="ss">ActiveRecord</span><span class="p">:</span><span class="ss">:Base</span>
</span><span class='line'> <span class="n">acts_as_paranoid</span>
</span><span class='line'>
</span><span class='line'> <span class="o">.</span><span class="n">.</span><span class="o">.</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
<p><strong> 5.この時点で、クエリのSQLにデフォルトで <code>deleted_at is NULL</code>が自動的に挿入される。コンソールを立ち上げて、Post.all を実行してSQLを確認してみる。 </strong></p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'> <span class="no">Post</span><span class="o">.</span><span class="n">all</span>
</span><span class='line'> <span class="err">⇒</span> <span class="no">SELECT</span> <span class="s2">"posts"</span><span class="o">.</span><span class="n">*</span> <span class="no">FROM</span> <span class="s2">"posts"</span> <span class="no">WHERE</span> <span class="p">(</span><span class="s2">"posts"</span><span class="o">.</span><span class="s2">"deleted_at"</span> <span class="no">IS</span> <span class="no">NULL</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>
<p><strong> 6.destroy を呼ぶと、レコードは削除されずに <code>deleted_at</code> に値が設定される。 </strong></p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'> <span class="n">post</span> <span class="o">=</span> <span class="no">Post</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
</span><span class='line'> <span class="n">post</span><span class="o">.</span><span class="n">destroy</span>
</span><span class='line'> <span class="err">⇒</span> <span class="no">UPDATE</span> <span class="s2">"posts"</span> <span class="no">SET</span> <span class="n">deleted_at</span> <span class="o">=</span> <span class="s1">'2013-08-20 03:47:01.506771'</span> <span class="no">WHERE</span> <span class="s2">"posts"</span><span class="o">.</span><span class="s2">"id"</span> <span class="o">=</span> <span class="mi">1</span> <span class="no">AND</span> <span class="p">(</span><span class="s2">"posts"</span><span class="o">.</span><span class="s2">"deleted_at"</span> <span class="no">IS</span> <span class="no">NULL</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>
<h2>各種メソッド</h2>
<ul>
<li><p>only_deleted<br>
deleted_at が入っているレコードを検索する。</p></li>
<li><p>with_deleted / unscoped<br>
すべてのレコードを検索する。</p></li>
<li><p>destroy! / delete_all!(conditions)<br>
レコードを削除する。</p></li>
</ul>
<h2>オプション</h2>
<ul>
<li>:column <br>
deleted_at 以外のカラムを論理削除の対象とする場合に使用する。</li>
</ul>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'> <span class="n">acts_as_paranoid</span> <span class="ss">:column</span> <span class="o">=></span> <span class="s1">'カラム名'</span>
</span></code></pre></td></tr></table></div></figure>
<h2>関連付けされたモデルの場合</h2>
<p>今回はこれにちょっとはまりました。GitHubに書いてある通りに記述しても正しく動いてくれなくて。とりあえず、以下の場合を想定します。</p>
<p>・PostモデルにCommentモデルが関連づけられている。</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">Post</span> <span class="o"><</span> <span class="ss">ActiveRecord</span><span class="p">:</span><span class="ss">:Base</span>
</span><span class='line'> <span class="n">has_many</span> <span class="ss">:comments</span>
</span><span class='line'> <span class="n">acts_as_paranoid</span>
</span><span class='line'> <span class="o">.</span><span class="n">.</span><span class="o">.</span>
</span><span class='line'>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">Comment</span> <span class="o"><</span> <span class="ss">ActiveRecord</span><span class="p">:</span><span class="ss">:Base</span>
</span><span class='line'> <span class="n">belongs_to</span> <span class="ss">:post</span>
</span><span class='line'> <span class="n">acts_as_paranoid</span>
</span><span class='line'> <span class="o">.</span><span class="n">.</span><span class="o">.</span>
</span><span class='line'>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
<p>このままPostのインスタンスに紐づくCommentを取得しようとすると、deleted_at が入っていないCommentを取得しようとします。</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'> <span class="n">post</span> <span class="o">=</span> <span class="no">Post</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
</span><span class='line'> <span class="n">post</span><span class="o">.</span><span class="n">comments</span>
</span><span class='line'> <span class="err">⇒</span> <span class="no">SELECT</span> <span class="s2">"comments"</span><span class="o">.</span><span class="n">*</span> <span class="no">FROM</span> <span class="s2">"comments"</span> <span class="no">WHERE</span> <span class="s2">"comments"</span><span class="o">.</span><span class="s2">"post_id"</span> <span class="o">=</span> <span class="mi">2</span> <span class="no">AND</span> <span class="p">(</span><span class="s2">"comments"</span><span class="o">.</span><span class="s2">"deleted_at"</span> <span class="no">IS</span> <span class="no">NULL</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>
<p>Post に紐づく Comment で、<code>deleted_at</code> が付いているレコードを取得するには、上記の各種メソッドで紹介した <code>with_deleted</code> または <code>unscoped</code> を使用します。</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'> <span class="n">post</span> <span class="o">=</span> <span class="no">Post</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
</span><span class='line'> <span class="n">post</span><span class="o">.</span><span class="n">comments</span><span class="o">.</span><span class="n">with_deleted</span>
</span><span class='line'> <span class="err">⇒</span> <span class="no">SELECT</span> <span class="s2">"comments"</span><span class="o">.</span><span class="n">*</span> <span class="no">FROM</span> <span class="s2">"comments"</span> <span class="no">WHERE</span> <span class="s2">"comments"</span><span class="o">.</span><span class="s2">"post_id"</span> <span class="o">=</span> <span class="mi">3</span>
</span></code></pre></td></tr></table></div></figure>
<p>さて、上記の各種メソッドは、<code>post.comments</code> のような配列のクラスには使えますが、<code>comment.post</code>のような場合に使うことができません。<code>comment.post</code> で <code>deleted_at</code> が入った Post を取得したい場合は、CommentモデルにPostモデルとの関連を違う名前で追加します。</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">Comment</span> <span class="o"><</span> <span class="ss">ActiveRecord</span><span class="p">:</span><span class="ss">:Base</span>
</span><span class='line'> <span class="n">belongs_to</span> <span class="ss">:post</span>
</span><span class='line'> <span class="n">belongs_to</span> <span class="ss">:post_including_deleted</span><span class="p">,</span> <span class="ss">:class_name</span> <span class="o">=></span> <span class="s2">"Post"</span><span class="p">,</span> <span class="ss">:with_deleted</span> <span class="o">=></span> <span class="kp">true</span><span class="p">,</span> <span class="ss">:foreign_key</span> <span class="o">=></span> <span class="ss">:post_id</span>
</span><span class='line'>
</span><span class='line'> <span class="n">acts_as_paranoid</span>
</span><span class='line'> <span class="o">.</span><span class="n">.</span><span class="o">.</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
<p>※ 関連名に *_with_deleted は使えないようです。</p>
<p>:foreign_key を指定しないと結果が全部 nil になって返ってきます。この部分が GitHub のサンプルコードになかったので原因を探すのに手間取りました。</p>
<p>それでは、追加したメソッドでCommentを取得してみます。</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'> <span class="n">comment</span> <span class="o">=</span> <span class="no">Comment</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
</span><span class='line'> <span class="n">comment</span><span class="o">.</span><span class="n">post_including_deleted</span>
</span><span class='line'> <span class="err">⇒</span> <span class="no">SELECT</span> <span class="s2">"posts"</span><span class="o">.</span><span class="n">*</span> <span class="no">FROM</span> <span class="s2">"posts"</span> <span class="no">WHERE</span> <span class="s2">"posts"</span><span class="o">.</span><span class="s2">"id"</span> <span class="o">=</span> <span class="mi">1</span> <span class="no">LIMIT</span> <span class="mi">1</span>
</span></code></pre></td></tr></table></div></figure>
<p>これで <code>deleted_ at</code> が入ったPostを取得することができました。</p>
</div>
</article>
<article>
<header>
<h1 class="entry-title"><a href="/blog/2013/08/03/blog-hajimemasita/">ブログ始めました</a></h1>
<p class="meta">
<time datetime="2013-08-03T19:04:00+09:00" pubdate data-updated="true">2013年08月03日 19:04</time>
</p>
</header>
<div class="entry-content"><p>こんにちは。ねこじゃらし開発部の川村です。
これから開発を通して得たTipsやノウハウ、開発チームの日常を記していこうと思います。</p>
<p><a href="https://coredrive.com/">https://coredrive.com/</a></p>
</div>
</article>
<div class="pagination">
<a href="/blog/archives">Blog Archives</a>
</div>
</div>
<aside class="sidebar">
<section>
<h1>Recent Posts</h1>
<ul id="recent_posts">
<li class="post">
<a href="/blog/2013/11/27/marionette-dot-js/">Backbone.Marionette</a>
</li>
<li class="post">
<a href="/blog/2013/08/20/acts-as-paranoid/">Acts_as_paranoid</a>
</li>
<li class="post">
<a href="/blog/2013/08/03/blog-hajimemasita/">ブログ始めました</a>
</li>
</ul>
</section>
<section>
<h1>GitHub Repos</h1>
<ul id="gh_repos">
<li class="loading">Status updating…</li>
</ul>
<a href="https://github.com/nekojarashi">@nekojarashi</a> on GitHub
<script type="text/javascript">
$(document).ready(function(){
if (!window.jXHR){
var jxhr = document.createElement('script');
jxhr.type = 'text/javascript';
jxhr.src = '/javascripts/libs/jXHR.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(jxhr, s);
}
github.showRepos({
user: 'nekojarashi',
count: 0,
skip_forks: true,
target: '#gh_repos'
});
});
</script>
<script src="/javascripts/github.js" type="text/javascript"> </script>
</section>
</aside>
</div>
</div>
<footer role="contentinfo"><p class="pull-left">
<a href="/">ねこじゃらし開発部ブログ</a>
- Copyright © 2014 - Nekojarashi Inc.
</p>
<p class="pull-right">
<span>Designed by <a href="http://blog.hyfather.com" target="_blank">Nikhil
Mungel</a>. Based on <a href="https://github.com/sevenadrian/foxslide"
target="_blank">foxslide</a>.</span><br/>
<span>
Powered by <a href="http://octopress.org/" target="_blank">Octopress</a>.
</span>
<span>
Cover photo
by <a href="http://www.flickr.com/photos/69912667@N07/6778147487/"
target="_blank">Carrie Nelson</a>.
</span>
</p>
</footer>
<script type="text/javascript">
(function(){
var twitterWidgets = document.createElement('script');
twitterWidgets.type = 'text/javascript';
twitterWidgets.async = true;
twitterWidgets.src = '//platform.twitter.com/widgets.js';
document.getElementsByTagName('head')[0].appendChild(twitterWidgets);
})();
</script>
<script type="text/javascript">
(function(){
var twitterWidgets = document.createElement('script');
twitterWidgets.type = 'text/javascript';
twitterWidgets.async = true;
twitterWidgets.src = '//platform.twitter.com/widgets.js';
document.getElementsByTagName('head')[0].appendChild(twitterWidgets);
})();
</script>
</body>
</html>