forked from mhartl/rails_tutorial_translation_2nd_ed
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfollowing-users.html
2537 lines (1671 loc) · 261 KB
/
following-users.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>following-users</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></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-11" href="following-users.html#top" class="heading"><span class="number">Chapter 11</span> Following users</a></h1>
<p>In this chapter, we will complete the core sample application by adding a social layer that allows users to follow (and unfollow) other users, resulting in each user’s Home page displaying a status feed of the followed users’ microposts. We will also make views to display both a user’s followers and the users each user is following. We will learn how to model relationships between users in <a class="ref" href="following-users.html#sec-the_relationship_model">Section 11.1</a>, and then make the web interface in <a class="ref" href="following-users.html#sec-a_web_interface_for_following_and_followers">Section 11.2</a> (including an introduction to Ajax). Finally, we’ll end by developing a fully functional status feed in <a class="ref" href="following-users.html#sec-the_status_feed">Section 11.3</a>.</p>
<p>This final chapter contains some of the most challenging material in the tutorial, including some Ruby/SQL trickery to make the status feed. Through these examples, you will see how Rails can handle even rather intricate data models, which should serve you well as you go on to develop your own applications with their own specific requirements. To help with the transition from tutorial to independent development, <a class="ref" href="following-users.html#sec-following_conclusion">Section 11.4</a> contains suggested extensions to the core sample application, along with pointers to more advanced resources.</p>
<p>As usual, Git users should create a new topic branch:</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> git checkout -b following-users
</pre></div>
</div>
<p>Because the material in this chapter is particularly challenging, before writing any code we’ll pause for a moment and take a tour of the interface. As in previous chapters, at this early stage we’ll represent pages using mockups.<sup class="footnote" id="fnref-11_1"><a href="#fn-11_1">1</a></sup> The full page flow runs as follows: a user (John Calvin) starts at his profile page (<a class="ref" href="following-users.html#fig-page_flow_profile_mockup">Figure 11.1</a>) and navigates to the Users page (<a class="ref" href="following-users.html#fig-page_flow_user_index_mockup">Figure 11.2</a>) to select a user to follow. Calvin navigates to the profile of a second user, Thomas Hobbes (<a class="ref" href="following-users.html#fig-page_flow_other_profile_follow_button_mockup">Figure 11.3</a>), clicking on the “Follow” button to follow that user. This changes the “Follow” button to “Unfollow”, and increments Hobbes’s “followers” count by one (<a class="ref" href="following-users.html#fig-page_flow_other_profile_unfollow_button_mockup">Figure 11.4</a>). Navigating to his home page, Calvin now sees an incremented “following” count and finds Hobbes’s microposts in his status feed (<a class="ref" href="following-users.html#fig-page_flow_home_page_feed_mockup">Figure 11.5</a>). The rest of this chapter is dedicated to making this page flow actually work.</p>
<div class="label" id="fig-page_flow_profile_mockup"></div>
<div class="figure"><div class="center"><span class="graphic"><img src="images/figures/page_flow_profile_mockup_bootstrap.png" alt="page_flow_profile_mockup_bootstrap" /></span></div><div class="caption"><span class="header">Figure 11.1: </span><span class="description">The current user’s profile. <a href="http://railstutorial.org/images/figures/page_flow_profile_mockup_bootstrap-full.png">(full size)</a></span></div></div>
<div class="label" id="fig-page_flow_user_index_mockup"></div>
<div class="figure"><div class="center"><span class="graphic"><img src="images/figures/page_flow_user_index_mockup_bootstrap.png" alt="page_flow_user_index_mockup_bootstrap" /></span></div><div class="caption"><span class="header">Figure 11.2: </span><span class="description">Finding a user to follow. <a href="http://railstutorial.org/images/figures/page_flow_user_index_mockup_bootstrap-full.png">(full size)</a></span></div></div>
<div class="label" id="fig-page_flow_other_profile_follow_button_mockup"></div>
<div class="figure"><div class="center"><span class="graphic"><img src="images/figures/page_flow_other_profile_follow_button_mockup_bootstrap.png" alt="page_flow_other_profile_follow_button_mockup_bootstrap" /></span></div><div class="caption"><span class="header">Figure 11.3: </span><span class="description">The profile of a user to follow, with a follow button. <a href="http://railstutorial.org/images/figures/page_flow_other_profile_follow_button_mockup_bootstrap-full.png">(full size)</a></span></div></div>
<div class="label" id="fig-page_flow_other_profile_unfollow_button_mockup"></div>
<div class="figure"><div class="center"><span class="graphic"><img src="images/figures/page_flow_other_profile_unfollow_button_mockup_bootstrap.png" alt="page_flow_other_profile_unfollow_button_mockup_bootstrap" /></span></div><div class="caption"><span class="header">Figure 11.4: </span><span class="description">A profile with an unfollow button and incremented followers count. <a href="http://railstutorial.org/images/figures/page_flow_other_profile_unfollow_button_mockup_bootstrap-full.png">(full size)</a></span></div></div>
<div class="label" id="fig-page_flow_home_page_feed_mockup"></div>
<div class="figure"><div class="center"><span class="graphic"><img src="images/figures/page_flow_home_page_feed_mockup_bootstrap.png" alt="page_flow_home_page_feed_mockup_bootstrap" /></span></div><div class="caption"><span class="header">Figure 11.5: </span><span class="description">The Home page with status feed and incremented following count. <a href="http://railstutorial.org/images/figures/page_flow_home_page_feed_mockup_bootstrap-full.png">(full size)</a></span></div></div>
<div class="label" id="sec-the_relationship_model"></div>
<h2><a id="sec-11_1" href="following-users.html#sec-the_relationship_model" class="heading"><span class="number">11.1</span> The Relationship model</a></h2>
<p>Our first step in implementing following users is to construct a data model, which is not as straightforward as it seems. Naïvely, it seems that a <code>has_many</code> relationship should do: a user <code>has_many</code> followed users and <code>has_many</code> followers. As we will see, there is a problem with this approach, and we’ll learn how to fix it using <code>has_many through</code>. It’s likely that many of the ideas in this section won’t seem obvious at first, and it may take a while for the rather complicated data model to sink in. If you find yourself getting confused, try pushing forward to the end; then, read the section a second time through to see if things are clearer.</p>
<div class="label" id="sec-a_problem_with_the_data_model"></div>
<h3><a id="sec-11_1_1" href="following-users.html#sec-a_problem_with_the_data_model" class="heading"><span class="number">11.1.1</span> A problem with the data model (and a solution)</a></h3>
<p>As a first step toward constructing a data model for following users, let’s examine a typical case. For instance, consider a user who follows a second user: we could say that, e.g., Calvin is following Hobbes, and Hobbes is followed by Calvin, so that Calvin is the <em>follower</em> and Hobbes is <em>followed</em>. Using Rails’ default pluralization convention, the set of all users following a given user is that user’s <em>followers</em>, and <code>user.followers</code> is an array of those users. Unfortunately, the reverse doesn’t work: by default, the set of all followed users would be called the <em>followeds</em>, which is ungrammatical and clumsy. We could call them <em>following</em>, but that’s ambiguous: in normal English, a “following” is the set of people following <em>you</em>, i.e., your followers—exactly the opposite of the intended meaning. Although we will use “following” as a label, as in “50 following, 75 followers”, we’ll use “followed users” for the users themselves, with a corresponding <code>user.followed_users</code> array.<sup class="footnote" id="fnref-11_2"><a href="#fn-11_2">2</a></sup></p>
<p>This discussion suggests modeling the followed users as in <a class="ref" href="following-users.html#fig-naive_user_has_many_following">Figure 11.6</a>, with a <code>followed_users</code> table and a <code>has_many</code> association. Since <code>user.followed_users</code> should be an array of users, each row of the <code>followed_users</code> table would need to be a user, as identified by the <code>followed_id</code>, together with the <code>follower_id</code> to establish the association.<sup class="footnote" id="fnref-11_3"><a href="#fn-11_3">3</a></sup> In addition, since each row is a user, we would need to include the user’s other attributes, including the name, password, etc.</p>
<div class="label" id="fig-naive_user_has_many_following"></div>
<div class="figure"><div class="center"><span class="graphic"><img src="images/figures/naive_user_has_many_followed_users.png" alt="naive_user_has_many_followed_users" /></span></div><div class="caption"><span class="header">Figure 11.6: </span><span class="description">A naïve implementation of user following.</span></div></div>
<p>The problem with the data model in <a class="ref" href="following-users.html#fig-naive_user_has_many_following">Figure 11.6</a> is that it is terribly redundant: each row contains not only each followed user’s id, but all their other information as well—all of which are <em>already</em> in the <code>users</code> table. Even worse, to model user <em>followers</em> we would need a separate, similarly redundant <code>followers</code> table. Finally, this data model is a maintainability nightmare: each time a user changed (say) his name, we would need to update not just the user’s record in the <code>users</code> table but also <em>every row containing that user</em>
in both the <code>followed_users</code> and <code>followers</code> tables.</p>
<p>The problem here is that we are missing an underlying abstraction. One way to find the proper abstraction is to consider how we might implement the act of <em>following</em> in a web application. Recall from <a class="ref" href="sign-up.html#sec-a_users_resource">Section 7.1.2</a> that the REST architecture involves <em>resources</em> that are created and destroyed. This leads us to ask two questions: When a user follows another user, what is being created? When a user <em>un</em>follows another user, what is being destroyed?</p>
<p>Upon reflection, we see that in these cases the application should either create or destroy a <em>relationship</em> between two users. A user then <code>has_many :relationships</code>, and has many <code>followed_users</code> (or <code>followers</code>) <em>through</em> these relationships. Indeed, <a class="ref" href="following-users.html#fig-naive_user_has_many_following">Figure 11.6</a> already contains most of the implementation: since each followed user is uniquely identified by <code>followed_id</code>, we could convert <code>followed_users</code> to a <code>relationships</code> table, omit the user details, and use <code>followed_id</code> to retrieve the followed user from the <code>users</code> table. Moreover, by considering <em>reverse</em> relationships, we could use the <code>follower_id</code> column to extract an array of user’s followers.</p>
<p>To make a <code>followed_users</code> array of users, it would be possible to pull out an array of <code>followed_id</code> attributes and then find the user for each one. As you might expect, though, Rails has a way to make this procedure more convenient, and the relevant technique is known as <code>has_many through</code>. As we will see in <a class="ref" href="following-users.html#sec-following">Section 11.1.4</a>, Rails allows us to say that a user is following many users <em>through</em> the relationships table, using the succinct code</p>
<div class="code"><div class="highlight"><pre><span class="n">has_many</span> <span class="ss">:followed_users</span><span class="p">,</span> <span class="ss">through:</span> <span class="ss">:relationships</span><span class="p">,</span> <span class="ss">source:</span> <span class="ss">:followed</span>
</pre></div>
</div>
<p>This code automatically populates <code>user.followed_users</code> with an array of followed users. A diagram of the data model appears in <a class="ref" href="following-users.html#fig-user_has_many_following">Figure 11.7</a>.</p>
<div class="label" id="fig-user_has_many_following"></div>
<div class="figure"><div class="center"><span class="graphic"><img src="images/figures/user_has_many_followed_users.png" alt="user_has_many_followed_users" /></span></div><div class="caption"><span class="header">Figure 11.7: </span><span class="description">A model of followed users through user relationships.</span></div></div>
<p>To get started with the implementation, we first generate a Relationship model as follows:</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> rails generate model Relationship follower_id:integer followed_id:integer
</pre></div>
</div>
<p>Since we will be finding relationships by <code>follower_id</code> and by <code>followed_id</code>, we should add an index on each column for efficiency, as shown in <a class="ref" href="following-users.html#code-relationships_migration">Listing 11.1</a>.</p>
<div class="label" id="code-relationships_migration"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 11.1.</span> <span class="description">Adding indices for the <code>relationships</code> table. <br /> <code>db/migrate/[timestamp]_create_relationships.rb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="k">class</span> <span class="nc">CreateRelationships</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">:relationships</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">integer</span> <span class="ss">:follower_id</span>
<span class="n">t</span><span class="o">.</span><span class="n">integer</span> <span class="ss">:followed_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">:relationships</span><span class="p">,</span> <span class="ss">:follower_id</span>
<span class="n">add_index</span> <span class="ss">:relationships</span><span class="p">,</span> <span class="ss">:followed_id</span>
<span class="n">add_index</span> <span class="ss">:relationships</span><span class="p">,</span> <span class="o">[</span><span class="ss">:follower_id</span><span class="p">,</span> <span class="ss">:followed_id</span><span class="o">]</span><span class="p">,</span> <span class="ss">unique:</span> <span class="kp">true</span>
<span class="k">end</span>
<span class="k">end</span>
</pre></div>
</div></div>
<p><a class="ref" href="following-users.html#code-relationships_migration">Listing 11.1</a> also includes a <em>composite</em> index that enforces uniqueness of pairs of (<code>follower_id</code>, <code>followed_id</code>), so that a user can’t follow another user more than once:</p>
<div class="code"><div class="highlight"><pre><span class="n">add_index</span> <span class="ss">:relationships</span><span class="p">,</span> <span class="o">[</span><span class="ss">:follower_id</span><span class="p">,</span> <span class="ss">:followed_id</span><span class="o">]</span><span class="p">,</span> <span class="ss">unique:</span> <span class="kp">true</span>
</pre></div>
</div>
<p>(Compare to the email uniqueness index from <a class="ref" href="modeling-users.html#code-email_uniqueness_index">Listing 6.22</a>.) As we’ll see starting in <a class="ref" href="following-users.html#sec-following">Section 11.1.4</a>, our user interface won’t allow this to happen, but adding a unique index arranges to raise an error if a user tries to create duplicate relationships anyway (using, e.g., a command-line tool such as <tt>curl</tt>). We could also add a uniqueness validation to the Relationship model, but because it is <em>always</em> an error to create duplicate relationships, the unique index is sufficient for our purposes.</p>
<p>To create the <code>relationships</code> table, we migrate the database and prepare the test database as usual:</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 the Relationship data model shown in <a class="ref" href="following-users.html#fig-relationship_model">Figure 11.8</a>.</p>
<div class="label" id="fig-relationship_model"></div>
<div class="figure"><div class="center"><span class="graphic"><img src="images/figures/relationship_model.png" alt="relationship_model" /></span></div><div class="caption"><span class="header">Figure 11.8: </span><span class="description">The Relationship data model.</span></div></div>
<div class="label" id="sec-relationship_user_associations"></div>
<h3><a id="sec-11_1_2" href="following-users.html#sec-relationship_user_associations" class="heading"><span class="number">11.1.2</span> User/relationship associations</a></h3>
<p>Before implementing followed users and followers, we first need to establish the association between users and relationships. A user <code>has_many</code> relationships, and—since relationships involve <em>two</em> users—a relationship <code>belongs_to</code> both a follower and a followed user.</p>
<p>As with microposts in <a class="ref" href="user-microposts.html#sec-user_micropost_associations">Section 10.1.3</a>, we will create new relationships using the user association, with code such as</p>
<div class="code"><div class="highlight"><pre><span class="n">user</span><span class="o">.</span><span class="n">relationships</span><span class="o">.</span><span class="n">build</span><span class="p">(</span><span class="ss">followed_id:</span> <span class="o">.</span><span class="n">.</span><span class="o">.</span><span class="p">)</span>
</pre></div>
</div>
<p>We start with some tests, shown in <a class="ref" href="following-users.html#code-relationship_create_test">Listing 11.2</a>, which make a <code>relationship</code> variable, checks that it is valid, and ensures that the <code>follower_id</code> isn’t accessible. (If the test for accessible attributes doesn’t fail, be sure that your <code>application.rb</code> has been updated in accordance with <a class="ref" href="user-microposts.html#code-application_whitelist">Listing 10.6</a>.)</p>
<div class="label" id="code-relationship_create_test"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 11.2.</span> <span class="description">Testing Relationship creation and attributes. <br /> <code>spec/models/relationship_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">Relationship</span> <span class="k">do</span>
<span class="n">let</span><span class="p">(</span><span class="ss">:follower</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">:followed</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">:relationship</span><span class="p">)</span> <span class="p">{</span> <span class="n">follower</span><span class="o">.</span><span class="n">relationships</span><span class="o">.</span><span class="n">build</span><span class="p">(</span><span class="ss">followed_id:</span> <span class="n">followed</span><span class="o">.</span><span class="n">id</span><span class="p">)</span> <span class="p">}</span>
<span class="n">subject</span> <span class="p">{</span> <span class="n">relationship</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 follower_id"</span> <span class="k">do</span>
<span class="n">expect</span> <span class="k">do</span>
<span class="no">Relationship</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="ss">follower_id:</span> <span class="n">follower</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>Note that, unlike the tests for the User and Micropost models, which use <code>@user</code> and <code>@micropost</code>, respectively, <a class="ref" href="following-users.html#code-relationship_create_test">Listing 11.2</a> uses <code>let</code> in preference to an instance variable. The differences rarely matter,<sup class="footnote" id="fnref-11_4"><a href="#fn-11_4">4</a></sup> but I consider <code>let</code> to be cleaner than using an instance variable. We originally used instance variables both because instance variables are important to introduce early and because <code>let</code> is a little more advanced.</p>
<p>We should also test the User model for a <code>relationships</code> attribute, as shown in <a class="ref" href="following-users.html#code-user_relationships_method_test">Listing 11.3</a>.</p>
<div class="label" id="code-user_relationships_method_test"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 11.3.</span> <span class="description">Testing for the <code>user.relationships</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="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">:feed</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">:relationships</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>At this point, you might expect application code as in <a class="ref" href="user-microposts.html#sec-user_micropost_associations">Section 10.1.3</a>, and it’s similar, but there is one critical difference: in the case of the Micropost model, we could say</p>
<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">belongs_to</span> <span class="ss">:user</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="k">end</span>
</pre></div>
</div>
<p>and</p>
<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">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>
<p>because the <code>microposts</code> table has a <code>user_id</code> attribute to identify the user (<a class="ref" href="user-microposts.html#sec-the_basic_model">Section 10.1.1</a>). An id used in this manner to connect two database tables is known as a <em>foreign key</em>, and when the foreign key for a User model object is <code>user_id</code>, Rails infers the association automatically: by default, Rails expects a foreign key of the form <code><class>_id</code>, where <code><class></code> is the lower-case version of the class name.<sup class="footnote" id="fnref-11_5"><a href="#fn-11_5">5</a></sup> In the present case, although we are still dealing with users, they are now identified with the foreign key <code>follower_id</code>, so we have to tell that to Rails, as shown in <a class="ref" href="following-users.html#code-user_relationships_association">Listing 11.4</a>.<sup class="footnote" id="fnref-11_6"><a href="#fn-11_6">6</a></sup></p>
<div class="label" id="code-user_relationships_association"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 11.4.</span> <span class="description">Implementing the user/relationships <code>has_many</code> association. <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="o">.</span>
<span class="o">.</span>
<span class="o">.</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="n">has_many</span> <span class="ss">:relationships</span><span class="p">,</span> <span class="ss">foreign_key:</span> <span class="s2">"follower_id"</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>(Since destroying a user should also destroy that user’s relationships, we’ve gone ahead and added <code>dependent: :destroy</code> to the association; writing a test for this is left as an exercise (<a class="ref" href="following-users.html#sec-following_exercises">Section 11.5</a>).)</p>
<p>As with the Micropost model, the Relationship model has a <code>belongs_to</code> relationship with users; in this case, a relationship object belongs to both a <code>follower</code> and a <code>followed</code> user, which we test for in <a class="ref" href="following-users.html#code-relationships_belongs_to_test">Listing 11.5</a>.</p>
<div class="label" id="code-relationships_belongs_to_test"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 11.5.</span> <span class="description">Testing the user/relationships <code>belongs_to</code> association. <br /> <code>spec/models/relationship_spec.rb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="n">describe</span> <span class="no">Relationship</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">"follower methods"</span> <span class="k">do</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">:follower</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">:followed</span><span class="p">)</span> <span class="p">}</span>
<span class="n">its</span><span class="p">(</span><span class="ss">:follower</span><span class="p">)</span> <span class="p">{</span> <span class="n">should</span> <span class="o">==</span> <span class="n">follower</span> <span class="p">}</span>
<span class="n">its</span><span class="p">(</span><span class="ss">:followed</span><span class="p">)</span> <span class="p">{</span> <span class="n">should</span> <span class="o">==</span> <span class="n">followed</span> <span class="p">}</span>
<span class="k">end</span>
<span class="k">end</span>
</pre></div>
</div></div>
<p>To write the application code, we define the <code>belongs_to</code> relationship as usual. Rails infers the names of the foreign keys from the corresponding symbols (i.e., <code>follower_id</code> from <code>:follower</code>, and <code>followed_id</code> from <code>:followed</code>), but since there is neither a Followed nor a Follower model we need to supply the class name <code>User</code>. The result is shown in <a class="ref" href="following-users.html#code-relationship_belongs_to">Listing 11.6</a>. Note that, unlike the default generated Relationship model, in this case only the <code>followed_id</code> is accessible.</p>
<div class="label" id="code-relationship_belongs_to"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 11.6.</span> <span class="description">Adding the <code>belongs_to</code> associations to the Relationship model. <br /> <code>app/models/relationship.rb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Relationship</span> <span class="o"><</span> <span class="ss">ActiveRecord::Base</span>
<span class="n">attr_accessible</span> <span class="ss">:followed_id</span>
<span class="n">belongs_to</span> <span class="ss">:follower</span><span class="p">,</span> <span class="ss">class_name:</span> <span class="s2">"User"</span>
<span class="n">belongs_to</span> <span class="ss">:followed</span><span class="p">,</span> <span class="ss">class_name:</span> <span class="s2">"User"</span>
<span class="k">end</span>
</pre></div>
</div></div>
<p>The <code>followed</code> association isn’t actually needed until <a class="ref" href="following-users.html#sec-followers">Section 11.1.5</a>, but the parallel follower/followed structure is clearer if we implement them both at the same time.</p>
<p>At this point, the tests in <a class="ref" href="following-users.html#code-relationship_create_test">Listing 11.2</a> and <a class="ref" href="following-users.html#code-user_relationships_method_test">Listing 11.3</a> should pass.</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-relationship_validations"></div>
<h3><a id="sec-11_1_3" href="following-users.html#sec-relationship_validations" class="heading"><span class="number">11.1.3</span> Validations</a></h3>
<p>Before moving on, we’ll add a couple of Relationship model validations for completeness. The tests (<a class="ref" href="following-users.html#code-relationship_validation_tests">Listing 11.7</a>) and application code (<a class="ref" href="following-users.html#code-relationship_validations">Listing 11.8</a>) are straightforward.</p>
<div class="label" id="code-relationship_validation_tests"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 11.7.</span> <span class="description">Testing the Relationship model validations. <br /> <code>spec/models/relationship_spec.rb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="n">describe</span> <span class="no">Relationship</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">"when followed id is not present"</span> <span class="k">do</span>
<span class="n">before</span> <span class="p">{</span> <span class="n">relationship</span><span class="o">.</span><span class="n">followed_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">"when follower id is not present"</span> <span class="k">do</span>
<span class="n">before</span> <span class="p">{</span> <span class="n">relationship</span><span class="o">.</span><span class="n">follower_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>
<div class="label" id="code-relationship_validations"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 11.8.</span> <span class="description">Adding the Relationship model validations. <br /> <code>app/models/relationship.rb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Relationship</span> <span class="o"><</span> <span class="ss">ActiveRecord::Base</span>
<span class="n">attr_accessible</span> <span class="ss">:followed_id</span>
<span class="n">belongs_to</span> <span class="ss">:follower</span><span class="p">,</span> <span class="ss">class_name:</span> <span class="s2">"User"</span>
<span class="n">belongs_to</span> <span class="ss">:followed</span><span class="p">,</span> <span class="ss">class_name:</span> <span class="s2">"User"</span>
<span class="n">validates</span> <span class="ss">:follower_id</span><span class="p">,</span> <span class="ss">presence:</span> <span class="kp">true</span>
<span class="n">validates</span> <span class="ss">:followed_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="sec-following"></div>
<h3><a id="sec-11_1_4" href="following-users.html#sec-following" class="heading"><span class="number">11.1.4</span> Followed users</a></h3>
<p>We come now to the heart of the Relationship associations: <code>followed_users</code> and <code>followers</code>. We start with <code>followed_users</code>, as shown in <a class="ref" href="following-users.html#code-user_following_test">Listing 11.9</a>.</p>
<div class="label" id="code-user_following_test"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 11.9.</span> <span class="description">A test for the <code>user.followed_users</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="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">:relationships</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">:followed_users</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>The implementation uses <code>has_many through</code> for the first time: a user has many following <em>through</em> relationships, as illustrated in <a class="ref" href="following-users.html#fig-user_has_many_following">Figure 11.7</a>. By default, in a <code>has_many through</code> association Rails looks for a foreign key corresponding to the singular version of the association; in other words, code like</p>
<div class="code"><div class="highlight"><pre><span class="n">has_many</span> <span class="ss">:followeds</span><span class="p">,</span> <span class="ss">through:</span> <span class="ss">:relationships</span>
</pre></div>
</div>
<p>would assemble an array using the <code>followed_id</code> in the <code>relationships</code> table. But, as noted in <a class="ref" href="following-users.html#sec-a_problem_with_the_data_model">Section 11.1.1</a>, <code>user.followeds</code> is rather awkward; far more natural is to use “followed users” as a plural of “followed”, and write instead <code>user.followed_users</code> for the array of followed users. Naturally, Rails allows us to override the default, in this case using the <code>:source</code> parameter (<a class="ref" href="following-users.html#code-has_many_following_through_relationships">Listing 11.10</a>), which explicitly tells Rails that the source of the <code>followed_users</code> array is the set of <code>followed</code> ids.</p>
<div class="label" id="code-has_many_following_through_relationships"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 11.10.</span> <span class="description">Adding the User model <code>followed_users</code> association.<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="o">.</span>
<span class="o">.</span>
<span class="o">.</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="n">has_many</span> <span class="ss">:relationships</span><span class="p">,</span> <span class="ss">foreign_key:</span> <span class="s2">"follower_id"</span><span class="p">,</span> <span class="ss">dependent:</span> <span class="ss">:destroy</span>
<span class="n">has_many</span> <span class="ss">:followed_users</span><span class="p">,</span> <span class="ss">through:</span> <span class="ss">:relationships</span><span class="p">,</span> <span class="ss">source:</span> <span class="ss">:followed</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="k">end</span>
</pre></div>
</div></div>
<p>To create a following relationship, we’ll introduce a <code>follow!</code> utility method so that we can write <code>user.follow!(other_user)</code>. (This <code>follow!</code> method should always work, so, as with <code>create!</code> and <code>save!</code>, we indicate with an exclamation point that an exception will be raised on failure.) We’ll also add an associated <code>following?</code> boolean method to test if one user is following another.<sup class="footnote" id="fnref-11_7"><a href="#fn-11_7">7</a></sup> The tests in <a class="ref" href="following-users.html#code-utility_method_tests">Listing 11.11</a> show how we expect these methods to be used in practice.</p>
<div class="label" id="code-utility_method_tests"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 11.11.</span> <span class="description">Tests for some “following” utility methods. <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">it</span> <span class="p">{</span> <span class="n">should</span> <span class="n">respond_to</span><span class="p">(</span><span class="ss">:followed_users</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">:following?</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">:follow!</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">"following"</span> <span class="k">do</span>
<span class="n">let</span><span class="p">(</span><span class="ss">:other_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="vi">@user</span><span class="o">.</span><span class="n">save</span>
<span class="vi">@user</span><span class="o">.</span><span class="n">follow!</span><span class="p">(</span><span class="n">other_user</span><span class="p">)</span>
<span class="k">end</span>
<span class="n">it</span> <span class="p">{</span> <span class="n">should</span> <span class="n">be_following</span><span class="p">(</span><span class="n">other_user</span><span class="p">)</span> <span class="p">}</span>
<span class="n">its</span><span class="p">(</span><span class="ss">:followed_users</span><span class="p">)</span> <span class="p">{</span> <span class="n">should</span> <span class="kp">include</span><span class="p">(</span><span class="n">other_user</span><span class="p">)</span> <span class="p">}</span>
<span class="k">end</span>
<span class="k">end</span>
</pre></div>
</div></div>
<p>In the application code, the <code>following?</code> method takes in a user, called <code>other_user</code>, and checks to see if a followed user with that id exists in the database; the <code>follow!</code> method calls <code>create!</code> through the <code>relationships</code> association to create the following relationship. The results appear in <a class="ref" href="following-users.html#code-following_p_follow_bang">Listing 11.12</a>.</p>
<div class="label" id="code-following_p_follow_bang"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 11.12.</span> <span class="description">The <code>following?</code> and <code>follow!</code> utility methods. <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="o">.</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="k">def</span> <span class="nf">feed</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="k">end</span>
<span class="k">def</span> <span class="nf">following?</span><span class="p">(</span><span class="n">other_user</span><span class="p">)</span>
<span class="n">relationships</span><span class="o">.</span><span class="n">find_by_followed_id</span><span class="p">(</span><span class="n">other_user</span><span class="o">.</span><span class="n">id</span><span class="p">)</span>
<span class="k">end</span>
<span class="k">def</span> <span class="nf">follow!</span><span class="p">(</span><span class="n">other_user</span><span class="p">)</span>
<span class="n">relationships</span><span class="o">.</span><span class="n">create!</span><span class="p">(</span><span class="ss">followed_id:</span> <span class="n">other_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="o">.</span>
<span class="o">.</span>
<span class="k">end</span>
</pre></div>
</div></div>
<p>Note that in <a class="ref" href="following-users.html#code-following_p_follow_bang">Listing 11.12</a> we have omitted the user itself, writing just</p>
<div class="code"><div class="highlight"><pre><span class="n">relationships</span><span class="o">.</span><span class="n">create!</span><span class="p">(</span><span class="o">.</span><span class="n">.</span><span class="o">.</span><span class="p">)</span>
</pre></div>
</div>
<p>instead of the equivalent code</p>
<div class="code"><div class="highlight"><pre><span class="nb">self</span><span class="o">.</span><span class="n">relationships</span><span class="o">.</span><span class="n">create!</span><span class="p">(</span><span class="o">.</span><span class="n">.</span><span class="o">.</span><span class="p">)</span>
</pre></div>
</div>
<p>Whether to include the explicit <code>self</code> is largely a matter of taste.</p>
<p>Of course, users should be able to unfollow other users as well as follow them, which leads to the somewhat predictable <code>unfollow!</code> method, as shown in <a class="ref" href="following-users.html#code-user_unfollow_test">Listing 11.13</a>.<sup class="footnote" id="fnref-11_8"><a href="#fn-11_8">8</a></sup></p>
<div class="label" id="code-user_unfollow_test"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 11.13.</span> <span class="description">A test for unfollowing a user. <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">it</span> <span class="p">{</span> <span class="n">should</span> <span class="n">respond_to</span><span class="p">(</span><span class="ss">:follow!</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">:unfollow!</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">"following"</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">"and unfollowing"</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">unfollow!</span><span class="p">(</span><span class="n">other_user</span><span class="p">)</span> <span class="p">}</span>
<span class="n">it</span> <span class="p">{</span> <span class="n">should_not</span> <span class="n">be_following</span><span class="p">(</span><span class="n">other_user</span><span class="p">)</span> <span class="p">}</span>
<span class="n">its</span><span class="p">(</span><span class="ss">:followed_users</span><span class="p">)</span> <span class="p">{</span> <span class="n">should_not</span> <span class="kp">include</span><span class="p">(</span><span class="n">other_user</span><span class="p">)</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>The code for <code>unfollow!</code> is straightforward: just find the relationship by followed id and destroy it (<a class="ref" href="following-users.html#code-user_unfollow">Listing 11.14</a>).</p>
<div class="label" id="code-user_unfollow"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 11.14.</span> <span class="description">Unfollowing a user by destroying a user relationship. <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="o">.</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="k">def</span> <span class="nf">following?</span><span class="p">(</span><span class="n">other_user</span><span class="p">)</span>
<span class="n">relationships</span><span class="o">.</span><span class="n">find_by_followed_id</span><span class="p">(</span><span class="n">other_user</span><span class="o">.</span><span class="n">id</span><span class="p">)</span>
<span class="k">end</span>
<span class="k">def</span> <span class="nf">follow!</span><span class="p">(</span><span class="n">other_user</span><span class="p">)</span>
<span class="n">relationships</span><span class="o">.</span><span class="n">create!</span><span class="p">(</span><span class="ss">followed_id:</span> <span class="n">other_user</span><span class="o">.</span><span class="n">id</span><span class="p">)</span>
<span class="k">end</span>
<span class="k">def</span> <span class="nf">unfollow!</span><span class="p">(</span><span class="n">other_user</span><span class="p">)</span>
<span class="n">relationships</span><span class="o">.</span><span class="n">find_by_followed_id</span><span class="p">(</span><span class="n">other_user</span><span class="o">.</span><span class="n">id</span><span class="p">)</span><span class="o">.</span><span class="n">destroy</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>
<div class="label" id="sec-followers"></div>
<h3><a id="sec-11_1_5" href="following-users.html#sec-followers" class="heading"><span class="number">11.1.5</span> Followers</a></h3>
<p>The final piece of the relationships puzzle is to add a <code>user.followers</code> method to go with <code>user.followed_users</code>. You may have noticed from <a class="ref" href="following-users.html#fig-user_has_many_following">Figure 11.7</a> that all the information needed to extract an array of followers is already present in the <code>relationships</code> table. Indeed, the technique is exactly the same as for user following, with the roles of <code>follower_id</code> and <code>followed_id</code> reversed. This suggests that, if we could somehow arrange for a <code>reverse_relationships</code> table with those two columns reversed (<a class="ref" href="following-users.html#fig-user_has_many_followers">Figure 11.9</a>), we could implement <code>user.followers</code> with little effort.</p>
<div class="label" id="fig-user_has_many_followers"></div>
<div class="figure"><div class="center"><span class="graphic"><img src="images/figures/user_has_many_followers_2nd_ed.png" alt="user_has_many_followers_2nd_ed" /></span></div><div class="caption"><span class="header">Figure 11.9: </span><span class="description">A model for user followers using a reverse Relationship model.</span></div></div>
<p>We begin with the tests, having faith that the magic of Rails will come to the rescue (<a class="ref" href="following-users.html#code-reverse_relationships_test">Listing 11.15</a>).</p>
<div class="label" id="code-reverse_relationships_test"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 11.15.</span> <span class="description">Testing for reverse relationships. <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">it</span> <span class="p">{</span> <span class="n">should</span> <span class="n">respond_to</span><span class="p">(</span><span class="ss">:relationships</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">:followed_users</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">:reverse_relationships</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">:followers</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">"following"</span> <span class="k">do</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">be_following</span><span class="p">(</span><span class="n">other_user</span><span class="p">)</span> <span class="p">}</span>
<span class="n">its</span><span class="p">(</span><span class="ss">:followed_users</span><span class="p">)</span> <span class="p">{</span> <span class="n">should</span> <span class="kp">include</span><span class="p">(</span><span class="n">other_user</span><span class="p">)</span> <span class="p">}</span>
<span class="n">describe</span> <span class="s2">"followed user"</span> <span class="k">do</span>
<span class="n">subject</span> <span class="p">{</span> <span class="n">other_user</span> <span class="p">}</span>
<span class="n">its</span><span class="p">(</span><span class="ss">:followers</span><span class="p">)</span> <span class="p">{</span> <span class="n">should</span> <span class="kp">include</span><span class="p">(</span><span class="vi">@user</span><span class="p">)</span> <span class="p">}</span>
<span class="k">end</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>Notice how we switch subjects using the <code>subject</code> method, replacing <code>@user</code> with <code>other_user</code>, allowing us to test the follower relationship in a natural way:</p>
<div class="code"><div class="highlight"><pre><span class="n">subject</span> <span class="p">{</span> <span class="n">other_user</span> <span class="p">}</span>
<span class="n">its</span><span class="p">(</span><span class="ss">:followers</span><span class="p">)</span> <span class="p">{</span> <span class="n">should</span> <span class="kp">include</span><span class="p">(</span><span class="vi">@user</span><span class="p">)</span> <span class="p">}</span>
</pre></div>
</div>
<p>As you probably suspect, we will not be making a whole database table just to hold reverse relationships. Instead, we will exploit the underlying symmetry between followers and followed users to simulate a <code>reverse_relationships</code> table by passing <code>followed_id</code> as the primary key. In other words, where the <code>relationships</code> association uses the <code>follower_id</code> foreign key,</p>
<div class="code"><div class="highlight"><pre><span class="n">has_many</span> <span class="ss">:relationships</span><span class="p">,</span> <span class="ss">foreign_key:</span> <span class="s2">"follower_id"</span>
</pre></div>
</div>
<p>the <code>reverse_relationships</code> association uses <code>followed_id</code>:</p>
<div class="code"><div class="highlight"><pre><span class="n">has_many</span> <span class="ss">:reverse_relationships</span><span class="p">,</span> <span class="ss">foreign_key:</span> <span class="s2">"followed_id"</span>
</pre></div>
</div>
<p>The <code>followers</code> association then gets built through the reverse relationships, as shown in <a class="ref" href="following-users.html#code-user_reverse_relationships">Listing 11.16</a>.</p>
<div class="label" id="code-user_reverse_relationships"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 11.16.</span> <span class="description">Implementing <code>user.followers</code> using reverse relationships. <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="o">.</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="n">has_many</span> <span class="ss">:reverse_relationships</span><span class="p">,</span> <span class="ss">foreign_key:</span> <span class="s2">"followed_id"</span><span class="p">,</span>
<span class="ss">class_name:</span> <span class="s2">"Relationship"</span><span class="p">,</span>
<span class="ss">dependent:</span> <span class="ss">:destroy</span>
<span class="n">has_many</span> <span class="ss">:followers</span><span class="p">,</span> <span class="ss">through:</span> <span class="ss">:reverse_relationships</span><span class="p">,</span> <span class="ss">source:</span> <span class="ss">:follower</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="k">end</span>
</pre></div>
</div></div>
<p>(As with <a class="ref" href="following-users.html#code-user_relationships_association">Listing 11.4</a>, the test for <code>dependent :destroy</code> is left as an exercise (<a class="ref" href="following-users.html#sec-following_exercises">Section 11.5</a>).) Note that we actually have to include the <em>class</em> name for this association, i.e.,</p>
<div class="code"><div class="highlight"><pre><span class="n">has_many</span> <span class="ss">:reverse_relationships</span><span class="p">,</span> <span class="ss">foreign_key:</span> <span class="s2">"followed_id"</span><span class="p">,</span>
<span class="ss">class_name:</span> <span class="s2">"Relationship"</span>
</pre></div>
</div>
<p>because otherwise Rails would look for a <code>ReverseRelationship</code> class, which doesn’t exist.</p>
<p>It’s also worth noting that we could actually omit the <code>:source</code> key in this case, using simply</p>
<div class="code"><div class="highlight"><pre><span class="n">has_many</span> <span class="ss">:followers</span><span class="p">,</span> <span class="ss">through:</span> <span class="ss">:reverse_relationships</span>
</pre></div>
</div>
<p>since, in the case of a <code>:followers</code> attribute, Rails will singularize “followers” and automatically look for the foreign key <code>follower_id</code> in this case. I’ve kept the <code>:source</code> key to emphasize the parallel structure with the <code>has_many :followed_users</code> association, but you are free to leave it off.</p>
<p>With the code in <a class="ref" href="following-users.html#code-user_reverse_relationships">Listing 11.16</a>, the following/follower associations are complete, and all the tests should pass:</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> bundle <span class="nb">exec </span>rspec spec/
</pre></div>
</div>
<p>This section has placed rather heavy demands on your data modeling skills, and it’s fine if it takes a while to soak in. In fact, one of the best ways to understand the associations is to use them in the web interface, as seen in the next section.</p>
<div class="label" id="sec-a_web_interface_for_following_and_followers"></div>
<h2><a id="sec-11_2" href="following-users.html#sec-a_web_interface_for_following_and_followers" class="heading"><span class="number">11.2</span> A web interface for following users</a></h2>
<p>In the introduction to this chapter, we saw a preview of the page flow for user following. In this section, we will implement the basic interface and following/unfollowing functionality shown in those mockups. We will also make separate pages to show the user following and followers arrays. In <a class="ref" href="following-users.html#sec-the_status_feed">Section 11.3</a>, we’ll complete our sample application by adding the user’s status feed.</p>
<div class="label" id="sec-sample_following_data"></div>
<h3><a id="sec-11_2_1" href="following-users.html#sec-sample_following_data" class="heading"><span class="number">11.2.1</span> Sample following data</a></h3>
<p>As in previous chapters, we will find it convenient to use the sample data Rake task to fill the database with sample relationships. This will allow us to design the look and feel of the web pages first, deferring the back-end functionality until later in this section.</p>
<p>When we last left the sample data populator in <a class="ref" href="user-microposts.html#code-sample_microposts">Listing 10.23</a>, it was getting rather cluttered, so we begin by defining separate methods to make users and microposts, and then add sample relationship data using a new <code>make_relationships</code> method. The results are shown in <a class="ref" href="following-users.html#code-sample_relationships">Listing 11.17</a>.</p>
<div class="label" id="code-sample_relationships"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 11.17.</span> <span class="description">Adding following/follower relationships to the sample data. <br /> <code>lib/tasks/sample_data.rake</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="n">namespace</span> <span class="ss">:db</span> <span class="k">do</span>
<span class="n">desc</span> <span class="s2">"Fill database with sample data"</span>
<span class="n">task</span> <span class="ss">populate:</span> <span class="ss">:environment</span> <span class="k">do</span>
<span class="n">make_users</span>
<span class="n">make_microposts</span>
<span class="n">make_relationships</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">def</span> <span class="nf">make_users</span>
<span class="n">admin</span> <span class="o">=</span> <span class="no">User</span><span class="o">.</span><span class="n">create!</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">"[email protected]"</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="n">admin</span><span class="o">.</span><span class="n">toggle!</span><span class="p">(</span><span class="ss">:admin</span><span class="p">)</span>
<span class="mi">99</span><span class="o">.</span><span class="n">times</span> <span class="k">do</span> <span class="o">|</span><span class="n">n</span><span class="o">|</span>
<span class="nb">name</span> <span class="o">=</span> <span class="ss">Faker::Name</span><span class="o">.</span><span class="n">name</span>
<span class="n">email</span> <span class="o">=</span> <span class="s2">"example-</span><span class="si">#{</span><span class="n">n</span><span class="o">+</span><span class="mi">1</span><span class="si">}</span><span class="s2">@railstutorial.org"</span>
<span class="n">password</span> <span class="o">=</span> <span class="s2">"password"</span>
<span class="no">User</span><span class="o">.</span><span class="n">create!</span><span class="p">(</span><span class="nb">name</span><span class="p">:</span> <span class="nb">name</span><span class="p">,</span>
<span class="ss">email:</span> <span class="n">email</span><span class="p">,</span>
<span class="ss">password:</span> <span class="n">password</span><span class="p">,</span>
<span class="ss">password_confirmation:</span> <span class="n">password</span><span class="p">)</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">def</span> <span class="nf">make_microposts</span>
<span class="n">users</span> <span class="o">=</span> <span class="no">User</span><span class="o">.</span><span class="n">all</span><span class="p">(</span><span class="ss">limit:</span> <span class="mi">6</span><span class="p">)</span>
<span class="mi">50</span><span class="o">.</span><span class="n">times</span> <span class="k">do</span>
<span class="n">content</span> <span class="o">=</span> <span class="ss">Faker::Lorem</span><span class="o">.</span><span class="n">sentence</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
<span class="n">users</span><span class="o">.</span><span class="n">each</span> <span class="p">{</span> <span class="o">|</span><span class="n">user</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">create!</span><span class="p">(</span><span class="ss">content:</span> <span class="n">content</span><span class="p">)</span> <span class="p">}</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">def</span> <span class="nf">make_relationships</span>
<span class="n">users</span> <span class="o">=</span> <span class="no">User</span><span class="o">.</span><span class="n">all</span>
<span class="n">user</span> <span class="o">=</span> <span class="n">users</span><span class="o">.</span><span class="n">first</span>
<span class="n">followed_users</span> <span class="o">=</span> <span class="n">users</span><span class="o">[</span><span class="mi">2</span><span class="o">.</span><span class="n">.</span><span class="mi">50</span><span class="o">]</span>
<span class="n">followers</span> <span class="o">=</span> <span class="n">users</span><span class="o">[</span><span class="mi">3</span><span class="o">.</span><span class="n">.</span><span class="mi">40</span><span class="o">]</span>
<span class="n">followed_users</span><span class="o">.</span><span class="n">each</span> <span class="p">{</span> <span class="o">|</span><span class="n">followed</span><span class="o">|</span> <span class="n">user</span><span class="o">.</span><span class="n">follow!</span><span class="p">(</span><span class="n">followed</span><span class="p">)</span> <span class="p">}</span>
<span class="n">followers</span><span class="o">.</span><span class="n">each</span> <span class="p">{</span> <span class="o">|</span><span class="n">follower</span><span class="o">|</span> <span class="n">follower</span><span class="o">.</span><span class="n">follow!</span><span class="p">(</span><span class="n">user</span><span class="p">)</span> <span class="p">}</span>
<span class="k">end</span>
</pre></div>
</div></div>
<p>Here the sample relationships are created using the code</p>
<div class="code"><div class="highlight"><pre><span class="k">def</span> <span class="nf">make_relationships</span>
<span class="n">users</span> <span class="o">=</span> <span class="no">User</span><span class="o">.</span><span class="n">all</span>
<span class="n">user</span> <span class="o">=</span> <span class="n">users</span><span class="o">.</span><span class="n">first</span>
<span class="n">followed_users</span> <span class="o">=</span> <span class="n">users</span><span class="o">[</span><span class="mi">2</span><span class="o">.</span><span class="n">.</span><span class="mi">50</span><span class="o">]</span>
<span class="n">followers</span> <span class="o">=</span> <span class="n">users</span><span class="o">[</span><span class="mi">3</span><span class="o">.</span><span class="n">.</span><span class="mi">40</span><span class="o">]</span>
<span class="n">followed_users</span><span class="o">.</span><span class="n">each</span> <span class="p">{</span> <span class="o">|</span><span class="n">followed</span><span class="o">|</span> <span class="n">user</span><span class="o">.</span><span class="n">follow!</span><span class="p">(</span><span class="n">followed</span><span class="p">)</span> <span class="p">}</span>
<span class="n">followers</span><span class="o">.</span><span class="n">each</span> <span class="p">{</span> <span class="o">|</span><span class="n">follower</span><span class="o">|</span> <span class="n">follower</span><span class="o">.</span><span class="n">follow!</span><span class="p">(</span><span class="n">user</span><span class="p">)</span> <span class="p">}</span>
<span class="k">end</span>
</pre></div>
</div>
<p>We somewhat arbitrarily arrange for the first user to follow users 3 through 51, and then have users 4 through 41 follow that user back. The resulting relationships will be sufficient for developing the application interface.</p>
<p>To execute the code in <a class="ref" href="following-users.html#code-sample_relationships">Listing 11.17</a>, populate the database as usual:</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> bundle <span class="nb">exec </span>rake db:reset
<span class="gp">$</span> bundle <span class="nb">exec </span>rake db:populate
<span class="gp">$</span> bundle <span class="nb">exec </span>rake db:test:prepare
</pre></div>
</div>
<div class="label" id="sec-stats_and_a_follow_form"></div>
<h3><a id="sec-11_2_2" href="following-users.html#sec-stats_and_a_follow_form" class="heading"><span class="number">11.2.2</span> Stats and a follow form</a></h3>
<p>Now that our sample users have both followed user and followers arrays, we need to update the profile page and Home page to reflect this. We’ll start by making a partial to display the following and follower statistics on the profile and home pages. We’ll next add a follow/unfollow form, and then make dedicated pages for showing user followed users and followers.</p>
<p>As noted in <a class="ref" href="following-users.html#sec-a_problem_with_the_data_model">Section 11.1.1</a>, the word “following” is ambiguous as an attribute (where <code>user.following</code> could reasonably mean either the followed users or the user’s followers), it makes sense as a label, as in “50 following”. Indeed, this is the label used by Twitter itself, a usage adopted in the mockups starting in <a class="ref" href="following-users.html#fig-page_flow_profile_mockup">Figure 11.1</a> and shown in close-up in <a class="ref" href="following-users.html#fig-stats_partial_mockup">Figure 11.10</a>.</p>
<div class="label" id="fig-stats_partial_mockup"></div>
<div class="figure"><div class="center"><span class="graphic"><img src="images/figures/stats_partial_mockup.png" alt="stats_partial_mockup" /></span></div><div class="caption"><span class="header">Figure 11.10: </span><span class="description">A mockup of the stats partial.</span></div></div>
<p>The stats in <a class="ref" href="following-users.html#fig-stats_partial_mockup">Figure 11.10</a> consist of the number of users the current user is following and the number of followers, each of which should be a link to its respective dedicated display page. In <a class="ref" href="filling-in-the-layout.html#top">Chapter 5</a>, we stubbed out such links with the dummy text <code>’#’</code>, but that was before we had much experience with routes. This time, although we’ll defer the actual pages to <a class="ref" href="following-users.html#sec-following_and_followers_pages">Section 11.2.3</a>, we’ll make the routes now, as seen in <a class="ref" href="following-users.html#code-following_followers_actions_routes">Listing 11.18</a>. This code uses the <code>:member</code> method inside a <code>resources</code> <em>block</em>, which we haven’t seen before, but see if you can guess what it does. (<em>Note</em>: The code in <a class="ref" href="following-users.html#code-following_followers_actions_routes">Listing 11.18</a> should <em>replace</em> the <code>resources :users</code>.)</p>
<div class="label" id="code-following_followers_actions_routes"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 11.18.</span> <span class="description">Adding <code>following</code> and <code>followers</code> actions to the Users controller. <br /> <code>config/routes.rb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="ss">SampleApp::Application</span><span class="o">.</span><span class="n">routes</span><span class="o">.</span><span class="n">draw</span> <span class="k">do</span>
<span class="n">resources</span> <span class="ss">:users</span> <span class="k">do</span>
<span class="n">member</span> <span class="k">do</span>
<span class="n">get</span> <span class="ss">:following</span><span class="p">,</span> <span class="ss">:followers</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>You might suspect that the URIs will look like /users/1/following and /users/1/followers, and that is exactly what the code in <a class="ref" href="following-users.html#code-following_followers_actions_routes">Listing 11.18</a> does. Since both pages will be showing data, we use <code>get</code> to arrange for the URIs to respond to <tt>GET</tt> requests (as required by the REST convention for such pages), and the <code>member</code> method means that the routes respond to URIs containing the user id. The other possibility, <code>collection</code>, works without the id, so that</p>
<div class="code"><div class="highlight"><pre><span class="n">resources</span> <span class="ss">:users</span> <span class="k">do</span>
<span class="n">collection</span> <span class="k">do</span>
<span class="n">get</span> <span class="ss">:tigers</span>
<span class="k">end</span>
<span class="k">end</span>
</pre></div>
</div>
<p>would respond to the URI /users/tigers (presumably to display all the tigers in our application). For more details on such routing options, see the <a href="http://guides.rubyonrails.org/routing.html">Rails Guides article on “Rails Routing from the Outside In”</a>. A table of the routes generated by <a class="ref" href="following-users.html#code-following_followers_actions_routes">Listing 11.18</a> appears in <a class="ref" href="following-users.html#table-following_routes">Table 11.1</a>; note the named routes for the followed user and followers pages, which we’ll put to use shortly. The unfortunate hybrid usage in the “following” route is forced by our choice to use the unambiguous “followed users” terminology along with the “following” usage from Twitter. Since the former would lead to routes of the form <code>followed_users_user_path</code>, which sounds strange, we’ve opted for the latter in the context of <a class="ref" href="following-users.html#table-following_routes">Table 11.1</a>, yielding <code>following_user_path</code>.</p>
<div class="label" id="table-following_routes"></div>
<div class="table"><div class="center">
<table class="tabular"><tr><th class="align_left"><strong>HTTP request</strong></th><th class="align_left"><strong>URI</strong></th><th class="align_left"><strong>Action</strong></th><th class="align_left"><strong>Named route</strong></th></tr><tr class="top_bar"><td class="align_left"><tt>GET</tt></td><td class="align_left">/users/1/following</td><td class="align_left"><code>following</code></td><td class="align_left"><code>following_user_path(1)</code></td></tr><tr><td class="align_left"><tt>GET</tt></td><td class="align_left">/users/1/followers</td><td class="align_left"><code>followers</code></td><td class="align_left"><code>followers_user_path(1)</code></td></tr></table></div><div class="caption"><span class="header">Table 11.1: </span><span class="description">RESTful routes provided by the custom rules in resource in <a class="ref" href="following-users.html#code-following_followers_actions_routes">Listing 11.18</a>.</span></div></div>
<p>With the routes defined, we are now in a position to make tests for the stats partial. (We could have written the tests first, but the named routes would have been hard to motivate without the updated routes file.) The stats partial will appear on both the profile page and the Home page; <a class="ref" href="following-users.html#code-stats_view_test">Listing 11.19</a> opts to test it on the latter.</p>
<div class="label" id="code-stats_view_test"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 11.19.</span> <span class="description">Testing the following/follower statistics on the Home page. <br /> <code>spec/requests/static_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">"StaticPages"</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">"Home page"</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">"for signed-in users"</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="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">"Lorem"</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">"Ipsum"</span><span class="p">)</span>