[RUBY] How to make a follow function in Rails

Premise

☆ Required table ・ Users table ・ Relationships table (intermediate table) ☆ Point ・ The association is different from the usual "many-to-many"

flow

  1. Make a relationship model
  2. Edit & execute relationship migration file
  3. Write associations in the user and relationships models
  4. Write the follow function method in the user model
  5. Follow to seeds
  6. Create and edit relationships controller
  7. Number of followers / followers, follow button installed in view
  8. Write the routing
  9. Write the follow function in the users controller

1. Make a relationship model

Execute relationships, which are intermediate tables, using associations to create a "many-to-many" relationship between user tables. Because followers are also users. First, let's create a relationships model.

Terminal


$ rails g model Relationship follower_id:integer followed_id:integer

The columns of the relationships table

column type
follower_id integer
followed_id integer

Will be.

2. Edit & execute relationship migration file

Please edit as below

db/migrate/_creation_relationships.rb


class CreateRelationships < ActiveRecord::Migration[5.2]
  def change
    create_table :relationships do |t|
      t.integer :follower_id
      t.integer :followed_id

      t.timestamps


    end
    add_index :relationships, :follower_id
    add_index :relationships, :followed_id
    add_index :relationships, [:follower_id, :followed_id], unique: true
  #add_index :table name,Column name
  end
end

By using add_index, data can be read / acquired faster. Once this is done, reset the migration file and use seed to populate the database with the initial data.

Terminal


$ rails db:migrate:reset db:seed

3. Write associations in the user and relationships models

First of all Write the association in the relationships model.

models/user.rb


#Take out the people you follow
has_many :active_relationships, foreign_key: "follower_id",
                                class_name: "Relationship",
                                dependent: :destroy

class_name: You can specify the Relationship class directly by using "Relationship"

models/relationship.rb


class Relationship < ApplicationRecord

 belongs_to :follower, class_name: "User"
 belongs_to :followed, class_name: "User"

 validates :follower_id, presence: true
 validates :followed__id, presence: true
end

** class_name: By supplementary setting with'User' **, it is prevented to refer to the non-existing class called follower/followed class, and it is clearly stated that it is a User class. In short, it means "belongs_to to the user model because there is no follower/followed model!".

models/user.rb


has_many :active_relationships, foreign_key: "follower_id",
                                class_name: "Relationship",
                                dependent: :destroy

#
has_many :followed_users, through: :active_relationships, source: :followed

4. Write the follow function method in the user model

5. Follow to seeds

6. Create and edit relationships controller

7. Number of followers / followers, follow button installed in view

8. Write the routing

9. Write the follow function in the users controller

Recommended Posts

How to make a follow function in Rails
How to insert a video in Rails
How to implement a like feature in Rails
How to easily create a pull-down in Rails
Implement follow function in Rails
[Rails] How to make seed
How to implement a like feature in Ajax in Rails
I tried to make a login function in Java
[How to insert a video in haml with Rails]
How to write a date comparison search in Rails
I want to define a function in Rails Console
[Rails 6] How to set a background image in Rails [CSS]
[Rails] How to load JavaScript in a specific view
Add a search function in Rails.
[Rails] How to write in Japanese
How to make a Java container
How to make a splash screen
How to introduce jQuery in Rails 6
How to make a Maven project
How to make a unique combination of data in the rails intermediate table
How to make a Java array
How to install Swiper in Rails
How to display a graph in Ruby on Rails (LazyHighChart)
Rails follow function
How to implement search functionality in Rails
How to change app name in rails
How to use custom helpers in rails
How to use MySQL in Rails tutorial
How to make a Discord bot (Java)
Make a login function with Rails anyway
Steps to set a favicon in Rails
Implemented follow function in Rails (Ajax communication)
[rails] How to configure routing in resources
[rails] How to create a partial template
How to implement ranking functionality in Rails
How to publish a library in jCenter
How to use credentials.yml.enc introduced in Rails 5.2
[Rails] A simple way to implement a self-introduction function in your profile
How to make a jar file with no dependencies in Maven
[Rails] How to put a crown mark on the ranking function
How to rename a model with foreign key constraints in Rails
I tried to make a group function (bulletin board) with Rails
Rails: How to write a rake task nicely
Convert to a tag to URL string in Rails
[Rails] How to write when making a subquery
How to implement a slideshow using slick in Rails (one by one & multiple by one)
[Rails] How to create a graph using lazy_high_charts
How to create a query using variables in GraphQL [Using Ruby on Rails]
How to display a web page in Java
[Rails] How to use select boxes in Ransack
Add a tag function to Rails. Use acts-as-taggable-on
How to translate Rails into Japanese in general
How to prevent direct URL typing in Rails
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
How to run a djUnit task in Ant
How to add a classpath in Spring Boot
How to create a theme in Liferay 7 / DXP
How to make a lightweight JRE for distribution
How to uninstall Rails
How to update user edits in Rails Devise without entering a password
How to make shaded-jar