-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathuser-microposts.html
2443 lines (1605 loc) · 242 KB
/
user-microposts.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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<title>user-microposts</title>
<link rel="stylesheet" href="pygments.css" type="text/css" />
<link rel="stylesheet" href="polytexnic.css" type="text/css" />
</head>
<body>
<div id="book">
<h1 class="title">Ruby on Rails Tutorial </h1>
<h1 class="subtitle"> Learn Web Development with Rails</h1>
<h2 class="author">Michael Hartl</h2>
<h2 class="contents">Contents</h2>
<div id="table_of_contents"><ol><li class="chapter"><a href="beginning.html#top"><span class="number">Chapter 1</span> From zero to deploy</a></li><li><ol><li class="section"><a href="beginning.html#sec-introduction"><span class="number">1.1</span> Introduction</a></li><li><ol><li class="subsection"><a href="beginning.html#sec-comments_for_various_readers"><span class="number">1.1.1</span> Comments for various readers</a></li><li class="subsection"><a href="beginning.html#sec-1_1_2"><span class="number">1.1.2</span> “Scaling” Rails</a></li><li class="subsection"><a href="beginning.html#sec-conventions"><span class="number">1.1.3</span> Conventions in this book</a></li></ol></li><li class="section"><a href="beginning.html#sec-up_and_running"><span class="number">1.2</span> Up and running</a></li><li><ol><li class="subsection"><a href="beginning.html#sec-development_tools"><span class="number">1.2.1</span> Development environments</a></li><li><ol><li class="subsubsection"><a href="beginning.html#sec-1_2_1_1">IDEs</a></li><li class="subsubsection"><a href="beginning.html#sec-1_2_1_2">Text editors and command lines</a></li><li class="subsubsection"><a href="beginning.html#sec-1_2_1_3">Browsers</a></li><li class="subsubsection"><a href="beginning.html#sec-1_2_1_4">A note about tools</a></li></ol></li><li class="subsection"><a href="beginning.html#sec-rubygems"><span class="number">1.2.2</span> Ruby, RubyGems, Rails, and Git</a></li><li><ol><li class="subsubsection"><a href="beginning.html#sec-rails_installer_windows">Rails Installer (Windows)</a></li><li class="subsubsection"><a href="beginning.html#sec-install_git">Install Git</a></li><li class="subsubsection"><a href="beginning.html#sec-install_ruby">Install Ruby</a></li><li class="subsubsection"><a href="beginning.html#sec-install_rubygems">Install RubyGems</a></li><li class="subsubsection"><a href="beginning.html#sec-install_rails">Install Rails</a></li></ol></li><li class="subsection"><a href="beginning.html#sec-the_first_application"><span class="number">1.2.3</span> The first application</a></li><li class="subsection"><a href="beginning.html#sec-bundler"><span class="number">1.2.4</span> Bundler</a></li><li class="subsection"><a href="beginning.html#sec-rails_server"><span class="number">1.2.5</span> <tt>rails server</tt></a></li><li class="subsection"><a href="beginning.html#sec-mvc"><span class="number">1.2.6</span> Model-view-controller (MVC)</a></li></ol></li><li class="section"><a href="beginning.html#sec-version_control"><span class="number">1.3</span> Version control with Git</a></li><li><ol><li class="subsection"><a href="beginning.html#sec-git_setup"><span class="number">1.3.1</span> Installation and setup</a></li><li><ol><li class="subsubsection"><a href="beginning.html#sec-1_3_1_1">First-time system setup</a></li><li class="subsubsection"><a href="beginning.html#sec-1_3_1_2">First-time repository setup</a></li></ol></li><li class="subsection"><a href="beginning.html#sec-adding_and_committing"><span class="number">1.3.2</span> Adding and committing</a></li><li class="subsection"><a href="beginning.html#sec-1_3_3"><span class="number">1.3.3</span> What good does Git do you?</a></li><li class="subsection"><a href="beginning.html#sec-github"><span class="number">1.3.4</span> GitHub</a></li><li class="subsection"><a href="beginning.html#sec-git_commands"><span class="number">1.3.5</span> Branch, edit, commit, merge</a></li><li><ol><li class="subsubsection"><a href="beginning.html#sec-git_branch">Branch</a></li><li class="subsubsection"><a href="beginning.html#sec-git_edit">Edit</a></li><li class="subsubsection"><a href="beginning.html#sec-git_commit">Commit</a></li><li class="subsubsection"><a href="beginning.html#sec-git_merge">Merge</a></li><li class="subsubsection"><a href="beginning.html#sec-git_push">Push</a></li></ol></li></ol></li><li class="section"><a href="beginning.html#sec-deploying"><span class="number">1.4</span> Deploying</a></li><li><ol><li class="subsection"><a href="beginning.html#sec-heroku_setup"><span class="number">1.4.1</span> Heroku setup</a></li><li class="subsection"><a href="beginning.html#sec-heroku_step_one"><span class="number">1.4.2</span> Heroku deployment, step one</a></li><li class="subsection"><a href="beginning.html#sec-1_4_3"><span class="number">1.4.3</span> Heroku deployment, step two</a></li><li class="subsection"><a href="beginning.html#sec-heroku_commands"><span class="number">1.4.4</span> Heroku commands</a></li></ol></li><li class="section"><a href="beginning.html#sec-beginning_conclusion"><span class="number">1.5</span> Conclusion</a></li></ol></li><li class="chapter"><a href="a-demo-app.html#top"><span class="number">Chapter 2</span> A demo app</a></li><li><ol><li class="section"><a href="a-demo-app.html#sec-planning_the_application"><span class="number">2.1</span> Planning the application</a></li><li><ol><li class="subsection"><a href="a-demo-app.html#sec-modeling_demo_users"><span class="number">2.1.1</span> Modeling demo users</a></li><li class="subsection"><a href="a-demo-app.html#sec-modeling_demo_microposts"><span class="number">2.1.2</span> Modeling demo microposts</a></li></ol></li><li class="section"><a href="a-demo-app.html#sec-demo_users_resource"><span class="number">2.2</span> The Users resource</a></li><li><ol><li class="subsection"><a href="a-demo-app.html#sec-a_user_tour"><span class="number">2.2.1</span> A user tour</a></li><li class="subsection"><a href="a-demo-app.html#sec-mvc_in_action"><span class="number">2.2.2</span> MVC in action</a></li><li class="subsection"><a href="a-demo-app.html#sec-weaknesses_of_this_users_resource"><span class="number">2.2.3</span> Weaknesses of this Users resource</a></li></ol></li><li class="section"><a href="a-demo-app.html#sec-microposts_resource"><span class="number">2.3</span> The Microposts resource</a></li><li><ol><li class="subsection"><a href="a-demo-app.html#sec-a_micropost_microtour"><span class="number">2.3.1</span> A micropost microtour</a></li><li class="subsection"><a href="a-demo-app.html#sec-putting_the_micro_in_microposts"><span class="number">2.3.2</span> Putting the <em>micro</em> in microposts</a></li><li class="subsection"><a href="a-demo-app.html#sec-demo_user_has_many_microposts"><span class="number">2.3.3</span> A user <tt>has_many</tt> microposts</a></li><li class="subsection"><a href="a-demo-app.html#sec-inheritance_hierarchies"><span class="number">2.3.4</span> Inheritance hierarchies</a></li><li class="subsection"><a href="a-demo-app.html#sec-deploying_the_demo_app"><span class="number">2.3.5</span> Deploying the demo app</a></li></ol></li><li class="section"><a href="a-demo-app.html#sec-2_4"><span class="number">2.4</span> Conclusion</a></li></ol></li><li class="chapter"><a href="static-pages.html#top"><span class="number">Chapter 3</span> Mostly static pages</a></li><li><ol><li class="section"><a href="static-pages.html#sec-static_pages"><span class="number">3.1</span> Static pages</a></li><li><ol><li class="subsection"><a href="static-pages.html#sec-truly_static_pages"><span class="number">3.1.1</span> Truly static pages</a></li><li class="subsection"><a href="static-pages.html#sec-static_pages_with_rails"><span class="number">3.1.2</span> Static pages with Rails</a></li></ol></li><li class="section"><a href="static-pages.html#sec-first_tests"><span class="number">3.2</span> Our first tests</a></li><li><ol><li class="subsection"><a href="static-pages.html#sec-TDD"><span class="number">3.2.1</span> Test-driven development</a></li><li class="subsection"><a href="static-pages.html#sec-adding_a_page"><span class="number">3.2.2</span> Adding a page</a></li><li><ol><li class="subsubsection"><a href="static-pages.html#sec-red">Red</a></li><li class="subsubsection"><a href="static-pages.html#sec-green">Green</a></li><li class="subsubsection"><a href="static-pages.html#sec-refactor">Refactor</a></li></ol></li></ol></li><li class="section"><a href="static-pages.html#sec-slightly_dynamic_pages"><span class="number">3.3</span> Slightly dynamic pages</a></li><li><ol><li class="subsection"><a href="static-pages.html#sec-testing_a_title_change"><span class="number">3.3.1</span> Testing a title change</a></li><li class="subsection"><a href="static-pages.html#sec-passing_title_tests"><span class="number">3.3.2</span> Passing title tests</a></li><li class="subsection"><a href="static-pages.html#sec-embedded_ruby"><span class="number">3.3.3</span> Embedded Ruby</a></li><li class="subsection"><a href="static-pages.html#sec-layouts"><span class="number">3.3.4</span> Eliminating duplication with layouts</a></li></ol></li><li class="section"><a href="static-pages.html#sec-static_pages_conclusion"><span class="number">3.4</span> Conclusion</a></li><li class="section"><a href="static-pages.html#sec-static_pages_exercises"><span class="number">3.5</span> Exercises</a></li><li class="section"><a href="static-pages.html#sec-advanced_setup"><span class="number">3.6</span> Advanced setup</a></li><li><ol><li class="subsection"><a href="static-pages.html#sec-eliminating_bundle_exec"><span class="number">3.6.1</span> Eliminating <tt>bundle exec</tt></a></li><li><ol><li class="subsubsection"><a href="static-pages.html#sec-rvm_bundler_integration">RVM Bundler integration</a></li><li class="subsubsection"><a href="static-pages.html#sec-binstubs">binstubs</a></li></ol></li><li class="subsection"><a href="static-pages.html#sec-guard"><span class="number">3.6.2</span> Automated tests with Guard</a></li><li class="subsection"><a href="static-pages.html#sec-spork"><span class="number">3.6.3</span> Speeding up tests with Spork</a></li><li><ol><li class="subsubsection"><a href="static-pages.html#sec-spork_and_guard">Guard with Spork</a></li></ol></li><li class="subsection"><a href="static-pages.html#sec-tests_inside_sublime_text"><span class="number">3.6.4</span> Tests inside Sublime Text</a></li></ol></li></ol></li><li class="chapter"><a href="rails-flavored-ruby.html#top"><span class="number">Chapter 4</span> Rails-flavored Ruby</a></li><li><ol><li class="section"><a href="rails-flavored-ruby.html#sec-motivation"><span class="number">4.1</span> Motivation</a></li><li class="section"><a href="rails-flavored-ruby.html#sec-strings_and_methods"><span class="number">4.2</span> Strings and methods</a></li><li><ol><li class="subsection"><a href="rails-flavored-ruby.html#sec-comments"><span class="number">4.2.1</span> Comments</a></li><li class="subsection"><a href="rails-flavored-ruby.html#sec-strings"><span class="number">4.2.2</span> Strings</a></li><li><ol><li class="subsubsection"><a href="rails-flavored-ruby.html#sec-printing">Printing</a></li><li class="subsubsection"><a href="rails-flavored-ruby.html#sec-single_quoted_strings">Single-quoted strings</a></li></ol></li><li class="subsection"><a href="rails-flavored-ruby.html#sec-objects_and_message_passing"><span class="number">4.2.3</span> Objects and message passing</a></li><li class="subsection"><a href="rails-flavored-ruby.html#sec-method_definitions"><span class="number">4.2.4</span> Method definitions</a></li><li class="subsection"><a href="rails-flavored-ruby.html#sec-back_to_the_title_helper"><span class="number">4.2.5</span> Back to the title helper</a></li></ol></li><li class="section"><a href="rails-flavored-ruby.html#sec-other_data_structures"><span class="number">4.3</span> Other data structures</a></li><li><ol><li class="subsection"><a href="rails-flavored-ruby.html#sec-arrays_and_ranges"><span class="number">4.3.1</span> Arrays and ranges</a></li><li class="subsection"><a href="rails-flavored-ruby.html#sec-blocks"><span class="number">4.3.2</span> Blocks</a></li><li class="subsection"><a href="rails-flavored-ruby.html#sec-hashes_and_symbols"><span class="number">4.3.3</span> Hashes and symbols</a></li><li class="subsection"><a href="rails-flavored-ruby.html#sec-css_revisited"><span class="number">4.3.4</span> CSS revisited</a></li></ol></li><li class="section"><a href="rails-flavored-ruby.html#sec-ruby_classes"><span class="number">4.4</span> Ruby classes</a></li><li><ol><li class="subsection"><a href="rails-flavored-ruby.html#sec-constructors"><span class="number">4.4.1</span> Constructors</a></li><li class="subsection"><a href="rails-flavored-ruby.html#sec-a_class_of_our_own"><span class="number">4.4.2</span> Class inheritance</a></li><li class="subsection"><a href="rails-flavored-ruby.html#sec-modifying_built_in_classes"><span class="number">4.4.3</span> Modifying built-in classes</a></li><li class="subsection"><a href="rails-flavored-ruby.html#sec-a_controller_class"><span class="number">4.4.4</span> A controller class</a></li><li class="subsection"><a href="rails-flavored-ruby.html#sec-a_user_class"><span class="number">4.4.5</span> A user class</a></li></ol></li><li class="section"><a href="rails-flavored-ruby.html#sec-conclusion"><span class="number">4.5</span> Conclusion</a></li><li class="section"><a href="rails-flavored-ruby.html#sec-exercises"><span class="number">4.6</span> Exercises</a></li></ol></li><li class="chapter"><a href="filling-in-the-layout.html#top"><span class="number">Chapter 5</span> Filling in the layout</a></li><li><ol><li class="section"><a href="filling-in-the-layout.html#sec-structure"><span class="number">5.1</span> Adding some structure</a></li><li><ol><li class="subsection"><a href="filling-in-the-layout.html#sec-adding_to_the_layout"><span class="number">5.1.1</span> Site navigation</a></li><li class="subsection"><a href="filling-in-the-layout.html#sec-custom_css"><span class="number">5.1.2</span> Bootstrap and custom CSS</a></li><li class="subsection"><a href="filling-in-the-layout.html#sec-partials"><span class="number">5.1.3</span> Partials</a></li></ol></li><li class="section"><a href="filling-in-the-layout.html#sec-sass_and_the_asset_pipeline"><span class="number">5.2</span> Sass and the asset pipeline</a></li><li><ol><li class="subsection"><a href="filling-in-the-layout.html#sec-the_asset_pipeline"><span class="number">5.2.1</span> The asset pipeline</a></li><li><ol><li class="subsubsection"><a href="filling-in-the-layout.html#sec-5_2_1_1">Asset directories</a></li><li class="subsubsection"><a href="filling-in-the-layout.html#sec-5_2_1_2">Manifest files</a></li><li class="subsubsection"><a href="filling-in-the-layout.html#sec-5_2_1_3">Preprocessor engines</a></li><li class="subsubsection"><a href="filling-in-the-layout.html#sec-5_2_1_4">Efficiency in production</a></li></ol></li><li class="subsection"><a href="filling-in-the-layout.html#sec-sass"><span class="number">5.2.2</span> Syntactically awesome stylesheets</a></li><li><ol><li class="subsubsection"><a href="filling-in-the-layout.html#sec-5_2_2_1">Nesting</a></li><li class="subsubsection"><a href="filling-in-the-layout.html#sec-5_2_2_2">Variables</a></li></ol></li></ol></li><li class="section"><a href="filling-in-the-layout.html#sec-layout_links"><span class="number">5.3</span> Layout links</a></li><li><ol><li class="subsection"><a href="filling-in-the-layout.html#sec-route_tests"><span class="number">5.3.1</span> Route tests</a></li><li class="subsection"><a href="filling-in-the-layout.html#sec-rails_routes"><span class="number">5.3.2</span> Rails routes</a></li><li class="subsection"><a href="filling-in-the-layout.html#sec-named_routes"><span class="number">5.3.3</span> Named routes</a></li><li class="subsection"><a href="filling-in-the-layout.html#sec-pretty_rspec"><span class="number">5.3.4</span> Pretty RSpec</a></li></ol></li><li class="section"><a href="filling-in-the-layout.html#sec-user_signup"><span class="number">5.4</span> User signup: A first step</a></li><li><ol><li class="subsection"><a href="filling-in-the-layout.html#sec-users_controller"><span class="number">5.4.1</span> Users controller</a></li><li class="subsection"><a href="filling-in-the-layout.html#sec-signup_url"><span class="number">5.4.2</span> Signup URI</a></li></ol></li><li class="section"><a href="filling-in-the-layout.html#sec-layout_conclusion"><span class="number">5.5</span> Conclusion</a></li><li class="section"><a href="filling-in-the-layout.html#sec-layout_exercises"><span class="number">5.6</span> Exercises</a></li></ol></li><li class="chapter"><a href="modeling-users.html#top"><span class="number">Chapter 6</span> Modeling users</a></li><li><ol><li class="section"><a href="modeling-users.html#sec-user_model"><span class="number">6.1</span> User model</a></li><li><ol><li class="subsection"><a href="modeling-users.html#sec-database_migrations"><span class="number">6.1.1</span> Database migrations</a></li><li class="subsection"><a href="modeling-users.html#sec-the_model_file"><span class="number">6.1.2</span> The model file</a></li><li><ol><li class="subsubsection"><a href="modeling-users.html#sec-model_annotation">Model annotation</a></li><li class="subsubsection"><a href="modeling-users.html#sec-accessible_attributes">Accessible attributes</a></li></ol></li><li class="subsection"><a href="modeling-users.html#sec-creating_user_objects"><span class="number">6.1.3</span> Creating user objects</a></li><li class="subsection"><a href="modeling-users.html#sec-finding_user_objects"><span class="number">6.1.4</span> Finding user objects</a></li><li class="subsection"><a href="modeling-users.html#sec-updating_user_objects"><span class="number">6.1.5</span> Updating user objects</a></li></ol></li><li class="section"><a href="modeling-users.html#sec-user_validations"><span class="number">6.2</span> User validations</a></li><li><ol><li class="subsection"><a href="modeling-users.html#sec-initial_user_tests"><span class="number">6.2.1</span> Initial user tests</a></li><li class="subsection"><a href="modeling-users.html#sec-presence_validation"><span class="number">6.2.2</span> Validating presence</a></li><li class="subsection"><a href="modeling-users.html#sec-length_validation"><span class="number">6.2.3</span> Length validation</a></li><li class="subsection"><a href="modeling-users.html#sec-format_validation"><span class="number">6.2.4</span> Format validation</a></li><li class="subsection"><a href="modeling-users.html#sec-uniqueness_validation"><span class="number">6.2.5</span> Uniqueness validation</a></li><li><ol><li class="subsubsection"><a href="modeling-users.html#sec-the_caveat">The uniqueness caveat</a></li></ol></li></ol></li><li class="section"><a href="modeling-users.html#sec-adding_a_secure_password"><span class="number">6.3</span> Adding a secure password</a></li><li><ol><li class="subsection"><a href="modeling-users.html#sec-an_encrypted_password"><span class="number">6.3.1</span> An encrypted password</a></li><li class="subsection"><a href="modeling-users.html#sec-password_and_confirmation"><span class="number">6.3.2</span> Password and confirmation</a></li><li class="subsection"><a href="modeling-users.html#sec-user_authentication"><span class="number">6.3.3</span> User authentication</a></li><li class="subsection"><a href="modeling-users.html#sec-has_secure_password"><span class="number">6.3.4</span> User has secure password</a></li><li class="subsection"><a href="modeling-users.html#sec-creating_a_user"><span class="number">6.3.5</span> Creating a user</a></li></ol></li><li class="section"><a href="modeling-users.html#sec-6_4"><span class="number">6.4</span> Conclusion</a></li><li class="section"><a href="modeling-users.html#sec-6_5"><span class="number">6.5</span> Exercises</a></li></ol></li><li class="chapter"><a href="sign-up.html#top"><span class="number">Chapter 7</span> Sign up</a></li><li><ol><li class="section"><a href="sign-up.html#sec-showing_users"><span class="number">7.1</span> Showing users</a></li><li><ol><li class="subsection"><a href="sign-up.html#sec-rails_environments"><span class="number">7.1.1</span> Debug and Rails environments</a></li><li class="subsection"><a href="sign-up.html#sec-a_users_resource"><span class="number">7.1.2</span> A Users resource</a></li><li class="subsection"><a href="sign-up.html#sec-tests_with_factories"><span class="number">7.1.3</span> Testing the user show page (with factories)</a></li><li class="subsection"><a href="sign-up.html#sec-a_gravatar_image"><span class="number">7.1.4</span> A Gravatar image and a sidebar</a></li></ol></li><li class="section"><a href="sign-up.html#sec-signup_form"><span class="number">7.2</span> Signup form</a></li><li><ol><li class="subsection"><a href="sign-up.html#sec-tests_for_user_signup"><span class="number">7.2.1</span> Tests for user signup</a></li><li class="subsection"><a href="sign-up.html#sec-using_form_for"><span class="number">7.2.2</span> Using <tt>form_for</tt></a></li><li class="subsection"><a href="sign-up.html#sec-the_form_html"><span class="number">7.2.3</span> The form HTML</a></li></ol></li><li class="section"><a href="sign-up.html#sec-signup_failure"><span class="number">7.3</span> Signup failure</a></li><li><ol><li class="subsection"><a href="sign-up.html#sec-a_working_form"><span class="number">7.3.1</span> A working form</a></li><li class="subsection"><a href="sign-up.html#sec-signup_error_messages"><span class="number">7.3.2</span> Signup error messages</a></li></ol></li><li class="section"><a href="sign-up.html#sec-signup_success"><span class="number">7.4</span> Signup success</a></li><li><ol><li class="subsection"><a href="sign-up.html#sec-the_finished_signup_form"><span class="number">7.4.1</span> The finished signup form</a></li><li class="subsection"><a href="sign-up.html#sec-the_flash"><span class="number">7.4.2</span> The flash</a></li><li class="subsection"><a href="sign-up.html#sec-the_first_signup"><span class="number">7.4.3</span> The first signup</a></li><li class="subsection"><a href="sign-up.html#sec-deploying_to_production_with_ssl"><span class="number">7.4.4</span> Deploying to production with SSL</a></li></ol></li><li class="section"><a href="sign-up.html#sec-7_5"><span class="number">7.5</span> Conclusion</a></li><li class="section"><a href="sign-up.html#sec-signup_exercises"><span class="number">7.6</span> Exercises</a></li></ol></li><li class="chapter"><a href="sign-in-sign-out.html#top"><span class="number">Chapter 8</span> Sign in, sign out</a></li><li><ol><li class="section"><a href="sign-in-sign-out.html#sec-signin_failure"><span class="number">8.1</span> Sessions and signin failure</a></li><li><ol><li class="subsection"><a href="sign-in-sign-out.html#sec-sessions_controller"><span class="number">8.1.1</span> Sessions controller</a></li><li class="subsection"><a href="sign-in-sign-out.html#sec-signin_tests"><span class="number">8.1.2</span> Signin tests</a></li><li class="subsection"><a href="sign-in-sign-out.html#sec-signin_form"><span class="number">8.1.3</span> Signin form</a></li><li class="subsection"><a href="sign-in-sign-out.html#sec-reviewing_form_submission"><span class="number">8.1.4</span> Reviewing form submission</a></li><li class="subsection"><a href="sign-in-sign-out.html#sec-rendering_with_a_flash_message"><span class="number">8.1.5</span> Rendering with a flash message</a></li></ol></li><li class="section"><a href="sign-in-sign-out.html#sec-signin_success"><span class="number">8.2</span> Signin success</a></li><li><ol><li class="subsection"><a href="sign-in-sign-out.html#sec-remember_me"><span class="number">8.2.1</span> Remember me</a></li><li class="subsection"><a href="sign-in-sign-out.html#sec-a_working_sign_in_method"><span class="number">8.2.2</span> A working <tt>sign_in</tt> method</a></li><li class="subsection"><a href="sign-in-sign-out.html#sec-current_user"><span class="number">8.2.3</span> Current user</a></li><li class="subsection"><a href="sign-in-sign-out.html#sec-changing_the_layout_links"><span class="number">8.2.4</span> Changing the layout links</a></li><li class="subsection"><a href="sign-in-sign-out.html#sec-signin_upon_signup"><span class="number">8.2.5</span> Signin upon signup</a></li><li class="subsection"><a href="sign-in-sign-out.html#sec-signing_out"><span class="number">8.2.6</span> Signing out</a></li></ol></li><li class="section"><a href="sign-in-sign-out.html#sec-cucumber"><span class="number">8.3</span> Introduction to Cucumber (optional)</a></li><li><ol><li class="subsection"><a href="sign-in-sign-out.html#sec-installation_and_setup"><span class="number">8.3.1</span> Installation and setup</a></li><li class="subsection"><a href="sign-in-sign-out.html#sec-features_and_steps"><span class="number">8.3.2</span> Features and steps</a></li><li class="subsection"><a href="sign-in-sign-out.html#sec-rspec_custom_matchers"><span class="number">8.3.3</span> Counterpoint: RSpec custom matchers</a></li></ol></li><li class="section"><a href="sign-in-sign-out.html#sec-8_4"><span class="number">8.4</span> Conclusion</a></li><li class="section"><a href="sign-in-sign-out.html#sec-sign_in_out_exercises"><span class="number">8.5</span> Exercises</a></li></ol></li><li class="chapter"><a href="updating-showing-and-deleting-users.html#top"><span class="number">Chapter 9</span> Updating, showing, and deleting users</a></li><li><ol><li class="section"><a href="updating-showing-and-deleting-users.html#sec-updating_users"><span class="number">9.1</span> Updating users</a></li><li><ol><li class="subsection"><a href="updating-showing-and-deleting-users.html#sec-edit_form"><span class="number">9.1.1</span> Edit form</a></li><li class="subsection"><a href="updating-showing-and-deleting-users.html#sec-unsuccessful_edits"><span class="number">9.1.2</span> Unsuccessful edits</a></li><li class="subsection"><a href="updating-showing-and-deleting-users.html#sec-successful_edits"><span class="number">9.1.3</span> Successful edits</a></li></ol></li><li class="section"><a href="updating-showing-and-deleting-users.html#sec-authorization"><span class="number">9.2</span> Authorization</a></li><li><ol><li class="subsection"><a href="updating-showing-and-deleting-users.html#sec-requiring_signed_in_users"><span class="number">9.2.1</span> Requiring signed-in users</a></li><li class="subsection"><a href="updating-showing-and-deleting-users.html#sec-requiring_the_right_user"><span class="number">9.2.2</span> Requiring the right user</a></li><li class="subsection"><a href="updating-showing-and-deleting-users.html#sec-friendly_forwarding"><span class="number">9.2.3</span> Friendly forwarding</a></li></ol></li><li class="section"><a href="updating-showing-and-deleting-users.html#sec-showing_all_users"><span class="number">9.3</span> Showing all users</a></li><li><ol><li class="subsection"><a href="updating-showing-and-deleting-users.html#sec-user_index"><span class="number">9.3.1</span> User index</a></li><li class="subsection"><a href="updating-showing-and-deleting-users.html#sec-sample_users"><span class="number">9.3.2</span> Sample users</a></li><li class="subsection"><a href="updating-showing-and-deleting-users.html#sec-pagination"><span class="number">9.3.3</span> Pagination</a></li><li class="subsection"><a href="updating-showing-and-deleting-users.html#sec-partial_refactoring"><span class="number">9.3.4</span> Partial refactoring</a></li></ol></li><li class="section"><a href="updating-showing-and-deleting-users.html#sec-destroying_users"><span class="number">9.4</span> Deleting users</a></li><li><ol><li class="subsection"><a href="updating-showing-and-deleting-users.html#sec-administrative_users"><span class="number">9.4.1</span> Administrative users</a></li><li><ol><li class="subsubsection"><a href="updating-showing-and-deleting-users.html#sec-revisiting_attr_accessible">Revisiting <tt>attr_accessible</tt></a></li></ol></li><li class="subsection"><a href="updating-showing-and-deleting-users.html#sec-the_destroy_action"><span class="number">9.4.2</span> The <tt>destroy</tt> action</a></li></ol></li><li class="section"><a href="updating-showing-and-deleting-users.html#sec-updating_and_deleting_users_conclusion"><span class="number">9.5</span> Conclusion</a></li><li class="section"><a href="updating-showing-and-deleting-users.html#sec-updating_deleting_exercises"><span class="number">9.6</span> Exercises</a></li></ol></li><li class="chapter"><a href="user-microposts.html#top"><span class="number">Chapter 10</span> User microposts</a></li><li><ol><li class="section"><a href="user-microposts.html#sec-a_micropost_model"><span class="number">10.1</span> A Micropost model</a></li><li><ol><li class="subsection"><a href="user-microposts.html#sec-the_basic_model"><span class="number">10.1.1</span> The basic model</a></li><li class="subsection"><a href="user-microposts.html#sec-accessible_attribute"><span class="number">10.1.2</span> Accessible attributes and the first validation</a></li><li class="subsection"><a href="user-microposts.html#sec-user_micropost_associations"><span class="number">10.1.3</span> User/Micropost associations</a></li><li class="subsection"><a href="user-microposts.html#sec-ordering_and_dependency"><span class="number">10.1.4</span> Micropost refinements</a></li><li><ol><li class="subsubsection"><a href="user-microposts.html#sec-default_scope">Default scope</a></li><li class="subsubsection"><a href="user-microposts.html#sec-dependent_destroy">Dependent: destroy</a></li></ol></li><li class="subsection"><a href="user-microposts.html#sec-micropost_validations"><span class="number">10.1.5</span> Content validations</a></li></ol></li><li class="section"><a href="user-microposts.html#sec-showing_microposts"><span class="number">10.2</span> Showing microposts</a></li><li><ol><li class="subsection"><a href="user-microposts.html#sec-augmenting_the_user_show_page"><span class="number">10.2.1</span> Augmenting the user show page</a></li><li class="subsection"><a href="user-microposts.html#sec-sample_microposts"><span class="number">10.2.2</span> Sample microposts</a></li></ol></li><li class="section"><a href="user-microposts.html#sec-manipulating_microposts"><span class="number">10.3</span> Manipulating microposts</a></li><li><ol><li class="subsection"><a href="user-microposts.html#sec-access_control"><span class="number">10.3.1</span> Access control</a></li><li class="subsection"><a href="user-microposts.html#sec-creating_microposts"><span class="number">10.3.2</span> Creating microposts</a></li><li class="subsection"><a href="user-microposts.html#sec-a_proto_feed"><span class="number">10.3.3</span> A proto-feed</a></li><li class="subsection"><a href="user-microposts.html#sec-destroying_microposts"><span class="number">10.3.4</span> Destroying microposts</a></li></ol></li><li class="section"><a href="user-microposts.html#sec-10_4"><span class="number">10.4</span> Conclusion</a></li><li class="section"><a href="user-microposts.html#sec-micropost_exercises"><span class="number">10.5</span> Exercises</a></li></ol></li><li class="chapter"><a href="following-users.html#top"><span class="number">Chapter 11</span> Following users</a></li><li><ol><li class="section"><a href="following-users.html#sec-the_relationship_model"><span class="number">11.1</span> The Relationship model</a></li><li><ol><li class="subsection"><a href="following-users.html#sec-a_problem_with_the_data_model"><span class="number">11.1.1</span> A problem with the data model (and a solution)</a></li><li class="subsection"><a href="following-users.html#sec-relationship_user_associations"><span class="number">11.1.2</span> User/relationship associations</a></li><li class="subsection"><a href="following-users.html#sec-relationship_validations"><span class="number">11.1.3</span> Validations</a></li><li class="subsection"><a href="following-users.html#sec-following"><span class="number">11.1.4</span> Followed users</a></li><li class="subsection"><a href="following-users.html#sec-followers"><span class="number">11.1.5</span> Followers</a></li></ol></li><li class="section"><a href="following-users.html#sec-a_web_interface_for_following_and_followers"><span class="number">11.2</span> A web interface for following users</a></li><li><ol><li class="subsection"><a href="following-users.html#sec-sample_following_data"><span class="number">11.2.1</span> Sample following data</a></li><li class="subsection"><a href="following-users.html#sec-stats_and_a_follow_form"><span class="number">11.2.2</span> Stats and a follow form</a></li><li class="subsection"><a href="following-users.html#sec-following_and_followers_pages"><span class="number">11.2.3</span> Following and followers pages</a></li><li class="subsection"><a href="following-users.html#sec-a_working_follow_button_the_standard_way"><span class="number">11.2.4</span> A working follow button the standard way</a></li><li class="subsection"><a href="following-users.html#sec-a_working_follow_button_with_ajax"><span class="number">11.2.5</span> A working follow button with Ajax</a></li></ol></li><li class="section"><a href="following-users.html#sec-the_status_feed"><span class="number">11.3</span> The status feed</a></li><li><ol><li class="subsection"><a href="following-users.html#sec-motivation_and_strategy"><span class="number">11.3.1</span> Motivation and strategy</a></li><li class="subsection"><a href="following-users.html#sec-a_first_feed_implementation"><span class="number">11.3.2</span> A first feed implementation</a></li><li class="subsection"><a href="following-users.html#sec-scopes_subselects_and_a_lambda"><span class="number">11.3.3</span> Subselects</a></li><li class="subsection"><a href="following-users.html#sec-the_new_status_feed"><span class="number">11.3.4</span> The new status feed</a></li></ol></li><li class="section"><a href="following-users.html#sec-following_conclusion"><span class="number">11.4</span> Conclusion</a></li><li><ol><li class="subsection"><a href="following-users.html#sec-extensions_to_the_sample_application"><span class="number">11.4.1</span> Extensions to the sample application</a></li><li><ol><li class="subsubsection"><a href="following-users.html#sec-replies">Replies</a></li><li class="subsubsection"><a href="following-users.html#sec-messaging">Messaging</a></li><li class="subsubsection"><a href="following-users.html#sec-follower_notifications">Follower notifications</a></li><li class="subsubsection"><a href="following-users.html#sec-password_reminders">Password reminders</a></li><li class="subsubsection"><a href="following-users.html#sec-signup_confirmation">Signup confirmation</a></li><li class="subsubsection"><a href="following-users.html#sec-rss_feed">RSS feed</a></li><li class="subsubsection"><a href="following-users.html#sec-rest_api">REST API</a></li><li class="subsubsection"><a href="following-users.html#sec-search">Search</a></li></ol></li><li class="subsection"><a href="following-users.html#sec-guide_to_further_resources"><span class="number">11.4.2</span> Guide to further resources</a></li></ol></li><li class="section"><a href="following-users.html#sec-following_exercises"><span class="number">11.5</span> Exercises</a></li></ol></li><li class="chapter"><a href="supplement.html#top"><span class="number">Chapter 12</span> Rails 4.0 supplement</a></li><li><ol><li class="section"><a href="supplement.html#sec-upgrading_from_rails_3_2_to_4_0"><span class="number">12.1</span> Upgrading from Rails 3.2 to 4.0</a></li><li><ol><li class="subsection"><a href="supplement.html#sec-rails_4_0_setup"><span class="number">12.1.1</span> Rails 4.0 setup</a></li><li class="subsection"><a href="supplement.html#sec-getting_to_green"><span class="number">12.1.2</span> Getting to green</a></li><li class="subsection"><a href="supplement.html#sec-some_specific_issues"><span class="number">12.1.3</span> Some specific issues</a></li><li><ol><li class="subsubsection"><a href="supplement.html#sec-models">Models</a></li><li class="subsubsection"><a href="supplement.html#sec-controllers_and_views">Controllers and views</a></li></ol></li><li class="subsection"><a href="supplement.html#sec-finishing_up"><span class="number">12.1.4</span> Finishing up</a></li><li class="subsection"><a href="supplement.html#sec-additional_resources"><span class="number">12.1.5</span> Additional resources</a></li></ol></li><li class="section"><a href="supplement.html#sec-strong_parameters"><span class="number">12.2</span> Strong parameters</a></li><li class="section"><a href="supplement.html#sec-security_updates"><span class="number">12.3</span> Security updates</a></li><li><ol><li class="subsection"><a href="supplement.html#sec-secret_key"><span class="number">12.3.1</span> Secret key</a></li><li class="subsection"><a href="supplement.html#sec-encrypted_remember_tokens"><span class="number">12.3.2</span> Hashed remember tokens</a></li></ol></li></ol></li></ol></div>
<div id="main_content"></div>
<p> <span class="preamble">
<span id="foreword">
<strong> Foreword</strong> <br />
</span>
</span></p>
<p>My former company (CD Baby) was one of the first to loudly switch to Ruby on Rails, and then even more loudly switch back to PHP (Google me to read about the drama). This book by Michael Hartl came so highly recommended that I had to try it, and the <em>Ruby on Rails Tutorial</em> is what I used to switch back to Rails again.</p>
<p>Though I’ve worked my way through many Rails books, this is the one that finally made me “get” it. Everything is done very much “the Rails way”—a way that felt very unnatural to me before, but now after doing this book finally feels natural. This is also the only Rails book that does test-driven development the entire time, an approach highly recommended by the experts but which has never been so clearly demonstrated before. Finally, by including Git, GitHub, and Heroku in the demo examples, the author really gives you a feel for what it’s like to do a real-world project. The tutorial’s code examples are not in isolation.</p>
<p>The linear narrative is such a great format. Personally, I powered through the <em>Rails Tutorial</em> in three long days, doing all the examples and challenges at the end of each chapter. Do it from start to finish, without jumping around, and you’ll get the ultimate benefit.</p>
<p>Enjoy!</p>
<p><a href="http://sivers.org/">Derek Sivers</a> (<a href="http://sivers.org/">sivers.org</a>) <br />
<em>Founder, CD Baby</em> <br /></p>
<p> <span class="preamble">
<strong> Acknowledgments</strong> <br />
</span></p>
<p>The <em>Ruby on Rails Tutorial</em> owes a lot to my previous Rails book, <em>RailsSpace</em>, and hence to my coauthor <a href="http://aure.com/">Aurelius Prochazka</a>. I’d like to thank Aure both for the work he did on that book and for his support of this one. I’d also like to thank Debra Williams Cauley, my editor on both <em>RailsSpace</em> and the <em>Ruby on Rails Tutorial</em>; as long as she keeps taking me to baseball games, I’ll keep writing books for her.</p>
<p>I’d like to acknowledge a long list of Rubyists who have taught and inspired me over the years: David Heinemeier Hansson, Yehuda Katz, Carl Lerche, Jeremy Kemper, Xavier Noria, Ryan Bates, Geoffrey Grosenbach, Peter Cooper, Matt Aimonetti, Gregg Pollack, Wayne E. Seguin, Amy Hoy, Dave Chelimsky, Pat Maddox, Tom Preston-Werner, Chris Wanstrath, Chad Fowler, Josh Susser, Obie Fernandez, Ian McFarland, Steven Bristol, Pratik Naik, Sarah Mei, Sarah Allen, Wolfram Arnold, Alex Chaffee, Giles Bowkett, Evan Dorn, Long Nguyen, James Lindenbaum, Adam Wiggins, Tikhon Bernstam, Ron Evans, Wyatt Greene, Miles Forrest, the good people at Pivotal Labs, the Heroku gang, the thoughtbot guys, and the GitHub crew. Finally, many, many readers—far too many to list—have contributed a huge number of bug reports and suggestions during the writing of this book, and I gratefully acknowledge their help in making it as good as it can be. <br /></p>
<p> <span class="preamble">
<span id="author">
<strong> About the author</strong> <br />
</span>
</span></p>
<p><a href="http://michaelhartl.com/">Michael Hartl</a> is the author of the <a href="http://ruby.railstutorial.org/"><em>Ruby on Rails Tutorial</em></a>, the leading introduction to web development with <a href="http://rubyonrails.org/">Ruby on Rails</a>. His prior experience includes writing and developing <em>RailsSpace</em>, an extremely obsolete Rails tutorial book, and developing Insoshi, a once-popular and now-obsolete social networking platform in Ruby on Rails. In 2011, Michael received a <a href="http://rubyheroes.com/heroes">Ruby Hero Award</a> for his contributions to the Ruby community. He is a graduate of <a href="http://college.harvard.edu/">Harvard College</a>, has a <a href="http://resolver.caltech.edu/CaltechETD:etd-05222003-161626">Ph.D. in Physics</a> from <a href="http://www.caltech.edu/">Caltech</a>, and is an alumnus of the <a href="http://ycombinator.com/">Y Combinator</a> entrepreneur program. <br /></p>
<p> <span id="license" class="preamble">
<strong> Copyright and license</strong> <br />
</span></p>
<p><em>Ruby on Rails Tutorial: Learn Web Development with Rails</em>. Copyright © 2012 by Michael Hartl. All source code in the <em>Ruby on Rails Tutorial</em> is available jointly under the <a href="http://www.opensource.org/licenses/mit-license.php">MIT License</a> and the <a href="http://people.freebsd.org/~phk/">Beerware License</a>.</p>
<div class="code"><div class="highlight"><pre>The MIT License
Copyright (c) 2012 Michael Hartl
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
</pre></div>
</div>
<div class="code"><div class="highlight"><pre>/*
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* Michael Hartl wrote this code. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a beer in return.
* ----------------------------------------------------------------------------
*/
</pre></div>
</div>
<div id="top"></div>
<h1 class="chapter"><a id="sec-10" href="user-microposts.html#top" class="heading"><span class="number">Chapter 10</span> User microposts</a></h1>
<p><a class="ref" href="updating-showing-and-deleting-users.html#top">Chapter 9</a> saw the completion of the REST actions for the Users resource, so the time has finally come to add a second full resource: user <em>microposts</em>.<sup class="footnote" id="fnref-10_1"><a href="#fn-10_1">1</a></sup> These are short messages associated with a particular user, first seen in larval form in <a class="ref" href="a-demo-app.html#top">Chapter 2</a>. In this chapter, we will make a full-strength version of the sketch from <a class="ref" href="a-demo-app.html#sec-microposts_resource">Section 2.3</a> by constructing the Micropost data model, associating it with the User model using the <code>has_many</code> and <code>belongs_to</code> methods, and then making the forms and partials needed to manipulate and display the results. In <a class="ref" href="following-users.html#top">Chapter 11</a>, we’ll complete our tiny Twitter clone by adding the notion of <em>following</em> users in order to receive a <em>feed</em> of their microposts.</p>
<p>If you’re using Git for version control, I suggest making a topic branch as usual:</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> git checkout -b user-microposts
</pre></div>
</div>
<div class="label" id="sec-a_micropost_model"></div>
<h2><a id="sec-10_1" href="user-microposts.html#sec-a_micropost_model" class="heading"><span class="number">10.1</span> A Micropost model</a></h2>
<p>We begin the Microposts resource by creating a Micropost model, which captures the essential characteristics of microposts. What follows builds on the work from <a class="ref" href="a-demo-app.html#sec-microposts_resource">Section 2.3</a>; as with the model in that section, our new Micropost model will include data validations and an association with the User model. Unlike that model, the present Micropost model will be fully tested, and will also have a default <em>ordering</em> and automatic <em>destruction</em> if its parent user is destroyed.</p>
<div class="label" id="sec-the_basic_model"></div>
<h3><a id="sec-10_1_1" href="user-microposts.html#sec-the_basic_model" class="heading"><span class="number">10.1.1</span> The basic model</a></h3>
<p>The Micropost model needs only two attributes: a <code>content</code> attribute to hold the micropost’s content,<sup class="footnote" id="fnref-10_2"><a href="#fn-10_2">2</a></sup> and a <code>user_id</code> to associate a micropost with a particular user. As with the case of the User model (<a class="ref" href="modeling-users.html#code-generate_user_model">Listing 6.1</a>), we generate it using <code>generate model</code>:</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> rails generate model Micropost content:string user_id:integer
</pre></div>
</div>
<p>This produces a migration to create a <code>microposts</code> table in the database (<a class="ref" href="user-microposts.html#code-micropost_migration">Listing 10.1</a>); compare it to the analogous migration for the <code>users</code> table from <a class="ref" href="modeling-users.html#code-users_migration">Listing 6.2</a>.</p>
<div class="label" id="code-micropost_migration"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 10.1.</span> <span class="description">The Micropost migration. (Note the index on <code>user_id</code> and <code>created_at</code>.) <br /> <code>db/migrate/[timestamp]_create_microposts.rb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="k">class</span> <span class="nc">CreateMicroposts</span> <span class="o"><</span> <span class="ss">ActiveRecord::Migration</span>
<span class="k">def</span> <span class="nf">change</span>
<span class="n">create_table</span> <span class="ss">:microposts</span> <span class="k">do</span> <span class="o">|</span><span class="n">t</span><span class="o">|</span>
<span class="n">t</span><span class="o">.</span><span class="n">string</span> <span class="ss">:content</span>
<span class="n">t</span><span class="o">.</span><span class="n">integer</span> <span class="ss">:user_id</span>
<span class="n">t</span><span class="o">.</span><span class="n">timestamps</span>
<span class="k">end</span>
<span class="n">add_index</span> <span class="ss">:microposts</span><span class="p">,</span> <span class="o">[</span><span class="ss">:user_id</span><span class="p">,</span> <span class="ss">:created_at</span><span class="o">]</span>
<span class="k">end</span>
<span class="k">end</span>
</pre></div>
</div></div>
<p>Note that, since we expect to retrieve all the microposts associated with a given user id in reverse order of creation, <a class="ref" href="user-microposts.html#code-micropost_migration">Listing 10.1</a> adds an index (<a class="ref" href="modeling-users.html#sidebar-database_indices">Box 6.2</a>) on the <code>user_id</code> and <code>created_at</code> columns:</p>
<div class="code"><div class="highlight"><pre><span class="n">add_index</span> <span class="ss">:microposts</span><span class="p">,</span> <span class="o">[</span><span class="ss">:user_id</span><span class="p">,</span> <span class="ss">:created_at</span><span class="o">]</span>
</pre></div>
</div>
<p>By including both the <code>user_id</code> and <code>created_at</code> columns as an array, we arrange for Rails to create a <em>multiple key index</em>, which means that Active Record uses <em>both</em> keys at the same time. Note also the <code>t.timestamps</code> line, which (as mentioned in <a class="ref" href="modeling-users.html#sec-database_migrations">Section 6.1.1</a>) adds the magic <code>created_at</code> and <code>updated_at</code> columns. We’ll put the <code>created_at</code> column to work in <a class="ref" href="user-microposts.html#sec-ordering_and_dependency">Section 10.1.4</a> and <a class="ref" href="user-microposts.html#sec-augmenting_the_user_show_page">Section 10.2.1</a>.</p>
<p>We’ll start with some minimal tests for the Micropost model based on the analogous tests for the User model (<a class="ref" href="modeling-users.html#code-user_spec">Listing 6.8</a>). In particular, we verify that a micropost object responds to the <code>content</code> and <code>user_id</code> attributes, as shown in <a class="ref" href="user-microposts.html#code-initial_micropost_spec">Listing 10.2</a>.</p>
<div class="label" id="code-initial_micropost_spec"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 10.2.</span> <span class="description">The initial Micropost spec. <br /> <code>spec/models/micropost_spec.rb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="nb">require</span> <span class="s1">'spec_helper'</span>
<span class="n">describe</span> <span class="no">Micropost</span> <span class="k">do</span>
<span class="n">let</span><span class="p">(</span><span class="ss">:user</span><span class="p">)</span> <span class="p">{</span> <span class="no">FactoryGirl</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="ss">:user</span><span class="p">)</span> <span class="p">}</span>
<span class="n">before</span> <span class="k">do</span>
<span class="c1"># This code is wrong!</span>
<span class="vi">@micropost</span> <span class="o">=</span> <span class="no">Micropost</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="ss">content:</span> <span class="s2">"Lorem ipsum"</span><span class="p">,</span> <span class="ss">user_id:</span> <span class="n">user</span><span class="o">.</span><span class="n">id</span><span class="p">)</span>
<span class="k">end</span>
<span class="n">subject</span> <span class="p">{</span> <span class="vi">@micropost</span> <span class="p">}</span>
<span class="n">it</span> <span class="p">{</span> <span class="n">should</span> <span class="n">respond_to</span><span class="p">(</span><span class="ss">:content</span><span class="p">)</span> <span class="p">}</span>
<span class="n">it</span> <span class="p">{</span> <span class="n">should</span> <span class="n">respond_to</span><span class="p">(</span><span class="ss">:user_id</span><span class="p">)</span> <span class="p">}</span>
<span class="k">end</span>
</pre></div>
</div></div>
<p>We can get these tests to pass by running the microposts migration and preparing the test database:</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> bundle <span class="nb">exec </span>rake db:migrate
<span class="gp">$</span> bundle <span class="nb">exec </span>rake db:test:prepare
</pre></div>
</div>
<p>The result is a Micropost model with the structure shown in <a class="ref" href="user-microposts.html#fig-micropost_model">Figure 10.1</a>.</p>
<div class="label" id="fig-micropost_model"></div>
<div class="figure"><div class="center"><span class="graphic"><img src="images/figures/micropost_model.png" alt="micropost_model" /></span></div><div class="caption"><span class="header">Figure 10.1: </span><span class="description">The Micropost data model.</span></div></div>
<p>You should verify that the tests pass:</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> bundle <span class="nb">exec </span>rspec spec/models/micropost_spec.rb
</pre></div>
</div>
<p>Even though the tests are passing, you might have noticed this code:</p>
<div class="code"><div class="highlight"><pre><span class="n">let</span><span class="p">(</span><span class="ss">:user</span><span class="p">)</span> <span class="p">{</span> <span class="no">FactoryGirl</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="ss">:user</span><span class="p">)</span> <span class="p">}</span>
<span class="n">before</span> <span class="k">do</span>
<span class="c1"># This code is wrong!</span>
<span class="vi">@micropost</span> <span class="o">=</span> <span class="no">Micropost</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="ss">content:</span> <span class="s2">"Lorem ipsum"</span><span class="p">,</span> <span class="ss">user_id:</span> <span class="n">user</span><span class="o">.</span><span class="n">id</span><span class="p">)</span>
<span class="k">end</span>
</pre></div>
</div>
<p>The comment indicates that the code in the <code>before</code> block is wrong. See if you can guess why. We’ll see the answer in <a class="ref" href="user-microposts.html#sec-user_micropost_associations">Section 10.1.3</a>.</p>
<div class="label" id="sec-accessible_attribute"></div>
<h3><a id="sec-10_1_2" href="user-microposts.html#sec-accessible_attribute" class="heading"><span class="number">10.1.2</span> Accessible attributes and the first validation</a></h3>
<p>To see why the code in the <code>before</code> block is wrong, we first start with validation tests for the Micropost model (<a class="ref" href="user-microposts.html#code-micropost_validity_test">Listing 10.3</a>). (Compare with the User model tests in <a class="ref" href="modeling-users.html#code-failing_validates_name_spec">Listing 6.11</a>.)</p>
<div class="label" id="code-micropost_validity_test"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 10.3.</span> <span class="description">Tests for the validity of a new micropost. <br /> <code>spec/models/micropost_spec.rb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="nb">require</span> <span class="s1">'spec_helper'</span>
<span class="n">describe</span> <span class="no">Micropost</span> <span class="k">do</span>
<span class="n">let</span><span class="p">(</span><span class="ss">:user</span><span class="p">)</span> <span class="p">{</span> <span class="no">FactoryGirl</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="ss">:user</span><span class="p">)</span> <span class="p">}</span>
<span class="n">before</span> <span class="k">do</span>
<span class="c1"># This code is wrong!</span>
<span class="vi">@micropost</span> <span class="o">=</span> <span class="no">Micropost</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="ss">content:</span> <span class="s2">"Lorem ipsum"</span><span class="p">,</span> <span class="ss">user_id:</span> <span class="n">user</span><span class="o">.</span><span class="n">id</span><span class="p">)</span>
<span class="k">end</span>
<span class="n">subject</span> <span class="p">{</span> <span class="vi">@micropost</span> <span class="p">}</span>
<span class="n">it</span> <span class="p">{</span> <span class="n">should</span> <span class="n">respond_to</span><span class="p">(</span><span class="ss">:content</span><span class="p">)</span> <span class="p">}</span>
<span class="n">it</span> <span class="p">{</span> <span class="n">should</span> <span class="n">respond_to</span><span class="p">(</span><span class="ss">:user_id</span><span class="p">)</span> <span class="p">}</span>
<span class="n">it</span> <span class="p">{</span> <span class="n">should</span> <span class="n">be_valid</span> <span class="p">}</span>
<span class="n">describe</span> <span class="s2">"when user_id is not present"</span> <span class="k">do</span>
<span class="n">before</span> <span class="p">{</span> <span class="vi">@micropost</span><span class="o">.</span><span class="n">user_id</span> <span class="o">=</span> <span class="kp">nil</span> <span class="p">}</span>
<span class="n">it</span> <span class="p">{</span> <span class="n">should_not</span> <span class="n">be_valid</span> <span class="p">}</span>
<span class="k">end</span>
<span class="k">end</span>
</pre></div>
</div></div>
<p>This code requires that the micropost be valid and tests for the presence of the <code>user_id</code> attribute. We can get these tests to pass with the simple presence validation shown in <a class="ref" href="user-microposts.html#code-micropost_user_id_validation">Listing 10.4</a>.</p>
<div class="label" id="code-micropost_user_id_validation"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 10.4.</span> <span class="description">A validation for the micropost’s <code>user_id</code>. <br /> <code>app/models/micropost.rb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Micropost</span> <span class="o"><</span> <span class="ss">ActiveRecord::Base</span>
<span class="n">attr_accessible</span> <span class="ss">:content</span><span class="p">,</span> <span class="ss">:user_id</span>
<span class="n">validates</span> <span class="ss">:user_id</span><span class="p">,</span> <span class="ss">presence:</span> <span class="kp">true</span>
<span class="k">end</span>
</pre></div>
</div></div>
<p>Now we’re prepared to see why</p>
<div class="code"><div class="highlight"><pre><span class="vi">@micropost</span> <span class="o">=</span> <span class="no">Micropost</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="ss">content:</span> <span class="s2">"Lorem ipsum"</span><span class="p">,</span> <span class="ss">user_id:</span> <span class="n">user</span><span class="o">.</span><span class="n">id</span><span class="p">)</span>
</pre></div>
</div>
<p>is wrong. The problem is that by default (as of Rails 3.2.3) <em>all</em> of the attributes for our Micropost model are accessible. As discussed in <a class="ref" href="modeling-users.html#sec-accessible_attributes">Section 6.1.2.2</a> and <a class="ref" href="updating-showing-and-deleting-users.html#sec-revisiting_attr_accessible">Section 9.4.1.1</a>, this means that anyone could change any aspect of a micropost object simply by using a command-line client to issue malicious requests. For example, a malicious user could change the <code>user_id</code> attributes on microposts, thereby associating microposts with the wrong users. This means that we should remove <code>:user_id</code> from the <code>attr_accessible</code> list, and once we do, the code above will fail. We’ll fix this issue in <a class="ref" href="user-microposts.html#sec-user_micropost_associations">Section 10.1.3</a>.</p>
<div class="label" id="sec-user_micropost_associations"></div>
<h3><a id="sec-10_1_3" href="user-microposts.html#sec-user_micropost_associations" class="heading"><span class="number">10.1.3</span> User/Micropost associations</a></h3>
<p>When constructing data models for web applications, it is essential to be able to make <em>associations</em> between individual models. In the present case, each micropost is associated with one user, and each user is associated with (potentially) many microposts—a relationship seen briefly in <a class="ref" href="a-demo-app.html#sec-demo_user_has_many_microposts">Section 2.3.3</a> and shown schematically in <a class="ref" href="user-microposts.html#fig-micropost_belongs_to_user">Figure 10.2</a> and <a class="ref" href="user-microposts.html#fig-user_has_many_microposts">Figure 10.3</a>. As part of implementing these associations, we’ll write tests for the Micropost model that, unlike <a class="ref" href="user-microposts.html#code-initial_micropost_spec">Listing 10.2</a>, are compatible with the use of <code>attr_accessible</code> in <a class="ref" href="user-microposts.html#code-micropost_accessible_attribute">Listing 10.7</a>.</p>
<div class="label" id="fig-micropost_belongs_to_user"></div>
<div class="figure"><div class="center"><span class="graphic"><img src="images/figures/micropost_belongs_to_user.png" alt="micropost_belongs_to_user" /></span></div><div class="caption"><span class="header">Figure 10.2: </span><span class="description">The <code>belongs_to</code> relationship between a micropost and its user.</span></div></div>
<div class="label" id="fig-user_has_many_microposts"></div>
<div class="figure"><div class="center"><span class="graphic"><img src="images/figures/user_has_many_microposts.png" alt="user_has_many_microposts" /></span></div><div class="caption"><span class="header">Figure 10.3: </span><span class="description">The <code>has_many</code> relationship between a user and its microposts.</span></div></div>
<p>Using the <code>belongs_to</code>/<code>has_many</code> association defined in this section, Rails constructs the methods shown in <a class="ref" href="user-microposts.html#table-association_methods">Table 10.1</a>.</p>
<div class="label" id="table-association_methods"></div>
<div class="table"><div class="center">
<table class="tabular"><tr><th class="align_left"><strong>Method</strong></th><th class="align_left"><strong>Purpose</strong></th></tr><tr class="top_bar"><td class="align_left"><code>micropost.user</code></td><td class="align_left">Return the User object associated with the micropost.</td></tr><tr><td class="align_left"><code>user.microposts</code></td><td class="align_left">Return an array of the user’s microposts.</td></tr><tr><td class="align_left"><code>user.microposts.create(arg)</code></td><td class="align_left">Create a micropost (<code>user_id = user.id</code>).</td></tr><tr><td class="align_left"><code>user.microposts.create!(arg)</code></td><td class="align_left">Create a micropost (exception on failure).</td></tr><tr><td class="align_left"><code>user.microposts.build(arg)</code></td><td class="align_left">Return a new Micropost object (<code>user_id = user.id</code>).</td></tr></table></div><div class="caption"><span class="header">Table 10.1: </span><span class="description">A summary of user/micropost association methods.</span></div></div>
<p>Note from <a class="ref" href="user-microposts.html#table-association_methods">Table 10.1</a> that instead of</p>
<div class="code"><div class="highlight"><pre><span class="no">Micropost</span><span class="o">.</span><span class="n">create</span>
<span class="no">Micropost</span><span class="o">.</span><span class="n">create!</span>
<span class="no">Micropost</span><span class="o">.</span><span class="n">new</span>
</pre></div>
</div>
<p>we have</p>
<div class="code"><div class="highlight"><pre><span class="n">user</span><span class="o">.</span><span class="n">microposts</span><span class="o">.</span><span class="n">create</span>
<span class="n">user</span><span class="o">.</span><span class="n">microposts</span><span class="o">.</span><span class="n">create!</span>
<span class="n">user</span><span class="o">.</span><span class="n">microposts</span><span class="o">.</span><span class="n">build</span>
</pre></div>
</div>
<p>This pattern is the canonical way to make a micropost: <em>through</em> its association with a user. When a new micropost is made in this way, its <code>user_id</code> is <em>automatically</em> set to the right value, which fixes the issue noted in <a class="ref" href="user-microposts.html#sec-accessible_attribute">Section 10.1.2</a>. In particular, we can replace the code</p>
<div class="code"><div class="highlight"><pre><span class="n">let</span><span class="p">(</span><span class="ss">:user</span><span class="p">)</span> <span class="p">{</span> <span class="no">FactoryGirl</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="ss">:user</span><span class="p">)</span> <span class="p">}</span>
<span class="n">before</span> <span class="k">do</span>
<span class="c1"># This code is wrong!</span>
<span class="vi">@micropost</span> <span class="o">=</span> <span class="no">Micropost</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="ss">content:</span> <span class="s2">"Lorem ipsum"</span><span class="p">,</span> <span class="ss">user_id:</span> <span class="n">user</span><span class="o">.</span><span class="n">id</span><span class="p">)</span>
<span class="k">end</span>
</pre></div>
</div>
<p>from <a class="ref" href="user-microposts.html#code-micropost_validity_test">Listing 10.3</a> with</p>
<div class="code"><div class="highlight"><pre><span class="n">let</span><span class="p">(</span><span class="ss">:user</span><span class="p">)</span> <span class="p">{</span> <span class="no">FactoryGirl</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="ss">:user</span><span class="p">)</span> <span class="p">}</span>
<span class="n">before</span> <span class="p">{</span> <span class="vi">@micropost</span> <span class="o">=</span> <span class="n">user</span><span class="o">.</span><span class="n">microposts</span><span class="o">.</span><span class="n">build</span><span class="p">(</span><span class="ss">content:</span> <span class="s2">"Lorem ipsum"</span><span class="p">)</span> <span class="p">}</span>
</pre></div>
</div>
<p>Once we define the proper associations, the resulting <code>@micropost</code> variable will automatically have <code>user_id</code> equal to its associated user.</p>
<p>Building the micropost through the User association doesn’t fix the security problem of having an accessible <code>user_id</code>, and
because this is such an important security concern we’ll add a failing test to catch it, as shown in <a class="ref" href="user-microposts.html#code-attr_accessible_user_id_test">Listing 10.5</a>.</p>
<div class="label" id="code-attr_accessible_user_id_test"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 10.5.</span> <span class="description">A test to ensure that the <code>user_id</code> isn’t accessible. <br /> <code>spec/models/micropost_spec.rb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="nb">require</span> <span class="s1">'spec_helper'</span>
<span class="n">describe</span> <span class="no">Micropost</span> <span class="k">do</span>
<span class="n">let</span><span class="p">(</span><span class="ss">:user</span><span class="p">)</span> <span class="p">{</span> <span class="no">FactoryGirl</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="ss">:user</span><span class="p">)</span> <span class="p">}</span>
<span class="n">before</span> <span class="p">{</span> <span class="vi">@micropost</span> <span class="o">=</span> <span class="n">user</span><span class="o">.</span><span class="n">microposts</span><span class="o">.</span><span class="n">build</span><span class="p">(</span><span class="ss">content:</span> <span class="s2">"Lorem ipsum"</span><span class="p">)</span> <span class="p">}</span>
<span class="n">subject</span> <span class="p">{</span> <span class="vi">@micropost</span> <span class="p">}</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="n">describe</span> <span class="s2">"accessible attributes"</span> <span class="k">do</span>
<span class="n">it</span> <span class="s2">"should not allow access to user_id"</span> <span class="k">do</span>
<span class="n">expect</span> <span class="k">do</span>
<span class="no">Micropost</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="ss">user_id:</span> <span class="n">user</span><span class="o">.</span><span class="n">id</span><span class="p">)</span>
<span class="k">end</span><span class="o">.</span><span class="n">to</span> <span class="n">raise_error</span><span class="p">(</span><span class="ss">ActiveModel::MassAssignmentSecurity</span><span class="o">::</span><span class="no">Error</span><span class="p">)</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">end</span>
</pre></div>
</div></div>
<p>This test verifies that calling <code>Micropost.new</code> with a nonempty <code>user_id</code> raises a mass assignment security error exception. This behavior is on by default as of Rails 3.2.3, but previous versions had it off, so you should make sure that your application is configured properly, as shown in <a class="ref" href="user-microposts.html#code-application_whitelist">Listing 10.6</a>.</p>
<div class="label" id="code-application_whitelist"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 10.6.</span> <span class="description">Ensuring that Rails throws errors on invalid mass assignment. <br /> <code>config/application.rb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="o">.</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="k">module</span> <span class="nn">SampleApp</span>
<span class="k">class</span> <span class="nc">Application</span> <span class="o"><</span> <span class="ss">Rails::Application</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="n">config</span><span class="o">.</span><span class="n">active_record</span><span class="o">.</span><span class="n">whitelist_attributes</span> <span class="o">=</span> <span class="kp">true</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="k">end</span>
<span class="k">end</span>
</pre></div>
</div></div>
<p>In the case of the Micropost model, there is only <em>one</em> attribute that needs to be editable through the web, namely, the <code>content</code> attribute, so we need to remove <code>:user_id</code> from the accessible list, as shown in <a class="ref" href="user-microposts.html#code-micropost_accessible_attribute">Listing 10.7</a>.</p>
<div class="label" id="code-micropost_accessible_attribute"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 10.7.</span> <span class="description">Making the <code>content</code> attribute (and <em>only</em> the <code>content</code> attribute) accessible. <br /> <code>app/models/micropost.rb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Micropost</span> <span class="o"><</span> <span class="ss">ActiveRecord::Base</span>
<span class="n">attr_accessible</span> <span class="ss">:content</span>
<span class="n">validates</span> <span class="ss">:user_id</span><span class="p">,</span> <span class="ss">presence:</span> <span class="kp">true</span>
<span class="k">end</span>
</pre></div>
</div></div>
<p>As seen in <a class="ref" href="user-microposts.html#table-association_methods">Table 10.1</a>, another result of the user/micropost association is <code>micropost.user</code>, which simply returns the micropost’s user. We can test this with the <code>it</code> and <code>its</code> methods as follows:</p>
<div class="code"><div class="highlight"><pre><span class="n">it</span> <span class="p">{</span> <span class="n">should</span> <span class="n">respond_to</span><span class="p">(</span><span class="ss">:user</span><span class="p">)</span> <span class="p">}</span>
<span class="n">its</span><span class="p">(</span><span class="ss">:user</span><span class="p">)</span> <span class="p">{</span> <span class="n">should</span> <span class="o">==</span> <span class="n">user</span> <span class="p">}</span>
</pre></div>
</div>
<p>The resulting Micropost model tests are shown in <a class="ref" href="user-microposts.html#code-micropost_belongs_to_user_spec">Listing 10.8</a>.</p>
<div class="label" id="code-micropost_belongs_to_user_spec"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 10.8.</span> <span class="description">Tests for the micropost’s user association. <br /> <code>spec/models/micropost_spec.rb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="nb">require</span> <span class="s1">'spec_helper'</span>
<span class="n">describe</span> <span class="no">Micropost</span> <span class="k">do</span>
<span class="n">let</span><span class="p">(</span><span class="ss">:user</span><span class="p">)</span> <span class="p">{</span> <span class="no">FactoryGirl</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="ss">:user</span><span class="p">)</span> <span class="p">}</span>
<span class="n">before</span> <span class="p">{</span> <span class="vi">@micropost</span> <span class="o">=</span> <span class="n">user</span><span class="o">.</span><span class="n">microposts</span><span class="o">.</span><span class="n">build</span><span class="p">(</span><span class="ss">content:</span> <span class="s2">"Lorem ipsum"</span><span class="p">)</span> <span class="p">}</span>
<span class="n">subject</span> <span class="p">{</span> <span class="vi">@micropost</span> <span class="p">}</span>
<span class="n">it</span> <span class="p">{</span> <span class="n">should</span> <span class="n">respond_to</span><span class="p">(</span><span class="ss">:content</span><span class="p">)</span> <span class="p">}</span>
<span class="n">it</span> <span class="p">{</span> <span class="n">should</span> <span class="n">respond_to</span><span class="p">(</span><span class="ss">:user_id</span><span class="p">)</span> <span class="p">}</span>
<span class="n">it</span> <span class="p">{</span> <span class="n">should</span> <span class="n">respond_to</span><span class="p">(</span><span class="ss">:user</span><span class="p">)</span> <span class="p">}</span>
<span class="n">its</span><span class="p">(</span><span class="ss">:user</span><span class="p">)</span> <span class="p">{</span> <span class="n">should</span> <span class="o">==</span> <span class="n">user</span> <span class="p">}</span>
<span class="n">it</span> <span class="p">{</span> <span class="n">should</span> <span class="n">be_valid</span> <span class="p">}</span>
<span class="n">describe</span> <span class="s2">"accessible attributes"</span> <span class="k">do</span>
<span class="n">it</span> <span class="s2">"should not allow access to user_id"</span> <span class="k">do</span>
<span class="n">expect</span> <span class="k">do</span>
<span class="no">Micropost</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="ss">user_id:</span> <span class="n">user</span><span class="o">.</span><span class="n">id</span><span class="p">)</span>
<span class="k">end</span><span class="o">.</span><span class="n">to</span> <span class="n">raise_error</span><span class="p">(</span><span class="ss">ActiveModel::MassAssignmentSecurity</span><span class="o">::</span><span class="no">Error</span><span class="p">)</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="n">describe</span> <span class="s2">"when user_id is not present"</span> <span class="k">do</span>
<span class="n">before</span> <span class="p">{</span> <span class="vi">@micropost</span><span class="o">.</span><span class="n">user_id</span> <span class="o">=</span> <span class="kp">nil</span> <span class="p">}</span>
<span class="n">it</span> <span class="p">{</span> <span class="n">should_not</span> <span class="n">be_valid</span> <span class="p">}</span>
<span class="k">end</span>
<span class="k">end</span>
</pre></div>
</div></div>
<p>On the User model side of the association, we’ll defer the more detailed tests to <a class="ref" href="user-microposts.html#sec-ordering_and_dependency">Section 10.1.4</a>; for now, we’ll simply test for the presence of a <code>microposts</code> attribute (<a class="ref" href="user-microposts.html#code-user_has_many_microposts_spec">Listing 10.9</a>).</p>
<div class="label" id="code-user_has_many_microposts_spec"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 10.9.</span> <span class="description">A test for the user’s <code>microposts</code> attribute. <br /> <code>spec/models/user_spec.rb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="nb">require</span> <span class="s1">'spec_helper'</span>
<span class="n">describe</span> <span class="no">User</span> <span class="k">do</span>
<span class="n">before</span> <span class="k">do</span>
<span class="vi">@user</span> <span class="o">=</span> <span class="no">User</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="nb">name</span><span class="p">:</span> <span class="s2">"Example User"</span><span class="p">,</span> <span class="ss">email:</span> <span class="s2">"user@example.com"</span><span class="p">,</span>
<span class="ss">password:</span> <span class="s2">"foobar"</span><span class="p">,</span> <span class="ss">password_confirmation:</span> <span class="s2">"foobar"</span><span class="p">)</span>
<span class="k">end</span>
<span class="n">subject</span> <span class="p">{</span> <span class="vi">@user</span> <span class="p">}</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="n">it</span> <span class="p">{</span> <span class="n">should</span> <span class="n">respond_to</span><span class="p">(</span><span class="ss">:authenticate</span><span class="p">)</span> <span class="p">}</span>
<span class="n">it</span> <span class="p">{</span> <span class="n">should</span> <span class="n">respond_to</span><span class="p">(</span><span class="ss">:microposts</span><span class="p">)</span> <span class="p">}</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="k">end</span>
</pre></div>
</div></div>
<p>After all that work, the code to implement the association is almost comically short: we can get the tests in both <a class="ref" href="user-microposts.html#code-micropost_belongs_to_user_spec">Listing 10.8</a> and <a class="ref" href="user-microposts.html#code-user_has_many_microposts_spec">Listing 10.9</a> to pass by adding just two lines: <code>belongs_to :user</code> (<a class="ref" href="user-microposts.html#code-micropost_belongs_to_user">Listing 10.10</a>) and <code>has_many :microposts</code> (<a class="ref" href="user-microposts.html#code-user_has_many_microposts">Listing 10.11</a>).</p>
<div class="label" id="code-micropost_belongs_to_user"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 10.10.</span> <span class="description">A micropost <code>belongs_to</code> a user. <br /> <code>app/models/micropost.rb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Micropost</span> <span class="o"><</span> <span class="ss">ActiveRecord::Base</span>
<span class="n">attr_accessible</span> <span class="ss">:content</span>
<span class="n">belongs_to</span> <span class="ss">:user</span>
<span class="n">validates</span> <span class="ss">:user_id</span><span class="p">,</span> <span class="ss">presence:</span> <span class="kp">true</span>
<span class="k">end</span>
</pre></div>
</div></div>
<div class="label" id="code-user_has_many_microposts"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 10.11.</span> <span class="description">A user <code>has_many</code> microposts. <br /> <code>app/models/user.rb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="k">class</span> <span class="nc">User</span> <span class="o"><</span> <span class="ss">ActiveRecord::Base</span>
<span class="n">attr_accessible</span> <span class="ss">:name</span><span class="p">,</span> <span class="ss">:email</span><span class="p">,</span> <span class="ss">:password</span><span class="p">,</span> <span class="ss">:password_confirmation</span>
<span class="n">has_secure_password</span>
<span class="n">has_many</span> <span class="ss">:microposts</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="k">end</span>
</pre></div>
</div></div>
<p>At this point, you should compare the entries in <a class="ref" href="user-microposts.html#table-association_methods">Table 10.1</a> with the code in <a class="ref" href="user-microposts.html#code-micropost_belongs_to_user_spec">Listing 10.8</a> and <a class="ref" href="user-microposts.html#code-user_has_many_microposts_spec">Listing 10.9</a> to satisfy yourself that you understand the basic nature of the associations. You should also check that the tests pass:</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> bundle <span class="nb">exec </span>rspec spec/models
</pre></div>
</div>
<div class="label" id="sec-ordering_and_dependency"></div>
<h3><a id="sec-10_1_4" href="user-microposts.html#sec-ordering_and_dependency" class="heading"><span class="number">10.1.4</span> Micropost refinements</a></h3>
<p>The test in <a class="ref" href="user-microposts.html#code-user_has_many_microposts_spec">Listing 10.9</a> of the <code>has_many</code> association doesn’t test for much—it merely verifies the <em>existence</em> of a <code>microposts</code> attribute. In this section, we’ll add <em>ordering</em> and <em>dependency</em> to microposts, while also testing that the <code>user.microposts</code> method actually returns an array of microposts.</p>
<p>We will need to construct some microposts in the User model test, which means that we should make a micropost factory at this point. To do this, we need a way to make an association in Factory Girl. Happily, this is easy, as seen in <a class="ref" href="user-microposts.html#code-micropost_factory">Listing 10.12</a>.</p>
<div class="label" id="code-micropost_factory"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 10.12.</span> <span class="description">The complete factory file, including a new factory for microposts. <br /> <code>spec/factories.rb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="no">FactoryGirl</span><span class="o">.</span><span class="n">define</span> <span class="k">do</span>
<span class="n">factory</span> <span class="ss">:user</span> <span class="k">do</span>
<span class="n">sequence</span><span class="p">(</span><span class="ss">:name</span><span class="p">)</span> <span class="p">{</span> <span class="o">|</span><span class="n">n</span><span class="o">|</span> <span class="s2">"Person </span><span class="si">#{</span><span class="n">n</span><span class="si">}</span><span class="s2">"</span> <span class="p">}</span>
<span class="n">sequence</span><span class="p">(</span><span class="ss">:email</span><span class="p">)</span> <span class="p">{</span> <span class="o">|</span><span class="n">n</span><span class="o">|</span> <span class="s2">"person_</span><span class="si">#{</span><span class="n">n</span><span class="si">}</span><span class="s2">@example.com"</span><span class="p">}</span>
<span class="n">password</span> <span class="s2">"foobar"</span>
<span class="n">password_confirmation</span> <span class="s2">"foobar"</span>
<span class="n">factory</span> <span class="ss">:admin</span> <span class="k">do</span>
<span class="n">admin</span> <span class="kp">true</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="n">factory</span> <span class="ss">:micropost</span> <span class="k">do</span>
<span class="n">content</span> <span class="s2">"Lorem ipsum"</span>
<span class="n">user</span>
<span class="k">end</span>
<span class="k">end</span>
</pre></div>
</div></div>
<p>Here we tell Factory Girl about the micropost’s associated user just by including a user in the definition of the factory:</p>
<div class="code"><div class="highlight"><pre><span class="n">factory</span> <span class="ss">:micropost</span> <span class="k">do</span>
<span class="n">content</span> <span class="s2">"Lorem ipsum"</span>
<span class="n">user</span>
<span class="k">end</span>
</pre></div>
</div>
<p>As we’ll see in the next section, this allows us to define factory microposts as follows:</p>
<div class="code"><div class="highlight"><pre><span class="no">FactoryGirl</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="ss">:micropost</span><span class="p">,</span> <span class="ss">user:</span> <span class="vi">@user</span><span class="p">,</span> <span class="ss">created_at:</span> <span class="mi">1</span><span class="o">.</span><span class="n">day</span><span class="o">.</span><span class="n">ago</span><span class="p">)</span>
</pre></div>
</div>
<div class="label" id="sec-default_scope"></div>
<h4><a id="sec-10_1_4_1" href="user-microposts.html#sec-default_scope" class="heading">Default scope</a></h4>
<p>By default, using <code>user.microposts</code> to pull a user’s microposts from the database makes no guarantees about the order of the posts, but (following the convention of blogs and Twitter) we want the microposts to come out in reverse order of when they were created, i.e., most recent first. To test this ordering, we first create a couple of microposts as follows:</p>
<div class="code"><div class="highlight"><pre><span class="no">FactoryGirl</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="ss">:micropost</span><span class="p">,</span> <span class="ss">user:</span> <span class="vi">@user</span><span class="p">,</span> <span class="ss">created_at:</span> <span class="mi">1</span><span class="o">.</span><span class="n">day</span><span class="o">.</span><span class="n">ago</span><span class="p">)</span>
<span class="no">FactoryGirl</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="ss">:micropost</span><span class="p">,</span> <span class="ss">user:</span> <span class="vi">@user</span><span class="p">,</span> <span class="ss">created_at:</span> <span class="mi">1</span><span class="o">.</span><span class="n">hour</span><span class="o">.</span><span class="n">ago</span><span class="p">)</span>
</pre></div>
</div>
<p>Here we indicate (using the time helpers discussed in <a class="ref" href="sign-in-sign-out.html#sidebar-time_helpers">Box 8.1</a>) that the second post was created more recently, i.e., <code>1.hour.ago</code>, while the first post was created <code>1.day.ago</code>. Note how convenient the use of Factory Girl is: not only can we assign the user using mass assignment (since factories bypass <code>attr_accessible</code>), we can also set <code>created_at</code> manually, which Active Record won’t allow us to do. (Recall that <code>created_at</code> and <code>updated_at</code> are “magic” columns, automatically set to the proper creation and update timestamps, so any explicit initialization values are overwritten by the magic.)</p>
<p>Most database adapters (including the one for SQLite) return the microposts in order of their ids, so we can arrange for an initial test that almost certainly fails using the code in <a class="ref" href="user-microposts.html#code-micropost_ordering_test">Listing 10.13</a>. This uses the <code>let!</code> (read “let bang”) method in place of <code>let</code>; the reason is that <code>let</code> variables are <em>lazy</em>, meaning that they only spring into existence when referenced. The problem is that we want the microposts to exist immediately, so that the timestamps are in the right order and so that <code>@user.microposts</code> isn’t empty. We accomplish this with <code>let!</code>, which forces the corresponding variable to come into existence immediately.</p>
<div class="label" id="code-micropost_ordering_test"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 10.13.</span> <span class="description">Testing the order of a user’s microposts. <br /> <code>spec/models/user_spec.rb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="nb">require</span> <span class="s1">'spec_helper'</span>
<span class="n">describe</span> <span class="no">User</span> <span class="k">do</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="n">describe</span> <span class="s2">"micropost associations"</span> <span class="k">do</span>
<span class="n">before</span> <span class="p">{</span> <span class="vi">@user</span><span class="o">.</span><span class="n">save</span> <span class="p">}</span>
<span class="n">let!</span><span class="p">(</span><span class="ss">:older_micropost</span><span class="p">)</span> <span class="k">do</span>
<span class="no">FactoryGirl</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="ss">:micropost</span><span class="p">,</span> <span class="ss">user:</span> <span class="vi">@user</span><span class="p">,</span> <span class="ss">created_at:</span> <span class="mi">1</span><span class="o">.</span><span class="n">day</span><span class="o">.</span><span class="n">ago</span><span class="p">)</span>
<span class="k">end</span>
<span class="n">let!</span><span class="p">(</span><span class="ss">:newer_micropost</span><span class="p">)</span> <span class="k">do</span>
<span class="no">FactoryGirl</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="ss">:micropost</span><span class="p">,</span> <span class="ss">user:</span> <span class="vi">@user</span><span class="p">,</span> <span class="ss">created_at:</span> <span class="mi">1</span><span class="o">.</span><span class="n">hour</span><span class="o">.</span><span class="n">ago</span><span class="p">)</span>
<span class="k">end</span>
<span class="n">it</span> <span class="s2">"should have the right microposts in the right order"</span> <span class="k">do</span>
<span class="vi">@user</span><span class="o">.</span><span class="n">microposts</span><span class="o">.</span><span class="n">should</span> <span class="o">==</span> <span class="o">[</span><span class="n">newer_micropost</span><span class="p">,</span> <span class="n">older_micropost</span><span class="o">]</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">end</span>
</pre></div>
</div></div>
<p>The key line here is</p>
<div class="code"><div class="highlight"><pre><span class="vi">@user</span><span class="o">.</span><span class="n">microposts</span><span class="o">.</span><span class="n">should</span> <span class="o">==</span> <span class="o">[</span><span class="n">newer_micropost</span><span class="p">,</span> <span class="n">older_micropost</span><span class="o">]</span>
</pre></div>
</div>
<p>indicating that the posts should be ordered newest first. This should fail because by default the posts will be ordered by id, i.e., <code>[older_micropost, newer_micropost]</code>. This test also verifies the basic correctness of the <code>has_many</code> association itself, by checking (as indicated in <a class="ref" href="user-microposts.html#table-association_methods">Table 10.1</a>) that <code>user.microposts</code> is an array of microposts.</p>
<p>To get the ordering test to pass, we use a Rails facility called <code>default_scope</code> with an <code>:order</code> parameter, as shown in <a class="ref" href="user-microposts.html#code-micropost_ordering">Listing 10.14</a>. (This is our first example of the notion of <em>scope</em>. We will learn about scope in a more general context in <a class="ref" href="following-users.html#top">Chapter 11</a>.)</p>
<div class="label" id="code-micropost_ordering"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 10.14.</span> <span class="description">Ordering the microposts with <code>default_scope</code>. <br /> <code>app/models/micropost.rb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Micropost</span> <span class="o"><</span> <span class="ss">ActiveRecord::Base</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="n">default_scope</span> <span class="ss">order:</span> <span class="s1">'microposts.created_at DESC'</span>
<span class="k">end</span>
</pre></div>
</div></div>
<p>The order here is <code>’microposts.created_at DESC’</code>, where <code>DESC</code> is SQL for “descending”, i.e., in descending order from newest to oldest.</p>
<div class="label" id="sec-dependent_destroy"></div>
<h4><a id="sec-10_1_4_2" href="user-microposts.html#sec-dependent_destroy" class="heading">Dependent: destroy</a></h4>
<p>Apart from proper ordering, there is a second refinement we’d like to add to microposts. Recall from <a class="ref" href="updating-showing-and-deleting-users.html#sec-destroying_users">Section 9.4</a> that site administrators have the power to <em>destroy</em> users. It stands to reason that, if a user is destroyed, the user’s microposts should be destroyed as well. We can test for this by first destroying a micropost’s user and then verifying that the associated microposts are no longer in the database.</p>
<p>In order to test destroying microposts properly, we first need to capture a given user’s posts in a local variable, and then destroy the user. A straighforward implementation looks like this:</p>
<div class="code"><div class="highlight"><pre><span class="n">microposts</span> <span class="o">=</span> <span class="vi">@user</span><span class="o">.</span><span class="n">microposts</span>
<span class="vi">@user</span><span class="o">.</span><span class="n">destroy</span>
<span class="n">microposts</span><span class="o">.</span><span class="n">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">micropost</span><span class="o">|</span>
<span class="c1"># Make sure the micropost doesn't appear in the database.</span>
<span class="k">end</span>
</pre></div>
</div>
<p>Unfortunately, this doesn’t work, due to a subtlety about Ruby arrays. Array assignment in Ruby copies a <em>reference</em> to the array, not the full array itself, which means that changes to the original array also affect the copy. For example, suppose we create an array, assign a second variable to it, and then reverse the first array in place using the <code>reverse!</code> method:</p>
<div class="code"><div class="highlight"><pre><span class="go">$ rails console</span>
<span class="gp">>> </span><span class="n">a</span> <span class="o">=</span> <span class="o">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="o">]</span>
<span class="go">=> [1, 2, 3]</span>
<span class="gp">>> </span><span class="n">b</span> <span class="o">=</span> <span class="n">a</span>
<span class="go">=> [1, 2, 3]</span>
<span class="gp">>> </span><span class="n">a</span><span class="o">.</span><span class="n">reverse!</span>
<span class="go">=> [3, 2, 1]</span>
<span class="gp">>> </span><span class="n">a</span>
<span class="go">=> [3, 2, 1]</span>
<span class="gp">>> </span><span class="n">b</span>
<span class="go">=> [3, 2, 1]</span>
</pre></div>
</div>
<p>Somewhat suprisingly, here <code>b</code> gets reversed as well as <code>a</code>. This is because both <code>a</code> and <code>b</code> point to the same array. (The same thing happens with other Ruby data structures, such as strings and hashes.)</p>
<p>In the case of a user’s microposts, we would have this:</p>
<div class="code"><div class="highlight"><pre><span class="go">$ rails console --sandbox</span>
<span class="gp">>> </span><span class="vi">@user</span> <span class="o">=</span> <span class="no">User</span><span class="o">.</span><span class="n">first</span>
<span class="gp">>> </span><span class="n">microposts</span> <span class="o">=</span> <span class="vi">@user</span><span class="o">.</span><span class="n">microposts</span>
<span class="gp">>> </span><span class="vi">@user</span><span class="o">.</span><span class="n">destroy</span>
<span class="gp">>> </span><span class="n">microposts</span>
<span class="go">=> []</span>
</pre></div>
</div>
<p>(Because we haven’t implemented the destruction of associated microposts yet, this code won’t currently work, and is included only to illustrate the principle.) Here we see that destroying a user leaves the <code>microposts</code> variable with no elements; i.e., it’s the empty array <code>[]</code>.</p>
<p>This behavior means that we must take great care when making duplicates of Ruby objects. To duplicate relatively simple objects such as arrays, we can use the <code>dup</code> method:</p>
<div class="code"><div class="highlight"><pre><span class="go">$ rails console</span>
<span class="gp">>> </span><span class="n">a</span> <span class="o">=</span> <span class="o">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="o">]</span>
<span class="go">=> [1, 2, 3]</span>
<span class="gp">>> </span><span class="n">b</span> <span class="o">=</span> <span class="n">a</span><span class="o">.</span><span class="n">dup</span>
<span class="go">=> [1, 2, 3]</span>
<span class="gp">>> </span><span class="n">a</span><span class="o">.</span><span class="n">reverse!</span>
<span class="go">=> [3, 2, 1]</span>
<span class="gp">>> </span><span class="n">a</span>
<span class="go">=> [3, 2, 1]</span>
<span class="gp">>> </span><span class="n">b</span>
<span class="go">=> [1, 2, 3]</span>
</pre></div>
</div>
<p>(This is known as a “shallow copy”. Making a “deep copy” is a much more difficult problem, and in fact has no general solution, but dropping “ruby deep copy” into a search engine should be enough to get you started if you need to copy a more complicated structure such as a nested array.) Applying the <code>dup</code> method to the user’s microposts gives us code like this:</p>
<div class="code"><div class="highlight"><pre><span class="n">microposts</span> <span class="o">=</span> <span class="vi">@user</span><span class="o">.</span><span class="n">microposts</span><span class="o">.</span><span class="n">dup</span>
<span class="vi">@user</span><span class="o">.</span><span class="n">destroy</span>
<span class="n">microposts</span><span class="o">.</span><span class="n">should_not</span> <span class="n">be_empty</span>
<span class="n">microposts</span><span class="o">.</span><span class="n">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">micropost</span><span class="o">|</span>
<span class="c1"># Make sure the micropost doesn't appear in the database.</span>
<span class="k">end</span>
</pre></div>
</div>
<p>Here we’ve included the line</p>
<div class="code"><div class="highlight"><pre><span class="n">microposts</span><span class="o">.</span><span class="n">should_not</span> <span class="n">be_empty</span>
</pre></div>
</div>
<p>as a safety check to catch any errors should the <code>dup</code> ever be accidentally removed.<sup class="footnote" id="fnref-10_3"><a href="#fn-10_3">3</a></sup> The full implementation appears in <a class="ref" href="user-microposts.html#code-micropost_dependency_test">Listing 10.15</a>.</p>
<div class="label" id="code-micropost_dependency_test"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 10.15.</span> <span class="description">Testing that microposts are destroyed when users are. <br /> <code>spec/models/user_spec.rb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="nb">require</span> <span class="s1">'spec_helper'</span>
<span class="n">describe</span> <span class="no">User</span> <span class="k">do</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="n">describe</span> <span class="s2">"micropost associations"</span> <span class="k">do</span>
<span class="n">before</span> <span class="p">{</span> <span class="vi">@user</span><span class="o">.</span><span class="n">save</span> <span class="p">}</span>
<span class="n">let!</span><span class="p">(</span><span class="ss">:older_micropost</span><span class="p">)</span> <span class="k">do</span>
<span class="no">FactoryGirl</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="ss">:micropost</span><span class="p">,</span> <span class="ss">user:</span> <span class="vi">@user</span><span class="p">,</span> <span class="ss">created_at:</span> <span class="mi">1</span><span class="o">.</span><span class="n">day</span><span class="o">.</span><span class="n">ago</span><span class="p">)</span>
<span class="k">end</span>
<span class="n">let!</span><span class="p">(</span><span class="ss">:newer_micropost</span><span class="p">)</span> <span class="k">do</span>
<span class="no">FactoryGirl</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="ss">:micropost</span><span class="p">,</span> <span class="ss">user:</span> <span class="vi">@user</span><span class="p">,</span> <span class="ss">created_at:</span> <span class="mi">1</span><span class="o">.</span><span class="n">hour</span><span class="o">.</span><span class="n">ago</span><span class="p">)</span>
<span class="k">end</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="n">it</span> <span class="s2">"should destroy associated microposts"</span> <span class="k">do</span>
<span class="n">microposts</span> <span class="o">=</span> <span class="vi">@user</span><span class="o">.</span><span class="n">microposts</span><span class="o">.</span><span class="n">dup</span>
<span class="vi">@user</span><span class="o">.</span><span class="n">destroy</span>
<span class="n">microposts</span><span class="o">.</span><span class="n">should_not</span> <span class="n">be_empty</span>
<span class="n">microposts</span><span class="o">.</span><span class="n">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">micropost</span><span class="o">|</span>
<span class="no">Micropost</span><span class="o">.</span><span class="n">find_by_id</span><span class="p">(</span><span class="n">micropost</span><span class="o">.</span><span class="n">id</span><span class="p">)</span><span class="o">.</span><span class="n">should</span> <span class="n">be_nil</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="k">end</span>
</pre></div>
</div></div>
<p>Here we have used <code>Micropost.find_by_id</code>, which returns <code>nil</code> if the record is not found, whereas <code>Micropost.find</code> raises an exception on failure, which is a bit harder to test for. (In case you’re curious,</p>
<div class="code"><div class="highlight"><pre><span class="nb">lambda</span> <span class="k">do</span>
<span class="no">Micropost</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="n">micropost</span><span class="o">.</span><span class="n">id</span><span class="p">)</span>
<span class="k">end</span><span class="o">.</span><span class="n">should</span> <span class="n">raise_error</span><span class="p">(</span><span class="ss">ActiveRecord::RecordNotFound</span><span class="p">)</span>
</pre></div>
</div>
<p>does the trick in this case.)</p>
<p>The application code to get <a class="ref" href="user-microposts.html#code-micropost_dependency_test">Listing 10.15</a> to pass is less than one line; in fact, it’s just an option to the <code>has_many</code> association method, as shown in <a class="ref" href="user-microposts.html#code-micropost_dependency">Listing 10.16</a>.</p>
<div class="label" id="code-micropost_dependency"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 10.16.</span> <span class="description">Ensuring that a user’s microposts are destroyed along with the user. <br /> <code>app/models/user.rb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="k">class</span> <span class="nc">User</span> <span class="o"><</span> <span class="ss">ActiveRecord::Base</span>
<span class="n">attr_accessible</span> <span class="ss">:name</span><span class="p">,</span> <span class="ss">:email</span><span class="p">,</span> <span class="ss">:password</span><span class="p">,</span> <span class="ss">:password_confirmation</span>
<span class="n">has_secure_password</span>
<span class="n">has_many</span> <span class="ss">:microposts</span><span class="p">,</span> <span class="ss">dependent:</span> <span class="ss">:destroy</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="k">end</span>
</pre></div>
</div></div>
<p>Here the option <code>dependent: :destroy</code> in</p>
<div class="code"><div class="highlight"><pre><span class="n">has_many</span> <span class="ss">:microposts</span><span class="p">,</span> <span class="ss">dependent:</span> <span class="ss">:destroy</span>
</pre></div>
</div>
<p>arranges for the dependent microposts (i.e., the ones belonging to the given user) to be destroyed when the user itself is destroyed. This prevents userless microposts from being stranded in the database when admins choose to remove users from the system.</p>
<p>With that, the final form of the user/micropost association is in place, and all the tests should be passing:</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> bundle <span class="nb">exec </span>rspec spec/
</pre></div>
</div>
<div class="label" id="sec-micropost_validations"></div>
<h3><a id="sec-10_1_5" href="user-microposts.html#sec-micropost_validations" class="heading"><span class="number">10.1.5</span> Content validations</a></h3>
<p>Before leaving the Micropost model, we’ll add validations for the micropost <code>content</code> (following the example from <a class="ref" href="a-demo-app.html#sec-putting_the_micro_in_microposts">Section 2.3.2</a>). Like the <code>user_id</code>, the <code>content</code> attribute must be present, and it is further constrained to be no longer than 140 characters, making it an honest <em>micro</em>post. The tests generally follow the examples from the User model validation tests in <a class="ref" href="modeling-users.html#sec-user_validations">Section 6.2</a>, as shown in <a class="ref" href="user-microposts.html#code-micropost_validations_tests">Listing 10.17</a>.</p>
<div class="label" id="code-micropost_validations_tests"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 10.17.</span> <span class="description">Tests for the Micropost model validations. <br /> <code>spec/models/micropost_spec.rb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="nb">require</span> <span class="s1">'spec_helper'</span>
<span class="n">describe</span> <span class="no">Micropost</span> <span class="k">do</span>
<span class="n">let</span><span class="p">(</span><span class="ss">:user</span><span class="p">)</span> <span class="p">{</span> <span class="no">FactoryGirl</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="ss">:user</span><span class="p">)</span> <span class="p">}</span>
<span class="n">before</span> <span class="p">{</span> <span class="vi">@micropost</span> <span class="o">=</span> <span class="n">user</span><span class="o">.</span><span class="n">microposts</span><span class="o">.</span><span class="n">build</span><span class="p">(</span><span class="ss">content:</span> <span class="s2">"Lorem ipsum"</span><span class="p">)</span> <span class="p">}</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="n">describe</span> <span class="s2">"when user_id is not present"</span> <span class="k">do</span>
<span class="n">before</span> <span class="p">{</span> <span class="vi">@micropost</span><span class="o">.</span><span class="n">user_id</span> <span class="o">=</span> <span class="kp">nil</span> <span class="p">}</span>
<span class="n">it</span> <span class="p">{</span> <span class="n">should_not</span> <span class="n">be_valid</span> <span class="p">}</span>
<span class="k">end</span>
<span class="n">describe</span> <span class="s2">"with blank content"</span> <span class="k">do</span>
<span class="n">before</span> <span class="p">{</span> <span class="vi">@micropost</span><span class="o">.</span><span class="n">content</span> <span class="o">=</span> <span class="s2">" "</span> <span class="p">}</span>
<span class="n">it</span> <span class="p">{</span> <span class="n">should_not</span> <span class="n">be_valid</span> <span class="p">}</span>
<span class="k">end</span>
<span class="n">describe</span> <span class="s2">"with content that is too long"</span> <span class="k">do</span>
<span class="n">before</span> <span class="p">{</span> <span class="vi">@micropost</span><span class="o">.</span><span class="n">content</span> <span class="o">=</span> <span class="s2">"a"</span> <span class="o">*</span> <span class="mi">141</span> <span class="p">}</span>
<span class="n">it</span> <span class="p">{</span> <span class="n">should_not</span> <span class="n">be_valid</span> <span class="p">}</span>
<span class="k">end</span>
<span class="k">end</span>
</pre></div>
</div></div>
<p>As in <a class="ref" href="modeling-users.html#sec-user_validations">Section 6.2</a>, the code in <a class="ref" href="user-microposts.html#code-micropost_validations_tests">Listing 10.17</a> uses string multiplication to test the micropost length validation:</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> rails console
<span class="gp">></span>> <span class="s2">"a"</span> * 10
<span class="go">=> "aaaaaaaaaa"</span>
<span class="gp">></span>> <span class="s2">"a"</span> * 141
<span class="go">=> "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</span>
<span class="go">aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"</span>
</pre></div>
</div>
<p>The application code is a one-liner:</p>
<div class="code"><div class="highlight"><pre><span class="n">validates</span> <span class="ss">:content</span><span class="p">,</span> <span class="ss">presence:</span> <span class="kp">true</span><span class="p">,</span> <span class="ss">length:</span> <span class="p">{</span> <span class="ss">maximum:</span> <span class="mi">140</span> <span class="p">}</span>
</pre></div>
</div>
<p>The resulting Micropost model is shown in <a class="ref" href="user-microposts.html#code-micropost_validations">Listing 10.18</a>.</p>
<div class="label" id="code-micropost_validations"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 10.18.</span> <span class="description">The Micropost model validations. <br /> <code>app/models/micropost.rb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Micropost</span> <span class="o"><</span> <span class="ss">ActiveRecord::Base</span>
<span class="n">attr_accessible</span> <span class="ss">:content</span>
<span class="n">belongs_to</span> <span class="ss">:user</span>
<span class="n">validates</span> <span class="ss">:content</span><span class="p">,</span> <span class="ss">presence:</span> <span class="kp">true</span><span class="p">,</span> <span class="ss">length:</span> <span class="p">{</span> <span class="ss">maximum:</span> <span class="mi">140</span> <span class="p">}</span>
<span class="n">validates</span> <span class="ss">:user_id</span><span class="p">,</span> <span class="ss">presence:</span> <span class="kp">true</span>
<span class="n">default_scope</span> <span class="ss">order:</span> <span class="s1">'microposts.created_at DESC'</span>
<span class="k">end</span>
</pre></div>
</div></div>
<div class="label" id="sec-showing_microposts"></div>
<h2><a id="sec-10_2" href="user-microposts.html#sec-showing_microposts" class="heading"><span class="number">10.2</span> Showing microposts</a></h2>
<p>Although we don’t yet have a way to create microposts through the web—that comes in <a class="ref" href="user-microposts.html#sec-creating_microposts">Section 10.3.2</a>—that won’t stop us from displaying them (and testing that display). Following Twitter’s lead, we’ll plan to display a user’s microposts not on a separate microposts <code>index</code> page, but rather directly on the user <code>show</code> page itself, as mocked up in <a class="ref" href="user-microposts.html#fig-user_microposts_mockup">Figure 10.4</a>. We’ll start with fairly simple ERb templates for adding a micropost display to the user profile, and then we’ll add microposts to the sample data populator from <a class="ref" href="updating-showing-and-deleting-users.html#sec-sample_users">Section 9.3.2</a> so that we have something to display.</p>
<div class="label" id="fig-user_microposts_mockup"></div>
<div class="figure"><div class="center"><span class="graphic"><img src="images/figures/user_microposts_mockup_bootstrap.png" alt="user_microposts_mockup_bootstrap" /></span></div><div class="caption"><span class="header">Figure 10.4: </span><span class="description">A mockup of a profile page with microposts. <a href="http://railstutorial.org/images/figures/user_microposts_mockup_bootstrap-full.png">(full size)</a></span></div></div>
<p>As with the discussion of the signin machinery in <a class="ref" href="sign-in-sign-out.html#sec-remember_me">Section 8.2.1</a>, <a class="ref" href="user-microposts.html#sec-augmenting_the_user_show_page">Section 10.2.1</a> will often push several elements onto the <a href="http://en.wikipedia.org/wiki/Stack_(data_structure)">stack</a> at a time, and then pop them off one by one. If you start getting bogged down, be patient; there’s some nice payoff in <a class="ref" href="user-microposts.html#sec-sample_microposts">Section 10.2.2</a>.</p>
<div class="label" id="sec-augmenting_the_user_show_page"></div>
<h3><a id="sec-10_2_1" href="user-microposts.html#sec-augmenting_the_user_show_page" class="heading"><span class="number">10.2.1</span> Augmenting the user show page</a></h3>
<p>We begin with tests for displaying the user’s microposts, which we’ll create in the request spec for Users. Our strategy is to create a couple of factory microposts associated with the user, and then verify that the show page contains each post’s content. We’ll also verify that, as in <a class="ref" href="user-microposts.html#fig-user_microposts_mockup">Figure 10.4</a>, the total number of microposts also gets displayed.</p>
<p>We can create the posts with the <code>let</code> method, but as in <a class="ref" href="user-microposts.html#code-micropost_ordering_test">Listing 10.13</a> we want the association to exist immediately so that the posts appear on the user show page. To accomplish this, we use the <code>let!</code> variant:</p>
<div class="code"><div class="highlight"><pre><span class="n">let</span><span class="p">(</span><span class="ss">:user</span><span class="p">)</span> <span class="p">{</span> <span class="no">FactoryGirl</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="ss">:user</span><span class="p">)</span> <span class="p">}</span>
<span class="n">let!</span><span class="p">(</span><span class="ss">:m1</span><span class="p">)</span> <span class="p">{</span> <span class="no">FactoryGirl</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="ss">:micropost</span><span class="p">,</span> <span class="ss">user:</span> <span class="n">user</span><span class="p">,</span> <span class="ss">content:</span> <span class="s2">"Foo"</span><span class="p">)</span> <span class="p">}</span>
<span class="n">let!</span><span class="p">(</span><span class="ss">:m2</span><span class="p">)</span> <span class="p">{</span> <span class="no">FactoryGirl</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="ss">:micropost</span><span class="p">,</span> <span class="ss">user:</span> <span class="n">user</span><span class="p">,</span> <span class="ss">content:</span> <span class="s2">"Bar"</span><span class="p">)</span> <span class="p">}</span>
<span class="n">before</span> <span class="p">{</span> <span class="n">visit</span> <span class="n">user_path</span><span class="p">(</span><span class="n">user</span><span class="p">)</span> <span class="p">}</span>
</pre></div>
</div>
<p>With the microposts so defined, we can test for their appearance on the profile page using the code in <a class="ref" href="user-microposts.html#code-user_show_microposts_test">Listing 10.19</a>.</p>
<div class="label" id="code-user_show_microposts_test"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 10.19.</span> <span class="description">A test for showing microposts on the user <code>show</code> page. <br /> <code>spec/requests/user_pages_spec.rb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="nb">require</span> <span class="s1">'spec_helper'</span>
<span class="n">describe</span> <span class="s2">"User pages"</span> <span class="k">do</span>