/episodes/163/en.html
HTML | 194 lines | 159 code | 35 blank | 0 comment | 0 complexity | 055a88c834e9492a4715eab1b16702cc MD5 | raw file
- <p>Below is a page from a basic social networking application. A user can sign up, then log in and interact with other users. The page shown displays a list of the users and has a link next to each one that allows you to add them as a friend.</p>
- <div class="imageWrapper">
- <img src="/system/photos/110/original/E163I01.png" width="808" height="452" alt="The users list page on our application."/>
- </div>
- <p>Although the link is there, it currently does nothing. In this episode we’ll write the code needed to enable a user to add other users as friends. To do this we’re going to need to create a self-referencing association: users will have relationships with other users, but instead of creating a relationship between two different models, we’ll be creating a relationship between two instances of the same model.</p>
- <h3>Generating The Right Actions</h3>
- <p>The “Add Friend” link on the users page doesn’t as yet link to anything.</p>
- <pre class="ruby">
- <%= link_to "Add Friend" %>
- </pre>
- <p>At some point we’re going to have to wire it up to an action in a controller, and it’s worth thinking a little now about what controller and action it will call when it’s clicked. We already have a <code>UsersController</code>, so this might seem the obvious place to deal with friends as friends are just other users. We could add two new actions to the <code>UsersController</code> to deal with adding and removing friends.</p>
- <pre class="ruby">
- def add_friend
-
- end
-
- def remove_friend
-
- end
- </pre>
- <p>This, however, is wrong for several reasons. The first alarm bell that should ring in your head when you start creating controller methods like these is that they are not among the seven standard RESTful methods (index, show, new, create, edit, update and destroy). Another concern should be raised by the fact that these methods both have <code>_friend</code> appended to them. This suggests that we’re putting these methods in their own namespace. The final clue that this isn’t the best approach is the first part of each method name: <code>add</code> and <code>remove</code> indicate that we’re creating and destroying a resource. All of these points give us a big hint that we should be creating a new controller for handling friends.</p>
- <h3>Creating Friendships</h3>
- <p>The controller we’ll create will be called <code>FriendshipsController</code>. The naming of this controller is important. We might have called it <code>FriendsController</code> but in our application a friend is just another <code>User</code>, and we’ll be using our new controller to create and destroy relationships between users, not to create and destroy users.</p>
- <p>A user can have many friends and be befriended by many other users so we’re going to have to create a many-to-many relationship. There are two ways to define this type of relationship in Rails: <code>has_and_belongs_to_many</code> and <code>has_many :through</code>. (You can find more information about these two methods by watching <a href="http://railscasts.com/episodes/47-two-many-to-many">Railscast 47</a>). These days, <code>has_many :through</code> is the most used, and it’s what we’ll be using to define our friendships.</p>
- <p>To use <code>has_many :through</code> we’ll need to create a join model, which we’ll call <code>Friendship</code>. This model will have two fields, a <code>user_id</code> that represents the current user who’s adding a friend and a <code>friend_id</code> that represents the user who’s being befriended.</p>
- <p>We’ll create our new model in the usual way,</p>
- <pre class="terminal">
- script/generate model Friendship user_id:integer friend_id:integer
- </pre>
- <p>and then run the generated migration.</p>
- <pre class="terminal">
- rake db:migrate
- </pre>
- <p>Along with the model we’ll need to generate the <code>FriendshipsController</code> we mentioned earlier.</p>
- <pre class="terminal">
- script/generate controller Friendships
- </pre>
- <p>As we’re using <code>Friendship</code> as a resource we’ll need to add the following line to <code>/config/routes.rb</code>.</p>
- <pre class="ruby">
- map.resources :friendships
- </pre>
- <p>Now that we have our model and controller we can start to define how a <code>Friendship</code> works. We’ll start off by defining the relationships between <code>User</code> and <code>Friendship</code> in the model classes.</p>
- <pre class="ruby">
- class Friendship < ActiveRecord::Base
- belongs_to :user
- belongs_to :friend, :class_name => 'User'
- end
- </pre>
- <p>A <code>Friendship</code> will <code>belong_to</code> a <code>User</code>, who will be the user who initialised the relationship, and will also belong to a <code>Friend</code>, who is the user who has been befriended. For the <code>friend</code> relationship we need to explicitly specify the class name as ActiveRecord cannot work out the model to use from the name in the association.</p>
- <p>We’ll also need to define the other half of the relationship in the <code>User</code> model.</p>
- <pre class="ruby">
- class User < ActiveRecord::Base
-
- has_many :friendships
- has_many :friends, :through => :friendships
- # rest of class omitted.
- end
- </pre>
- <p>The relationship here is defined as you would define any other many-to-many relationship. There’s no need to specify any custom naming as the relationships can all be worked out from the names of the relationships.</p>
- <h3>Wiring Up The “Add Friend” Link</h3>
- <p>Now that the relationships between the models have been defined, and we have our <code>FriendshipsController</code> we can start work on the “Add Friend” link in the users’ <code>index</code> view.</p>
- <pre class="ruby">
- <% for user in @users %>
- <div class="user">
- <p>
- <strong><%=h user.username %></strong>
- <%= link_to "Add Friend", friendships_path(:friend_id => user), :method => :post %>
- <div class="clear"></div>
- </p>
- </div>
- <% end %>
- </pre>
- <p>The link needs to call the <code>create</code> method in the <code>FriendshipsController</code>. To do that we have the link POST to the <code>friendships_path</code>. That isn’t quite enough though; we also need to pass the user that we’re going to befriend. We do that by adding it as a parameter to the <code>friendships_path</code> method. That will pass the <code>id</code> of the user to the <code>create</code> action so that it knows which user we’re befriending.</p>
- <p>Now that the link code is written we’ll move on to the <code>FriendshipsController</code> and write the <code>create</code> action.</p>
- <pre class="ruby">
- def create
- @friendship = current_user.friendships.build(:friend_id => params[:friend_id])
- if @friendship.save
- flash[:notice] = "Added friend."
- redirect_to root_url
- else
- flash[:notice] = "Unable to add friend."
- redirect_to root_url
- end
- end
- </pre>
- <p>We create the new friendship by taking our current user and building a friendship through it, passing it the <code>id</code> of the user we’re befriending. Using <code>build</code> will mean that the <code>user_id</code> of the new <code>Friendship</code> is automatically set, and as we’ve passed the <code>friend_id</code> from the parameters the relationship is fully defined. We can then save the relationship and redirect back to the home page, displaying a <code>flash</code> notice that says that the friend has been added. If for some reason the relationship is invalid then we’ll show a different <code>flash</code> notice. Obviously, if this was an application that was going into production then we’d give the user more specific information as to why their request failed.</p>
- <h3>Viewing Our Friends</h3>
- <p>A logged-in user can now click the “Add Friend” link and make another user their friend. They can’t, however, see a list of their friends. To fix this we’ll modify the user’s profile page so that they can see who they have added as a friend. The profile page is the <code>show</code> page for a <code>User</code>. Currently it just shows their name and provides a link that allows them to find friends.</p>
- <pre class="ruby">
- <% title "My Profile" %>
- <p>Username: <%=h @user.username %></p>
- <p><%= link_to "Find Friends", users_path %></p>
- </pre>
- <p>To display a list of their friends, we can loop through the user’s <code>friends</code> collection and display each one.</p>
- <pre class="ruby">
- <h2>Your Friends</h2>
- <ul>
- <% for user in @user.friends %>
- <li><%= h. user.username %></li>
- <% end %>
- </ul>
- </pre>
- <p>The profile page will now show the list of our friends.</p>
- <div class="imageWrapper">
- <img src="/system/photos/111/original/E163I02.png" width="808" height="452" alt="Our friends are now listed on our profile page."/>
- </div>
- <h3>Removing Friends</h3>
- <p>It would be useful if there was a link next to each of our listed friends that allowed us to remove that user as a friend. To implement that we need to add a link to the <code>FriendshipController</code>’s <code>destroy</code> action, passing the <code>id</code> of the friendship. It’s here where things could get a little tricky as we’re looping through <code>User</code>s and don’t have access to the <code>Friendship</code> model to get the <code>id</code> that identifies the relationship. This is a common problem that beginners to Rails encounter when they use many-to-many relationships as they can forget that there is a join model that we can interact with directly rather than, in this case jumping directly from <code>User</code> to <code>Friend</code>.</p>
- <p>Instead of looping through a user’s friends to create the list, we’ll instead loop through their friendships. The code in the view will now change to this:</p>
- <pre class="ruby">
- <h2>Your Friends</h2>
- <ul>
- <% for friendship in @user.friendships %>
- <li>
- <%= h friendship.friend.username %>
- (<%= link_to "remove", friendship, :method => :delete %>)
- </li>
- <% end %>
- </ul>
- </pre>
- <p>We can now get each friend’s name by using <code>friendship.friend.username</code> and create our delete link by passing the friendship as a parameter to the link and telling it to use DELETE as the method so that it will call the <code>destroy</code> action.</p>
- <p>Talking of the <code>destroy</code> action, it’s now time to implement it in the <code>FriendshipsController</code>.</p>
- <pre class="ruby">
- def destroy
- @friendship = current_user.friendships.find(params[:id])
- @friendship.destroy
- flash[:notice] = "Removed friendship."
- redirect_to current_user
- end
- </pre>
- <p>Note that in the first line of the action we search for the friendship only within the current user’s friendships. If we just called <code>Friendship.find(params[:id])</code> then a maliciously minded user could destroy relationships between any two users, when they should be restricted to only destroying relationships they themselves have made. The rest of the action destroys the friendship and then redirects back to the user’s profile page.</p>
- <p>The profile page will now show a link and we’ll be able to remove a friendship with another user by clicking the link next to their name.</p>
- <div class="imageWrapper">
- <img src="/system/photos/112/original/E163I03.png" width="808" height="452" alt="Removing a friend from our list."/>
- </div>
- <h3>Inverse Relationships</h3>
- <p>When creating self-referential relationships it’s important to remember that we’re only creating one side of the relationship. Although <code>eifion</code> has <code>paul</code> listed as a friend above, if we were to visit <code>paul</code>’s profile we wouldn’t see <code>eifion</code> listed unless <code>paul</code> had chosen to add him. We need two <code>Friendship</code> records to create a mutual friendship.</p>
- <p>To finish this episode we’ll add a list to the profile page so that a user can see who has them listed as a friend so that we can see the other side of this relationship. To do this we need to add two more relationships to the <code>User</code> model.</p>
- <pre class="ruby">
- class User < ActiveRecord::Base
-
- has_many :friendships
- has_many :friends, :through => :friendships
-
- has_many :inverse_friendships, :class_name => "Friendship", :foreign_key => "friend_id"
- has_many :inverse_friends, :through => :inverse_friendships, :source => :user
- #rest of class omitted.
- end
- </pre>
- <p>It’s difficult to think up appropriate names to define the other side of the relationship so we’ll prefix both with the word “inverse” to give us <code>inverse_friendships</code> and <code>inverse_friends</code>. We need to specify some additional options to make the relationships work. For <code>inverse_friendships</code> we’ll have to specify the name of the other model as it can’t be inferred from the relationship name and we’ll also have to define the foreign key as <code>friend_id</code>. For the <code>inverse_friends</code> relationship we need to specify the <code>source</code> as <code>users</code>, as again it cannot be inferred from the name of the relationship.</p>
- <p>Back in the profile page we want to show a list of the users who have befriended us. To do this we just need to loop through our <code>inverse_friends</code> and display their user names.</p>
- <pre class="ruby">
- <h2>Users Who Have Befriended You</h2>
- <ul>
- <% for user in @user.inverse_friends %>
- <li><%= h user.username %></li>
- <% end %>
- </ul>
- </pre>
- <p>If we log in as <code>paul</code> and add <code>eifion</code> as one of his friends, then log back in as <code>eifion</code> we’ll now see <code>paul</code> listed as having added <code>eifion</code> as a friend.</p>
- <div class="imageWrapper">
- <img src="/system/photos/113/original/E163I04.png" width="808" height="472" alt="We can now see who call us a friend."/>
- </div>
- <p>That’s it for this episode. We haven’t quite recreated Facebook, but hopefully you’ve now got a good idea of how self-referential relationships work in Ruby on Rails.</p>