Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

2538 lines (1672 sloc) 267.58 kb
<!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> &ldquo;Scaling&rdquo; 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&rsquo;ve worked my way through many Rails books, this is the one that finally made me &ldquo;get&rdquo; it. Everything is done very much &ldquo;the Rails way&rdquo;&mdash;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&rsquo;s like to do a real-world project. The tutorial&rsquo;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&rsquo;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>Formerly: Founder,</em> <a href="http://www.cdbaby.com/"><em>CD Baby</em></a> <br />
<em>Currently: Founder,</em> <a href="http://thoughts.pro/"><em>Thoughts Ltd.</em></a> <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&rsquo;d like to thank Aure both for the work he did on that book and for his support of this one. I&rsquo;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&rsquo;ll keep writing books for her.</p>
<p>I&rsquo;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&nbsp;E. Seguin, Amy Hoy, Dave Chelimsky, Pat Maddox, Tom Preston-Werner, Chris Wanstrath, Chad Fowler, Josh Susser, Obie Fernandez, Ian McFarland, Steven Bristol, 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&mdash;far too many to list&mdash;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&nbsp;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&nbsp;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 Devlopment with Rails</em>. Copyright &copy; 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 &quot;Software&quot;), 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 &quot;AS IS&quot;, 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>/*
* ----------------------------------------------------------------------------
* &quot;THE BEER-WARE LICENSE&quot; (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&rsquo;s Home page displaying a status feed of the followed users&rsquo; microposts. We will also make views to display both a user&rsquo;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&nbsp;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&nbsp;11.2</a> (including an introduction to Ajax). Finally, we&rsquo;ll end by developing a fully functional status feed in <a class="ref" href="following-users.html#sec-the_status_feed">Section&nbsp;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&nbsp;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&rsquo;ll pause for a moment and take a tour of the interface. As in previous chapters, at this early stage we&rsquo;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&nbsp;11.1</a>) and navigates to the Users page (<a class="ref" href="following-users.html#fig-page_flow_user_index_mockup">Figure&nbsp;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&nbsp;11.3</a>), clicking on the &ldquo;Follow&rdquo; button to follow that user. This changes the &ldquo;Follow&rdquo; button to &ldquo;Unfollow&rdquo;, and increments Hobbes&rsquo;s &ldquo;followers&rdquo; count by one (<a class="ref" href="following-users.html#fig-page_flow_other_profile_unfollow_button_mockup">Figure&nbsp;11.4</a>). Navigating to his home page, Calvin now sees an incremented &ldquo;following&rdquo; count and finds Hobbes&rsquo;s microposts in his status feed (<a class="ref" href="following-users.html#fig-page_flow_home_page_feed_mockup">Figure&nbsp;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&rsquo;s profile.&nbsp;<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.&nbsp;<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.&nbsp;<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.&nbsp;<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.&nbsp;<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&iuml;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&rsquo;ll learn how to fix it using <code>has_many through</code>. It&rsquo;s likely that many of the ideas in this section won&rsquo;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&rsquo;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&rsquo; default pluralization convention, the set of all users following a given user is that user&rsquo;s <em>followers</em>, and <code>user.followers</code> is an array of those users. Unfortunately, the reverse doesn&rsquo;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&rsquo;s ambiguous: in normal English, a &ldquo;following&rdquo; is the set of people following <em>you</em>, i.e., your followers&mdash;exactly the opposite of the intended meaning. Although we will use &ldquo;following&rdquo; as a label, as in &ldquo;50 following, 75 followers&rdquo;, we&rsquo;ll use &ldquo;followed users&rdquo; 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&nbsp;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&rsquo;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&iuml;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&nbsp;11.6</a> is that it is terribly redundant: each row contains not only each followed user&rsquo;s id, but all their other information as well&mdash;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&rsquo;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&nbsp;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&nbsp;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&rsquo;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&nbsp;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&nbsp;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&nbsp;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">&lt;</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&nbsp;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&rsquo;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&nbsp;6.22</a>.) As we&rsquo;ll see starting in <a class="ref" href="following-users.html#sec-following">Section&nbsp;11.1.4</a>, our user interface won&rsquo;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&nbsp;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&mdash;since relationships involve <em>two</em> users&mdash;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&nbsp;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&nbsp;11.2</a>, which make a <code>relationship</code> variable, checks that it is valid, and ensures that the <code>follower_id</code> isn&rsquo;t accessible. (If the test for accessible attributes doesn&rsquo;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&nbsp;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">&#39;spec_helper&#39;</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">&quot;accessible attributes&quot;</span> <span class="k">do</span>
<span class="n">it</span> <span class="s2">&quot;should not allow access to follower_id&quot;</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&nbsp;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&nbsp;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">&#39;spec_helper&#39;</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&nbsp;10.1.3</a>, and it&rsquo;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">&lt;</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">&lt;</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&nbsp;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>&lt;class&gt;_id</code>, where <code>&lt;class&gt;</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&nbsp;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">&lt;</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">&quot;follower_id&quot;</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&rsquo;s relationships, we&rsquo;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&nbsp;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&nbsp;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">&quot;follower methods&quot;</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&nbsp;11.6</a>. Note that, unlike the default generate 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">&lt;</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">&quot;User&quot;</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">&quot;User&quot;</span>
<span class="k">end</span>
</pre></div>
</div></div>
<p>The <code>followed</code> association isn&rsquo;t actually needed until <a class="ref" href="following-users.html#sec-followers">Section&nbsp;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&nbsp;11.2</a> and <a class="ref" href="following-users.html#code-user_relationships_method_test">Listing&nbsp;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&rsquo;ll add a couple of Relationship model validations for completeness. The tests (<a class="ref" href="following-users.html#code-relationship_validation_tests">Listing&nbsp;11.7</a>) and application code (<a class="ref" href="following-users.html#code-relationship_validations">Listing&nbsp;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">&quot;when followed id is not present&quot;</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">&quot;when follower id is not present&quot;</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">&lt;</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">&quot;User&quot;</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">&quot;User&quot;</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 <a class="ref" href="following-users.html#code-user_following_test">Listing&nbsp;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">&#39;spec_helper&#39;</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&nbsp;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&nbsp;11.1.1</a>, <code>user.followeds</code> is rather awkward; far more natural is to use &ldquo;followed users&rdquo; as a plural of &ldquo;followed&rdquo;, 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&nbsp;11.10</a>), which explicitly tells Rails that the source of the <code>followed_users</code> array is the set of <code>followed</code>&nbsp;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">&lt;</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">&quot;follower_id&quot;</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&rsquo;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&rsquo;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&nbsp;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 &ldquo;following&rdquo; 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">&#39;spec_helper&#39;</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">&quot;following&quot;</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&nbsp;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">&lt;</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&nbsp;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&nbsp;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">&#39;spec_helper&#39;</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">&quot;following&quot;</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">&quot;and unfollowing&quot;</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&nbsp;id and destroy it (<a class="ref" href="following-users.html#code-user_unfollow">Listing&nbsp;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">&lt;</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&nbsp;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&nbsp;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&nbsp;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">&#39;spec_helper&#39;</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">&quot;following&quot;</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">&quot;followed user&quot;</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">&quot;follower_id&quot;</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">&quot;followed_id&quot;</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&nbsp;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">&lt;</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">&quot;followed_id&quot;</span><span class="p">,</span>
<span class="ss">class_name:</span> <span class="s2">&quot;Relationship&quot;</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&nbsp;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&nbsp;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">&quot;followed_id&quot;</span><span class="p">,</span>
<span class="ss">class_name:</span> <span class="s2">&quot;Relationship&quot;</span>
</pre></div>
</div>
<p>because otherwise Rails would look for a <code>ReverseRelationship</code> class, which doesn&rsquo;t exist.</p>
<p>It&rsquo;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 &ldquo;followers&rdquo; and automatically look for the foreign key <code>follower_id</code> in this case. I&rsquo;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&nbsp;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&rsquo;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&nbsp;11.3</a>, we&rsquo;ll complete our sample application by adding the user&rsquo;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&nbsp;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&nbsp;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">&quot;Fill database with sample data&quot;</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">&quot;Example User&quot;</span><span class="p">,</span>
<span class="ss">email:</span> <span class="s2">&quot;example@railstutorial.org&quot;</span><span class="p">,</span>
<span class="ss">password:</span> <span class="s2">&quot;foobar&quot;</span><span class="p">,</span>
<span class="ss">password_confirmation:</span> <span class="s2">&quot;foobar&quot;</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">&quot;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&quot;</span>
<span class="n">password</span> <span class="o">=</span> <span class="s2">&quot;password&quot;</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&nbsp;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&nbsp;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&rsquo;ll start by making a partial to display the following and follower statistics on the profile and home pages. We&rsquo;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&nbsp;11.1.1</a>, the word &ldquo;following&rdquo; is ambiguous as an attribute (where <code>user.following</code> could reasonably mean either the followed users or the user&rsquo;s followers), it makes sense as a label, as in &ldquo;50 following&rdquo;. 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&nbsp;11.1</a> and shown in close-up in <a class="ref" href="following-users.html#fig-stats_partial_mockup">Figure&nbsp;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&nbsp;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&nbsp;5</a>, we stubbed out such links with the dummy text&nbsp;<code>&rsquo;#&rsquo;</code>, but that was before we had much experience with routes. This time, although we&rsquo;ll defer the actual pages to <a class="ref" href="following-users.html#sec-following_and_followers_pages">Section&nbsp;11.2.3</a>, we&rsquo;ll make the routes now, as seen in <a class="ref" href="following-users.html#code-following_followers_actions_routes">Listing&nbsp;11.18</a>. This code uses the <code>:member</code> method inside a <code>resources</code> <em>block</em>, which we haven&rsquo;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&nbsp;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&nbsp;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 &ldquo;Rails Routing from the Outside In&rdquo;</a>. A table of the routes generated by <a class="ref" href="following-users.html#code-following_followers_actions_routes">Listing&nbsp;11.18</a> appears in <a class="ref" href="following-users.html#table-following_routes">Table&nbsp;11.1</a>; note the named routes for the followed user and followers pages, which we&rsquo;ll put to use shortly. The unfortunate hybrid usage in the &ldquo;following&rdquo; route is forced by our choice to use the unambiguous &ldquo;followed users&rdquo; terminology along with the &ldquo;following&rdquo; usage from Twitter. Since the former would lead to routes of the form <code>followed_users_user_path</code>, which sounds strange, we&rsquo;ve opted for the latter in the context of <a class="ref" href="following-users.html#table-following_routes">Table&nbsp;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&nbsp;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&nbsp;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">&#39;spec_helper&#39;</span>
<span class="n">describe</span> <span class="s2">&quot;StaticPages&quot;</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">&quot;Home page&quot;</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">&quot;for signed-in users&quot;</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">&quot;Lorem&quot;</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">&quot;Ipsum&quot;</span><span class="p">)</span>
<span class="n">sign_in</span> <span class="n">user</span>
<span class="n">visit</span> <span class="n">root_path</span>
<span class="k">end</span>
<span class="n">it</span> <span class="s2">&quot;should render the user&#39;s feed&quot;</span> <span class="k">do</span>
<span class="n">user</span><span class="o">.</span><span class="n">feed</span><span class="o">.</span><span class="n">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">item</span><span class="o">|</span>
<span class="n">page</span><span class="o">.</span><span class="n">should</span> <span class="n">have_selector</span><span class="p">(</span><span class="s2">&quot;li#</span><span class="si">#{</span><span class="n">item</span><span class="o">.</span><span class="n">id</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">,</span> <span class="ss">text:</span> <span class="n">item</span><span class="o">.</span><span class="n">content</span><span class="p">)</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="n">describe</span> <span class="s2">&quot;follower/following counts&quot;</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="n">other_user</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="n">visit</span> <span class="n">root_path</span>
<span class="k">end</span>
<span class="n">it</span> <span class="p">{</span> <span class="n">should</span> <span class="n">have_link</span><span class="p">(</span><span class="s2">&quot;0 following&quot;</span><span class="p">,</span> <span class="ss">href:</span> <span class="n">following_user_path</span><span class="p">(</span><span class="n">user</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">have_link</span><span class="p">(</span><span class="s2">&quot;1 followers&quot;</span><span class="p">,</span> <span class="ss">href:</span> <span class="n">followers_user_path</span><span class="p">(</span><span class="n">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>
<span class="o">.</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="k">end</span>
</pre></div>
</div></div>
<p>The core of this test is the expectation that the following and follower counts appear on the page, together with the right URIs:</p>
<div class="code"><div class="highlight"><pre><span class="n">it</span> <span class="p">{</span> <span class="n">should</span> <span class="n">have_link</span><span class="p">(</span><span class="s2">&quot;0 following&quot;</span><span class="p">,</span> <span class="ss">href:</span> <span class="n">following_user_path</span><span class="p">(</span><span class="n">user</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">have_link</span><span class="p">(</span><span class="s2">&quot;1 followers&quot;</span><span class="p">,</span> <span class="ss">href:</span> <span class="n">followers_user_path</span><span class="p">(</span><span class="n">user</span><span class="p">))</span> <span class="p">}</span>
</pre></div>
</div>
<p>Here we have used the named routes shown in <a class="ref" href="following-users.html#table-following_routes">Table&nbsp;11.1</a> to verify that the links have the right addresses. Also note that in this case the word &ldquo;followers&rdquo; is acting as a <em>label</em>, so we keep it plural even when there is only one follower.</p>
<p>The application code for the stats partial is just a couple of links inside a div, as shown in <a class="ref" href="following-users.html#code-stats_partial">Listing&nbsp;11.20</a>.</p>
<div class="label" id="code-stats_partial"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 11.20.</span> <span class="description">A partial for displaying follower stats. <br /> <code>app/views/shared/_stats.html.erb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="cp">&lt;%</span> <span class="vi">@user</span> <span class="o">||=</span> <span class="n">current_user</span> <span class="cp">%&gt;</span>
<span class="nt">&lt;div</span> <span class="na">class=</span><span class="s">&quot;stats&quot;</span><span class="nt">&gt;</span>
<span class="nt">&lt;a</span> <span class="na">href=</span><span class="s">&quot;</span><span class="cp">&lt;%=</span> <span class="n">following_user_path</span><span class="p">(</span><span class="vi">@user</span><span class="p">)</span> <span class="cp">%&gt;</span><span class="s">&quot;</span><span class="nt">&gt;</span>
<span class="nt">&lt;strong</span> <span class="na">id=</span><span class="s">&quot;following&quot;</span> <span class="na">class=</span><span class="s">&quot;stat&quot;</span><span class="nt">&gt;</span>
<span class="cp">&lt;%=</span> <span class="vi">@user</span><span class="o">.</span><span class="n">followed_users</span><span class="o">.</span><span class="n">count</span> <span class="cp">%&gt;</span>
<span class="nt">&lt;/strong&gt;</span>
following
<span class="nt">&lt;/a&gt;</span>
<span class="nt">&lt;a</span> <span class="na">href=</span><span class="s">&quot;</span><span class="cp">&lt;%=</span> <span class="n">followers_user_path</span><span class="p">(</span><span class="vi">@user</span><span class="p">)</span> <span class="cp">%&gt;</span><span class="s">&quot;</span><span class="nt">&gt;</span>
<span class="nt">&lt;strong</span> <span class="na">id=</span><span class="s">&quot;followers&quot;</span> <span class="na">class=</span><span class="s">&quot;stat&quot;</span><span class="nt">&gt;</span>
<span class="cp">&lt;%=</span> <span class="vi">@user</span><span class="o">.</span><span class="n">followers</span><span class="o">.</span><span class="n">count</span> <span class="cp">%&gt;</span>
<span class="nt">&lt;/strong&gt;</span>
followers
<span class="nt">&lt;/a&gt;</span>
<span class="nt">&lt;/div&gt;</span>
</pre></div>
</div></div>
<p>Since we will be including the stats on both the user show pages and the home page, the first line of <a class="ref" href="following-users.html#code-stats_partial">Listing&nbsp;11.20</a> picks the right one using</p>
<div class="code"><div class="highlight"><pre><span class="cp">&lt;%</span> <span class="vi">@user</span> <span class="o">||=</span> <span class="n">current_user</span> <span class="cp">%&gt;</span>
</pre></div>
</div>
<p>As discussed in <a class="ref" href="sign-in-sign-out.html#sidebar-or_equals">Box&nbsp;8.2</a>, this does nothing when <code>@user</code> is not <code>nil</code> (as on a profile page), but when it is (as on the Home page) it sets <code>@user</code> to the current user.</p>
<p>Note also that the following/follower counts are calculated through the associations using</p>
<div class="code"><div class="highlight"><pre><span class="vi">@user</span><span class="o">.</span><span class="n">followed_users</span><span class="o">.</span><span class="n">count</span>
</pre></div>
</div>
<p>and</p>
<div class="code"><div class="highlight"><pre><span class="vi">@user</span><span class="o">.</span><span class="n">followers</span><span class="o">.</span><span class="n">count</span>
</pre></div>
</div>
<p>Compare these to the microposts count from <a class="ref" href="user-microposts.html#code-user_show_microposts">Listing&nbsp;10.20</a>, where we wrote</p>
<div class="code"><div class="highlight"><pre><span class="vi">@user</span><span class="o">.</span><span class="n">microposts</span><span class="o">.</span><span class="n">count</span>
</pre></div>
</div>
<p>to count the microposts.</p>
<p>One final detail worth noting is the presence of CSS ids on some elements, as in</p>
<div class="code"><div class="highlight"><pre><span class="nt">&lt;strong</span> <span class="na">id=</span><span class="s">&quot;following&quot;</span> <span class="na">class=</span><span class="s">&quot;stat&quot;</span><span class="nt">&gt;</span>
...
<span class="nt">&lt;/strong&gt;</span>
</pre></div>
</div>
<p>This is for the benefit of the Ajax implementation in <a class="ref" href="following-users.html#sec-a_working_follow_button_with_ajax">Section&nbsp;11.2.5</a>, which accesses elements on the page using their unique ids.</p>
<p>With the partial in hand, including the stats on the Home page is easy, as shown in <a class="ref" href="following-users.html#code-home_page_stats">Listing&nbsp;11.21</a>. (This also gets the test in <a class="ref" href="following-users.html#code-stats_view_test">Listing&nbsp;11.19</a> to pass.)</p>
<div class="label" id="code-home_page_stats"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 11.21.</span> <span class="description">Adding follower stats to the Home page. <br /> <code>app/views/static_pages/home.html.erb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="cp">&lt;%</span> <span class="k">if</span> <span class="n">signed_in?</span> <span class="cp">%&gt;</span>
.
.
.
<span class="nt">&lt;section&gt;</span>
<span class="cp">&lt;%=</span> <span class="n">render</span> <span class="s1">&#39;shared/user_info&#39;</span> <span class="cp">%&gt;</span>
<span class="nt">&lt;/section&gt;</span>
<span class="nt">&lt;section&gt;</span>
<span class="cp">&lt;%=</span> <span class="n">render</span> <span class="s1">&#39;shared/stats&#39;</span> <span class="cp">%&gt;</span>
<span class="nt">&lt;/section&gt;</span>
<span class="nt">&lt;section&gt;</span>
<span class="cp">&lt;%=</span> <span class="n">render</span> <span class="s1">&#39;shared/micropost_form&#39;</span> <span class="cp">%&gt;</span>
<span class="nt">&lt;/section&gt;</span>
.
.
.
<span class="cp">&lt;%</span> <span class="k">else</span> <span class="cp">%&gt;</span>
.
.
.
<span class="cp">&lt;%</span> <span class="k">end</span> <span class="cp">%&gt;</span>
</pre></div>
</div></div>
<p>To style the stats, we&rsquo;ll add some SCSS, as shown in <a class="ref" href="following-users.html#code-stats_css">Listing&nbsp;11.22</a> (which contains all the stylesheet code needed in this chapter). The result appears in <a class="ref" href="following-users.html#fig-home_page_follow_stats">Figure&nbsp;11.11</a>.</p>
<div class="label" id="code-stats_css"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 11.22.</span> <span class="description">SCSS for the Home page sidebar. <br /> <code>app/assets/stylesheets/custom.css.scss</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="nc">.</span>
<span class="nc">.</span>
<span class="nc">.</span>
<span class="o">/*</span> <span class="nt">sidebar</span> <span class="o">*/</span>
<span class="nc">.</span>
<span class="nc">.</span>
<span class="nc">.</span>
<span class="nc">.stats</span> <span class="p">{</span>
<span class="na">overflow</span><span class="o">:</span> <span class="no">auto</span><span class="p">;</span>
<span class="nt">a</span> <span class="p">{</span>
<span class="na">float</span><span class="o">:</span> <span class="no">left</span><span class="p">;</span>
<span class="na">padding</span><span class="o">:</span> <span class="mi">0</span> <span class="mi">10</span><span class="kt">px</span><span class="p">;</span>
<span class="na">border-left</span><span class="o">:</span> <span class="mi">1</span><span class="kt">px</span> <span class="no">solid</span> <span class="nv">$grayLighter</span><span class="p">;</span>
<span class="na">color</span><span class="o">:</span> <span class="nb">gray</span><span class="p">;</span>
<span class="k">&amp;</span><span class="nd">:first-child</span> <span class="p">{</span>
<span class="na">padding-left</span><span class="o">:</span> <span class="mi">0</span><span class="p">;</span>
<span class="na">border</span><span class="o">:</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">&amp;</span><span class="nd">:hover</span> <span class="p">{</span>
<span class="na">text-decoration</span><span class="o">:</span> <span class="no">none</span><span class="p">;</span>
<span class="na">color</span><span class="o">:</span> <span class="nv">$blue</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="nt">strong</span> <span class="p">{</span>
<span class="na">display</span><span class="o">:</span> <span class="no">block</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="nc">.user_avatars</span> <span class="p">{</span>
<span class="na">overflow</span><span class="o">:</span> <span class="no">auto</span><span class="p">;</span>
<span class="na">margin-top</span><span class="o">:</span> <span class="mi">10</span><span class="kt">px</span><span class="p">;</span>
<span class="nc">.gravatar</span> <span class="p">{</span>
<span class="na">margin</span><span class="o">:</span> <span class="mi">1</span><span class="kt">px</span> <span class="mi">1</span><span class="kt">px</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="nc">.</span>
<span class="nc">.</span>
<span class="nc">.</span>
</pre></div>
</div></div>
<div class="label" id="fig-home_page_follow_stats"></div>
<div class="figure"><div class="center"><span class="graphic"><img src="images/figures/home_page_follow_stats_bootstrap.png" alt="home_page_follow_stats_bootstrap" /></span></div><div class="caption"><span class="header">Figure 11.11: </span><span class="description">The Home page (<a href="http://localhost:3000/">/</a>) with follow stats.&nbsp;<a href="http://railstutorial.org/images/figures/home_page_follow_stats_bootstrap-full.png">(full size)</a></span></div></div>
<p>We&rsquo;ll render the stats partial on the profile page in a moment, but first let&rsquo;s make a partial for the follow/unfollow button, as shown in <a class="ref" href="following-users.html#code-follow_form_partial">Listing&nbsp;11.23</a>.</p>
<div class="label" id="code-follow_form_partial"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 11.23.</span> <span class="description">A partial for a follow/unfollow form. <br /> <code>app/views/users/_follow_form.html.erb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="cp">&lt;%</span> <span class="k">unless</span> <span class="n">current_user?</span><span class="p">(</span><span class="vi">@user</span><span class="p">)</span> <span class="cp">%&gt;</span>
<span class="nt">&lt;div</span> <span class="na">id=</span><span class="s">&quot;follow_form&quot;</span><span class="nt">&gt;</span>
<span class="cp">&lt;%</span> <span class="k">if</span> <span class="n">current_user</span><span class="o">.</span><span class="n">following?</span><span class="p">(</span><span class="vi">@user</span><span class="p">)</span> <span class="cp">%&gt;</span>
<span class="cp">&lt;%=</span> <span class="n">render</span> <span class="s1">&#39;unfollow&#39;</span> <span class="cp">%&gt;</span>
<span class="cp">&lt;%</span> <span class="k">else</span> <span class="cp">%&gt;</span>
<span class="cp">&lt;%=</span> <span class="n">render</span> <span class="s1">&#39;follow&#39;</span> <span class="cp">%&gt;</span>
<span class="cp">&lt;%</span> <span class="k">end</span> <span class="cp">%&gt;</span>
<span class="nt">&lt;/div&gt;</span>
<span class="cp">&lt;%</span> <span class="k">end</span> <span class="cp">%&gt;</span>
</pre></div>
</div></div>
<p>This does nothing but defer the real work to <code>follow</code> and <code>unfollow</code> partials, which need a new routes file with rules for the Relationships resource, which follows the Microposts resource example (<a class="ref" href="user-microposts.html#code-microposts_resource">Listing&nbsp;10.25</a>), as seen in <a class="ref" href="following-users.html#code-relationships_resource">Listing&nbsp;11.24</a>.</p>
<div class="label" id="code-relationships_resource"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 11.24.</span> <span class="description">Adding the routes for user relationships. <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="o">.</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="n">resources</span> <span class="ss">:sessions</span><span class="p">,</span> <span class="ss">only:</span> <span class="o">[</span><span class="ss">:new</span><span class="p">,</span> <span class="ss">:create</span><span class="p">,</span> <span class="ss">:destroy</span><span class="o">]</span>
<span class="n">resources</span> <span class="ss">:microposts</span><span class="p">,</span> <span class="ss">only:</span> <span class="o">[</span><span class="ss">:create</span><span class="p">,</span> <span class="ss">:destroy</span><span class="o">]</span>
<span class="n">resources</span> <span class="ss">:relationships</span><span class="p">,</span> <span class="ss">only:</span> <span class="o">[</span><span class="ss">:create</span><span class="p">,</span> <span class="ss">:destroy</span><span class="o">]</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="k">end</span>
</pre></div>
</div></div>
<p>The follow/unfollow partials themselves are shown in <a class="ref" href="following-users.html#code-follow_form">Listing&nbsp;11.25</a> and <a class="ref" href="following-users.html#code-unfollow_form">Listing&nbsp;11.26</a>.</p>
<div class="label" id="code-follow_form"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 11.25.</span> <span class="description">A form for following a user. <br /> <code>app/views/users/_follow.html.erb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="cp">&lt;%=</span> <span class="n">form_for</span><span class="p">(</span><span class="n">current_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="vi">@user</span><span class="o">.</span><span class="n">id</span><span class="p">))</span> <span class="k">do</span> <span class="o">|</span><span class="n">f</span><span class="o">|</span> <span class="cp">%&gt;</span>
<span class="nt">&lt;div&gt;</span><span class="cp">&lt;%=</span> <span class="n">f</span><span class="o">.</span><span class="n">hidden_field</span> <span class="ss">:followed_id</span> <span class="cp">%&gt;</span><span class="nt">&lt;/div&gt;</span>
<span class="cp">&lt;%=</span> <span class="n">f</span><span class="o">.</span><span class="n">submit</span> <span class="s2">&quot;Follow&quot;</span><span class="p">,</span> <span class="ss">class:</span> <span class="s2">&quot;btn btn-large btn-primary&quot;</span> <span class="cp">%&gt;</span>
<span class="cp">&lt;%</span> <span class="k">end</span> <span class="cp">%&gt;</span>
</pre></div>
</div></div>
<div class="label" id="code-unfollow_form"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 11.26.</span> <span class="description">A form for unfollowing a user. <br /> <code>app/views/users/_unfollow.html.erb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="cp">&lt;%=</span> <span class="n">form_for</span><span class="p">(</span><span class="n">current_user</span><span class="o">.</span><span class="n">relationships</span><span class="o">.</span><span class="n">find_by_followed_id</span><span class="p">(</span><span class="vi">@user</span><span class="p">),</span>
<span class="ss">html:</span> <span class="p">{</span> <span class="nb">method</span><span class="p">:</span> <span class="ss">:delete</span> <span class="p">})</span> <span class="k">do</span> <span class="o">|</span><span class="n">f</span><span class="o">|</span> <span class="cp">%&gt;</span>
<span class="cp">&lt;%=</span> <span class="n">f</span><span class="o">.</span><span class="n">submit</span> <span class="s2">&quot;Unfollow&quot;</span><span class="p">,</span> <span class="ss">class:</span> <span class="s2">&quot;btn btn-large&quot;</span> <span class="cp">%&gt;</span>
<span class="cp">&lt;%</span> <span class="k">end</span> <span class="cp">%&gt;</span>
</pre></div>
</div></div>
<p>These two forms both use <code>form_for</code> to manipulate a Relationship model object; the main difference between the two is that <a class="ref" href="following-users.html#code-follow_form">Listing&nbsp;11.25</a> builds a <em>new</em> relationship, whereas <a class="ref" href="following-users.html#code-unfollow_form">Listing&nbsp;11.26</a> finds the existing relationship. Naturally, the former sends a <tt>POST</tt> request to the Relationships controller to <code>create</code> a relationship, while the latter sends a <tt>DELETE</tt> request to <code>destroy</code> a relationship. (We&rsquo;ll write these actions in <a class="ref" href="following-users.html#sec-a_working_follow_button_the_standard_way">Section&nbsp;11.2.4</a>.) Finally, you&rsquo;ll note that the follow/unfollow form doesn&rsquo;t have any content other than the button, but it still needs to send the <code>followed_id</code>, which we accomplish with the <code>hidden_field</code> method, which produces HTML of the form</p>
<div class="code"><div class="highlight"><pre><span class="nt">&lt;input</span> <span class="na">id=</span><span class="s">&quot;followed_relationship_followed_id&quot;</span>
<span class="na">name=</span><span class="s">&quot;followed_relationship[followed_id]&quot;</span>
<span class="na">type=</span><span class="s">&quot;hidden&quot;</span> <span class="na">value=</span><span class="s">&quot;3&quot;</span> <span class="nt">/&gt;</span>
</pre></div>
</div>
<p>The &ldquo;hidden&rdquo; <code>input</code> tag puts the relevant information on the page without displaying it in the browser.</p>
<p>We can now include the follow form and the following statistics on the user profile page simply by rendering the partials, as shown in <a class="ref" href="following-users.html#code-user_follow_form_profile_stats">Listing&nbsp;11.27</a>. Profiles with follow and unfollow buttons, respectively, appear in <a class="ref" href="following-users.html#fig-profile_follow_button">Figure&nbsp;11.12</a> and <a class="ref" href="following-users.html#fig-profile_unfollow_button">Figure&nbsp;11.13</a>.</p>
<div class="label" id="code-user_follow_form_profile_stats"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 11.27.</span> <span class="description">Adding the follow form and follower stats to the user profile page. <br /> <code>app/views/users/show.html.erb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="cp">&lt;%</span> <span class="n">provide</span><span class="p">(</span><span class="ss">:title</span><span class="p">,</span> <span class="vi">@user</span><span class="o">.</span><span class="n">name</span><span class="p">)</span> <span class="cp">%&gt;</span>
<span class="nt">&lt;div</span> <span class="na">class=</span><span class="s">&quot;row&quot;</span><span class="nt">&gt;</span>
<span class="nt">&lt;aside</span> <span class="na">class=</span><span class="s">&quot;span4&quot;</span><span class="nt">&gt;</span>
<span class="nt">&lt;section&gt;</span>
<span class="nt">&lt;h1&gt;</span>
<span class="cp">&lt;%=</span> <span class="n">gravatar_for</span> <span class="vi">@user</span> <span class="cp">%&gt;</span>
<span class="cp">&lt;%=</span> <span class="vi">@user</span><span class="o">.</span><span class="n">name</span> <span class="cp">%&gt;</span>
<span class="nt">&lt;/h1&gt;</span>
<span class="nt">&lt;/section&gt;</span>
<span class="nt">&lt;section&gt;</span>
<span class="cp">&lt;%=</span> <span class="n">render</span> <span class="s1">&#39;shared/stats&#39;</span> <span class="cp">%&gt;</span>
<span class="nt">&lt;/section&gt;</span>
<span class="nt">&lt;/aside&gt;</span>
<span class="nt">&lt;div</span> <span class="na">class=</span><span class="s">&quot;span8&quot;</span><span class="nt">&gt;</span>
<span class="cp">&lt;%=</span> <span class="n">render</span> <span class="s1">&#39;follow_form&#39;</span> <span class="k">if</span> <span class="n">signed_in?</span> <span class="cp">%&gt;</span>
.
.
.
<span class="nt">&lt;/div&gt;</span>
<span class="nt">&lt;/div&gt;</span>
</pre></div>
</div></div>
<div class="label" id="fig-profile_follow_button"></div>
<div class="figure"><div class="center"><span class="graphic"><img src="images/figures/profile_follow_button_bootstrap.png" alt="profile_follow_button_bootstrap" /></span></div><div class="caption"><span class="header">Figure 11.12: </span><span class="description">A user profile with a follow button (<a href="http://localhost:3000/users/2">/users/2</a>).&nbsp;<a href="http://railstutorial.org/images/figures/profile_follow_button_bootstrap-full.png">(full size)</a></span></div></div>
<div class="label" id="fig-profile_unfollow_button"></div>
<div class="figure"><div class="center"><span class="graphic"><img src="images/figures/profile_unfollow_button_bootstrap.png" alt="profile_unfollow_button_bootstrap" /></span></div><div class="caption"><span class="header">Figure 11.13: </span><span class="description">A user profile with an unfollow button (<a href="http://localhost:3000/users/8">/users/6</a>).&nbsp;<a href="http://railstutorial.org/images/figures/profile_unfollow_button_bootstrap-full.png">(full size)</a></span></div></div>
<p>We&rsquo;ll get these buttons working soon enough&mdash;in fact, we&rsquo;ll do it two ways, the standard way (<a class="ref" href="following-users.html#sec-a_working_follow_button_the_standard_way">Section&nbsp;11.2.4</a>) and using Ajax (<a class="ref" href="following-users.html#sec-a_working_follow_button_with_ajax">Section&nbsp;11.2.5</a>)&mdash;but first we&rsquo;ll finish the HTML interface by making the following and followers pages.</p>
<div class="label" id="sec-following_and_followers_pages"></div>
<h3><a id="sec-11_2_3" href="following-users.html#sec-following_and_followers_pages" class="heading"><span class="number">11.2.3</span> Following and followers pages</a></h3>
<p>Pages to display followed users and followers will resemble a hybrid of the user profile page and the user index page (<a class="ref" href="updating-showing-and-deleting-users.html#sec-user_index">Section&nbsp;9.3.1</a>), with a sidebar of user information (including the following stats) and a list of users. In addition, we&rsquo;ll include a raster of user profile image links in the sidebar. Mockups matching these requirements appear in <a class="ref" href="following-users.html#fig-following_mockup">Figure&nbsp;11.14</a> (following) and <a class="ref" href="following-users.html#fig-followers_mockup">Figure&nbsp;11.15</a> (followers).</p>
<div class="label" id="fig-following_mockup"></div>
<div class="figure"><div class="center"><span class="graphic"><img src="images/figures/following_mockup_bootstrap.png" alt="following_mockup_bootstrap" /></span></div><div class="caption"><span class="header">Figure 11.14: </span><span class="description">A mockup of the user following page.&nbsp;<a href="http://railstutorial.org/images/figures/following_mockup_bootstrap-full.png">(full size)</a></span></div></div>
<div class="label" id="fig-followers_mockup"></div>
<div class="figure"><div class="center"><span class="graphic"><img src="images/figures/followers_mockup_bootstrap.png" alt="followers_mockup_bootstrap" /></span></div><div class="caption"><span class="header">Figure 11.15: </span><span class="description">A mockup of the user followers page.&nbsp;<a href="http://railstutorial.org/images/figures/followers_mockup_bootstrap-full.png">(full size)</a></span></div></div>
<p>Our first step is to get the following and followers links to work. We&rsquo;ll follow Twitter&rsquo;s lead and have both pages to require user signin, as tested in <a class="ref" href="following-users.html#code-following_followers_authorization_test">Listing&nbsp;11.28</a>. For signed-in users, the pages should have links for following and followers, respectively, as tested in <a class="ref" href="following-users.html#code-following_followers_tests">Listing&nbsp;11.29</a>.</p>
<div class="label" id="code-following_followers_authorization_test"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 11.28.</span> <span class="description">Tests for the authorization of the following and followers pages. <br /> <code>spec/requests/authentication_pages_spec.rb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="nb">require</span> <span class="s1">&#39;spec_helper&#39;</span>
<span class="n">describe</span> <span class="s2">&quot;Authentication&quot;</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">&quot;authorization&quot;</span> <span class="k">do</span>
<span class="n">describe</span> <span class="s2">&quot;for non-signed-in users&quot;</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">describe</span> <span class="s2">&quot;in the Users controller&quot;</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">&quot;visiting the following page&quot;</span> <span class="k">do</span>
<span class="n">before</span> <span class="p">{</span> <span class="n">visit</span> <span class="n">following_user_path</span><span class="p">(</span><span class="n">user</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">have_selector</span><span class="p">(</span><span class="s1">&#39;title&#39;</span><span class="p">,</span> <span class="ss">text:</span> <span class="s1">&#39;Sign in&#39;</span><span class="p">)</span> <span class="p">}</span>
<span class="k">end</span>
<span class="n">describe</span> <span class="s2">&quot;visiting the followers page&quot;</span> <span class="k">do</span>
<span class="n">before</span> <span class="p">{</span> <span class="n">visit</span> <span class="n">followers_user_path</span><span class="p">(</span><span class="n">user</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">have_selector</span><span class="p">(</span><span class="s1">&#39;title&#39;</span><span class="p">,</span> <span class="ss">text:</span> <span class="s1">&#39;Sign in&#39;</span><span class="p">)</span> <span class="p">}</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>
<span class="o">.</span>
<span class="o">.</span>
<span class="o">.</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="code-following_followers_tests"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 11.29.</span> <span class="description">Test for the <code>followed_users</code> and <code>followers</code> pages. <br /> <code>spec/requests/user_pages_spec.rb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="nb">require</span> <span class="s1">&#39;spec_helper&#39;</span>
<span class="n">describe</span> <span class="s2">&quot;User pages&quot;</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">&quot;following/followers&quot;</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">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="p">{</span> <span class="n">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="p">}</span>
<span class="n">describe</span> <span class="s2">&quot;followed users&quot;</span> <span class="k">do</span>
<span class="n">before</span> <span class="k">do</span>
<span class="n">sign_in</span> <span class="n">user</span>
<span class="n">visit</span> <span class="n">following_user_path</span><span class="p">(</span><span class="n">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">have_selector</span><span class="p">(</span><span class="s1">&#39;title&#39;</span><span class="p">,</span> <span class="ss">text:</span> <span class="n">full_title</span><span class="p">(</span><span class="s1">&#39;Following&#39;</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">have_selector</span><span class="p">(</span><span class="s1">&#39;h3&#39;</span><span class="p">,</span> <span class="ss">text:</span> <span class="s1">&#39;Following&#39;</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">have_link</span><span class="p">(</span><span class="n">other_user</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="ss">href:</span> <span class="n">user_path</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="n">describe</span> <span class="s2">&quot;followers&quot;</span> <span class="k">do</span>
<span class="n">before</span> <span class="k">do</span>
<span class="n">sign_in</span> <span class="n">other_user</span>
<span class="n">visit</span> <span class="n">followers_user_path</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">have_selector</span><span class="p">(</span><span class="s1">&#39;title&#39;</span><span class="p">,</span> <span class="ss">text:</span> <span class="n">full_title</span><span class="p">(</span><span class="s1">&#39;Followers&#39;</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">have_selector</span><span class="p">(</span><span class="s1">&#39;h3&#39;</span><span class="p">,</span> <span class="ss">text:</span> <span class="s1">&#39;Followers&#39;</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">have_link</span><span class="p">(</span><span class="n">user</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="ss">href:</span> <span class="n">user_path</span><span class="p">(</span><span class="n">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 only tricky part of the implementation is realizing that we need to add two new actions to the Users controller; based on the routes defined in <a class="ref" href="following-users.html#code-following_followers_actions_routes">Listing&nbsp;11.18</a>, we need to call them <code>following</code> and <code>followers</code>. Each action needs to set a title, find the user, retrieve either <code>@user.followed_users</code> or <code>@user.followers</code> (in paginated form), and then render the page. The result appears in <a class="ref" href="following-users.html#code-following_followers_actions">Listing&nbsp;11.30</a>.</p>
<div class="label" id="code-following_followers_actions"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 11.30.</span> <span class="description">The <code>following</code> and <code>followers</code> actions. <br /> <code>app/controllers/users_controller.rb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="k">class</span> <span class="nc">UsersController</span> <span class="o">&lt;</span> <span class="no">ApplicationController</span>
<span class="n">before_filter</span> <span class="ss">:signed_in_user</span><span class="p">,</span>
<span class="ss">only:</span> <span class="o">[</span><span class="ss">:index</span><span class="p">,</span> <span class="ss">:edit</span><span class="p">,</span> <span class="ss">:update</span><span class="p">,</span> <span class="ss">:destroy</span><span class="p">,</span> <span class="ss">:following</span><span class="p">,</span> <span class="ss">:followers</span><span class="o">]</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="vi">@title</span> <span class="o">=</span> <span class="s2">&quot;Following&quot;</span>
<span class="vi">@user</span> <span class="o">=</span> <span class="no">User</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="n">params</span><span class="o">[</span><span class="ss">:id</span><span class="o">]</span><span class="p">)</span>
<span class="vi">@users</span> <span class="o">=</span> <span class="vi">@user</span><span class="o">.</span><span class="n">followed_users</span><span class="o">.</span><span class="n">paginate</span><span class="p">(</span><span class="ss">page:</span> <span class="n">params</span><span class="o">[</span><span class="ss">:page</span><span class="o">]</span><span class="p">)</span>
<span class="n">render</span> <span class="s1">&#39;show_follow&#39;</span>
<span class="k">end</span>
<span class="k">def</span> <span class="nf">followers</span>
<span class="vi">@title</span> <span class="o">=</span> <span class="s2">&quot;Followers&quot;</span>
<span class="vi">@user</span> <span class="o">=</span> <span class="no">User</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="n">params</span><span class="o">[</span><span class="ss">:id</span><span class="o">]</span><span class="p">)</span>
<span class="vi">@users</span> <span class="o">=</span> <span class="vi">@user</span><span class="o">.</span><span class="n">followers</span><span class="o">.</span><span class="n">paginate</span><span class="p">(</span><span class="ss">page:</span> <span class="n">params</span><span class="o">[</span><span class="ss">:page</span><span class="o">]</span><span class="p">)</span>
<span class="n">render</span> <span class="s1">&#39;show_follow&#39;</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 here that both actions make an <em>explicit</em> call to <code>render</code>, in this case rendering a view called <code>show_follow</code>, which we must create. The reason for the common view is that the ERb is nearly identical for the two cases, and <a class="ref" href="following-users.html#code-show_follow_view">Listing&nbsp;11.31</a> covers them both.</p>
<div class="label" id="code-show_follow_view"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 11.31.</span> <span class="description">The <code>show_follow</code> view used to render following and followers. <br /> <code>app/views/users/show_follow.html.erb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="cp">&lt;%</span> <span class="n">provide</span><span class="p">(</span><span class="ss">:title</span><span class="p">,</span> <span class="vi">@title</span><span class="p">)</span> <span class="cp">%&gt;</span>
<span class="nt">&lt;div</span> <span class="na">class=</span><span class="s">&quot;row&quot;</span><span class="nt">&gt;</span>
<span class="nt">&lt;aside</span> <span class="na">class=</span><span class=