MAKING FOLLOW-MAPPING ON RAILS

MAKING FOLLOW-MAPPING ON RAILS

 

STEP-1:  On terminal

$rails g model FollowMapping follower_id:integer followee_id:integer
$rake db:migrate

STEP-2: home_controller

def users
  @users = User.all  
end

STEP-3: In home folder, create a file users.html.erb

<%= user.email %> <% if current_user.can_follow user.id %> 
<%= link_to "Follow", url_for(controller: :home, action: :follow, followee_id: user.id), method: :post %>
<% end %>

<% if current_user.can_un_follow user.id %> 
<%= link_to "UnFollow", url_for(controller: :home, action: :un_follow, followee_id: user.id), method: :post %>
<% end %>

STEP-4: in routes.rb

get '/users' => 'home#users'

STEP-5: in index.html.erb

<% link_to "Users", user_path %>

STEP-6: in user.rb

def follow_relation user_id  
  return UserRelations::SELF if id == user_id  
  if FollowMapping.where(:followee_id => user_id, :follower_id => id).length > 0  
return UserRelations::FOLLOWED  
else  return UserRelations::NOTFOLLOWED  
end  
end
  def can_follow user_id  
return follow_relation(user_id) == UserRelations::NOTFOLLOWED  
end
  def can_un_follow user_id    
return follow_relation(user_id) == UserRelations::FOLLOWED  
end
  def followee_ids  
FollowMapping.where(follower_id: id).pluck(:followee_id)  
end

  class UserRelations  
SELF = 0  
FOLLOWED = 1  
NOTFOLLOWED = 2  
end

Note: Just take care of not missing the main end after all this

STEP-7: users.html.erb

<%= user.email %> <% if current_user.can_follow user.id %> 
<%= link_to "Follow", url_for(controller: :home, action: :follow, followee_id: user.id), method: :post %>
<% end %>

<% if current_user.can_un_follow user.id %> 
<%= link_to "UnFollow", url_for(controller: :home, action: :un_follow, followee_id: user.id), method: :post %>
<% end %>

STEP-8: In home_controller

def follow
  followee_id = params[:followee_id]  
if current_user.can_follow followee_id  
FollowMapping.create(:followee_id => followee_id, :follower_id => current_user.id)  
else  
end  
return redirect_to '/users'  
end

def un_follow  followee_id = params[:followee_id]  
if current_user.can_un_follow followee_id  FollowMapping.where(:followee_id => followee_id, :follower_id => current_user.id).first.destroy  else  
end  
return redirect_to '/users'  
end

STEP-9: now in Followmapping.rb

class FollowMapping < ActiveRecord::Base
 validates_uniqueness_of :followee_id, scope: :follower_id
end

STEP-10: in  routes.rb

post '/follow' => 'home#follow'
 post '/un_follow' => 'home#un_follow'

STEP-11: Make changes in db -> migrate -> somenumber_create_follow_mapping.rb file

class CreateFollowMappings < ActiveRecord::Migration
 def change
 create_table :follow_mappings do |t|
 t.integer :follower_id
 t.integer :followee_id

t.timestamps null: false
 end
 end
end

STEP-12: in terminal,

$rake db:migrate

Hope it helps.

 

One thought on “MAKING FOLLOW-MAPPING ON RAILS

Add yours

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: