Introduction
Social media platforms like Twitter and Instagram have revolutionized the way we interact with each other. One of the key features of these platforms is the ability to follow other users and see their updates on your timeline. In this blog post, we will discuss how to create followship models in Ruby on Rails, similar to those used by Twitter and Instagram.
Creating a Followship Model
I assume we have a User
model, which will be used for followship modeling. The first step in creating a followership model is to generate a new model in Ruby on Rails. This can be done by running the following command in the terminal:
rails generate model follow follower_id:bigint:index followed_id:bigint:index
This command will create a new model called 'Follow' with two attributes: follower_id
and followed_id.
The follower_id
attribute represents the user following someone else, while the followed_id
attribute represents the user being followed.
Add a unique index for the scope of follower_id
and followed_id
so that if a user is already following a user, then that user cannot follow that user again to keep data in the database consistent:
class CreateFollows < ActiveRecord::Migration[7.0]
def change
create_table :follows do |t|
t.bigint :follower_id
t.bigint :followed_id
t.timestamps
end
add_index :follows, :follower_id
add_index :follows, :followed_id
add_index :follows, [:follower_id, :followed_id], unique: true
end
end
Also, update the model accordingly:
class Follow < ApplicationRecord
belongs_to :follower, class_name: 'User'
belongs_to :followed, class_name: 'User'
validates :follower_id, uniqueness: { scope: :followed_id }
validate :follower_is_not_followed
def follower_is_not_followed
errors.add(:follower, 'cannot follow themselves') if follower == followed
end
end
By this, we are disallowing the user to follow himself and another user he is already following.
Next, we need to set up the associations between the Follow and User models. We can do this by adding the following code to the User model:
class User < ApplicationRecord
has_many :active_relationships, class_name: 'Follow', foreign_key: 'follower_id', dependent: :destroy,
inverse_of: :follower
has_many :followings, through: :active_relationships, source: :followed
has_many :passive_relationships, class_name: 'Follow', foreign_key: 'followed_id', dependent: :destroy,
inverse_of: :followed
has_many :followers, through: :passive_relationships, source: :follower
end
This code sets up a many-to-many relationship between the User and Follow models. It also creates two associations for each user: 'followers' and 'following.’ The 'followers' association represents the users following the current user, while the ‘following’ association represents the users the current user is following.
Implementing Follow and Unfollow Functionality
Once the following model is set up, we can implement our application follow and unfollow functionality. In the user model, we create methods to follow and unfollow:
class User < ApplicationRecord
# ...
# This method make user to follow other_user
def follow(other_user)
active_relationships.create(followed_id: other_user.id)
end
# This method make user to unfollow other_user
def unfollow(other_user)
active_relationships.find_by(followed_id: other_user.id)&.destroy
end
# This method returns true if a user is following other_user
def following?(other_user)
followings.include?(other_user)
end
end
The 'follow' action adds the selected user to the current user's 'following' association, while the 'unfollow' action removes the selected user from the current user's 'following' association. These actions can be called from our views using links or buttons.
Finally, if you want to find out the user’s followings, you can write user.followings
; when we want to find out the user’s followers, write user.followers
.
Conclusion
Creating follow models in Ruby on Rails is a great way to implement social media-like functionality in your application. Following the steps outlined in this blog post, you can easily set up a many-to-many relationship between your User and Followship models and implement follow and unfollow functionality for your users. With these features, your application will be more engaging and interactive for your users.
Happy Coding!
Contact me at sulman@hey.com
Top comments (0)