The Relationship Model in Java

Print DataMatrix in Java The Relationship Model
The Relationship Model
DataMatrix Drawer In Java
Using Barcode drawer for Java Control to generate, create Data Matrix image in Java applications.
@userfollowingshould include(@followed) end end end
Bar Code Printer In Java
Using Barcode encoder for Java Control to generate, create bar code image in Java applications.
Note that we have replaced the include method seen in Listing 1131 with should
Barcode Decoder In Java
Using Barcode decoder for Java Control to read, scan read, scan image in Java applications.
include, effectively transforming
Print DataMatrix In C#.NET
Using Barcode generation for VS .NET Control to generate, create Data Matrix ECC200 image in .NET applications.
@userfollowinginclude (@followed)should be_true
Data Matrix Creator In .NET Framework
Using Barcode encoder for ASP.NET Control to generate, create Data Matrix 2d barcode image in ASP.NET applications.
into the clearer and more succinct
Painting Data Matrix In Visual Studio .NET
Using Barcode generation for .NET framework Control to generate, create Data Matrix ECC200 image in .NET applications.
@userfollowingshould include(@followed)
ECC200 Drawer In VB.NET
Using Barcode drawer for Visual Studio .NET Control to generate, create Data Matrix 2d barcode image in .NET framework applications.
This example shows just how flexible the RSpec boolean convention is; even though include is already a Ruby keyword (used to include a module, as seen in, eg, Listing 911), in this context RSpec correctly guesses that we want to test array inclusion In the application code, the following method takes in a user, called followed, and checks to see if a follower with that id exists in the database; the follow! method calls create! through the relationships association to create the following relationship The results appear in Listing 12139
Encoding DataMatrix In Java
Using Barcode encoder for Java Control to generate, create Data Matrix 2d barcode image in Java applications.
Listing 1213 The following and follow! utility methods
GTIN - 12 Creation In Java
Using Barcode encoder for Java Control to generate, create UPC A image in Java applications.
app/models/userrb
Barcode Drawer In Java
Using Barcode generator for Java Control to generate, create bar code image in Java applications.
class User < ActiveRecord::Base def selfauthenticate_with_salt(id, stored_salt) end def following (followed) relationshipsfind_by_followed_id(followed) end
Code39 Maker In Java
Using Barcode creation for Java Control to generate, create ANSI/AIM Code 39 image in Java applications.
9 The authenticate_with_salt method is included simply to orient you within the User model file
Draw Barcode In Java
Using Barcode encoder for Java Control to generate, create bar code image in Java applications.
def follow!(followed) relationshipscreate!(:followed_id => followedid) end end
Intelligent Mail Creation In Java
Using Barcode maker for Java Control to generate, create 4-State Customer Barcode image in Java applications.
12: Following Users
Read ANSI/AIM Code 39 In .NET Framework
Using Barcode recognizer for Visual Studio .NET Control to read, scan read, scan image in Visual Studio .NET applications.
Note that in Listing 1213 we have omitted the user itself, writing just
Creating Data Matrix ECC200 In Visual Studio .NET
Using Barcode printer for .NET Control to generate, create DataMatrix image in .NET framework applications.
relationshipscreate!()
Draw Data Matrix ECC200 In Visual Basic .NET
Using Barcode encoder for .NET framework Control to generate, create DataMatrix image in VS .NET applications.
instead of the equivalent code
Decode Bar Code In Java
Using Barcode decoder for Java Control to read, scan read, scan image in Java applications.
selfrelationshipscreate!()
Draw Code 128C In C#.NET
Using Barcode creator for Visual Studio .NET Control to generate, create Code 128 Code Set B image in Visual Studio .NET applications.
Whether to include the explicit self is largely a matter of taste Of course, users should be able to unfollow other users as well as follow them, which leads to the somewhat predictable unfollow! method, as shown in Listing 121410
GS1-128 Maker In VS .NET
Using Barcode creation for ASP.NET Control to generate, create GS1 128 image in ASP.NET applications.
Listing 1214 A test for unfollowing a user
Create USS Code 39 In C#.NET
Using Barcode generation for Visual Studio .NET Control to generate, create Code 39 Extended image in Visual Studio .NET applications.
spec/models/user_specrb
describe User do describe "relationships" do it "should have an unfollow! method" do @followedshould respond_to(:unfollow!) end it "should unfollow a user" do @userfollow!(@followed)
10 The unfollow! method doesn t raise an exception on failure in fact, I don t even know how Rails indicates a failed destroy but we use an exclamation point to maintain the follow!/unfollow! symmetry
The Relationship Model
@userunfollow!(@followed) @usershould_not be_following(@followed) end end end
The code for unfollow! is straightforward: just find the relationship by followed id and destroy it (Listing 1215)11
Listing 1215 Unfollowing a user by destroying a user relationship
app/models/userrb
class User < ActiveRecord::Base def following (followed) relationshipsfind_by_followed_id(followed) end def follow!(followed) relationshipscreate!(:followed_id => followedid) end def unfollow!(followed) relationshipsfind_by_followed_id(followed)destroy end end
1215 Followers
The final piece of the relationships puzzle is to add a userfollowers method to go with userfollowing You may have noticed from Figure 127 that all the information needed to extract an array of followers is already present in the relationships table Indeed, the technique is exactly the same as for user following, with the roles of
11 You might notice that sometimes we access id explicitly, as in followedid, and sometimes we just use followed I am ashamed to admit that my usual algorithm for telling when to leave it off is to see if it works without id, and then add id if it breaks
12: Following Users
id 1
user name email Michael Hartl mhartl@examplecom
userfollowers reverse_relationships follower_id followed_id 1 2 1 7 3 1 7 2 1 10 2 1 1 8 9 1 user id 3 has_many user id 2 has_many user has_many id 9 name email name email name email
through
User has_many : followers, :through => :reverse_relationships, :source => follower_id
Figure 129 A model for user followers using a reverse Relationship model (full size)
follower_id and followed_id reversed This suggests that, if we could some-
how arrange for a reverse_relationships table with those two columns reversed (Figure 129), we could implement userfollowers with little effort We begin with the tests, having faith that the magic of Rails will come to the rescue (Listing 1216)
Listing 1216 Testing for reverse relationships
spec/models/user_specrb
describe User do describe "relationships" do it "should have a reverse_relationships method" do @usershould respond_to(:reverse_relationships) end it "should have a followers method" do
The Relationship Model
@usershould respond_to(:followers) end
it "should include the follower in the followers array" do @userfollow!(@followed) @followedfollowersshould include(@user) end end end
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 following to simulate a reverse_relationships table by passing followed_id as the primary key In other words, where the relationships association uses the follower_id foreign key,
has_many :relationships, :foreign_key => "follower_id"
the reverse_relationships association uses followed_id:
has_many :reverse_relationships, :foreign_key => "followed_id"
The followers association then gets built through the reverse relationships, as shown in Listing 1217
Listing 1217 Implementing userfollowers using reverse relationships
app/models/userrb
class User < ActiveRecord::Base has_many :reverse_relationships, :foreign_key => "followed_id", :class_name => "Relationship", :dependent => :destroy has_many :followers, :through => :reverse_relationships, :source => :follower end
12: Following Users
(As with Listing 125, the test for dependent :destroy is left as an exercise (Section 125)) Note that we actually have to include the class name for this association, ie,
has_many :reverse_relationships, :foreign_key => "followed_id", :class_name => "Relationship"
because otherwise Rails will look for a ReverseRelationship class, which doesn t exist It s also worth noting that we could actually omit the :source key in this case, using simply