[RUBY] Where the follow function implementation is stuck

find_or_create_by method

def follow(other_user)
  unless self == other_user #Trying to follow other_Verify that user is not himself
    self.relationships.find_or_create_by(follow_id: other_user.id)
  end
end

If there is data that meets the condition of the argument, return it find_by (other_user), otherwise create a new create (other_user) Save after creating a new one

Reference article: What is the difference between find_or_initialize_by and find_or_create_by?

includes method

def following?(other_user)
  self.followings.include?(other_user)
end
What is the include? method?

Method to judge whether the element specified by the argument is included in the array

relationship model migration file

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

      t.timestamps null: false

      add_index :relationships, :follower_id
      add_index :relationships, :following_id
      add_index :relationships, [:follower_id, :follwing_id], unique: true #follow_id and following_Setting to prevent the same combination of id
    end
  end
end

I understand that ʻadd_index adds an index, but what is the purpose of indexing in the first place? This article was helpful because I didn't understand the meaning of the description of ʻadd_index.

Reference article: How to put an index in the database

Q. What is the index for?

To make it easier to find when retrieving data from columns. For example, in this case, to make it easier to search for followers from the follower column. I don't need to have only 3 people in the data, but when it comes to 10,000 people, it takes time to search, so it seems better to add it.

add_How to write index


add_index :table name,Column name

In the above example, it is the following_id column of the relation table and the following_id column of the relation table.

How to write a user model

user.rb


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

The follwing_relationships model doesn't really exist. It's just set like this for convenience. This model is a model of the people who follow. So follower_id is set with a foreign key.

class_name:" Relationship " As I said earlier, the follwing_relationships model doesn't really exist, and it actually exists, so the name is follwing_relationships. , The name alone really means "Relationship".

dependent:: destroy This is fairly simple, and when a user disappears, the relationship with the people that the user is following disappears. The user model and the relationship model are dependent, and when the user disappears, the relationship (relationship between the user and follow_ids) of that user also disappears.

user.rb


has_many :follwings, through: :following_relationships

The association between models is defined by has_many: follwings (model name). through :: following_relationships This shows an intermediate table. It's been mentioned several times, but the name is following_relationships, but in reality relationships is the intermediate table.

The point is that there is a relation model between the two user models, the user model (followed people) and the user model (followed people).

Two conditions for using the association
  1. Relationships are defined in the model class with methods such as has_many and belongs_to
  2. There is a column called model name_id that belongs to the table to which it belongs

Especially note that 2. is easy to forget !! This time, following_id is required, but if you look at the above migration file, you can see that following_id is put there, but this time it is OK because it is defined there.

user.rb


def following?(other_user) 
  following_relationships.find_by(following_id: other_user.id)
end

def follow!(other_user)
  following_relationships.create!(following_id: other_user.id)
end

def unfollow!(other_user)
  following_relationships.find_by(following_id: other_user.id).destroy
end

The find_by method returns only one. So, if you set this, you can pinpoint the user you want to find. Reference article: [[Rails] Various usages of the most detailed find_by method in the world](https://pikawaka.com/rails/find_by#find_by%E3%83%A1%E3%82%BD%E3%83% 83% E3% 83% 89% E3% 82% 92% E4% BD% BF% E7% 94% A8% E3% 81% 99% E3% 82% 8B% E5% A0% B4% E9% 9D% A2% E3% 81% A8% E3% 81% AF% EF% BC% 9F)

create! means to save as it is.

The last one is to find one user and delete the .destroy method, so delete that data.

What I still don't understand is why write methods in the model? Normally I thought that such a method would be written in the controller. When I searched, I found this article. How do I use the methods I wrote in the Rails model in the controller?

What I found out was to make the description of the controller as simple as possible. It seems like that. Certainly, if you write the above description in the controller, it will be confusing and difficult to understand.

My Gresh Sean File Error

Caused by: Mysql2 :: Error: Table'(app name) _development.relationships' doesn't exist I searched for "migration Table doesn't exist" and found this article. Reference article: Notes on migration files after migrating to Rails 5.1

routing

routes.rb


resources :users do
  member do
    get :following, :followers
  end
end
Q. What is member? What do you mean?

Reference article: I tried to explain the difference between members and collection in routes.rb of rails in an easy-to-understand manner. ~ Rails from beginner to intermediate ~

following_user GET    /users/:id/following(.:format)                                                           users#following
followers_user GET    /users/:id/followers(.:format)                                                           users#followers

As you can see from the URL,: id will be added automatically.

Articles that I used as a reference

Add a follow function with rails. I implemented the follow function while looking at this article. How to put an index in the database What is the difference between find_or_initialize_by and find_or_create_by? How do I use the methods I wrote in the Rails model in the controller?

Recommended Posts

Where the follow function implementation is stuck
Follow function (Ajax) implementation
Where is the Java LocalDateTime.now () timezone?
Like function The part that is stuck in making it asynchronous
[Ruby on Rails] Follow function implementation: Bidirectional
[Rails 6] Asynchronous (Ajax) follow function is implemented
[Rails] I will explain the implementation procedure of the follow function using form_with.
Rails follow function
[Java] Where is the implementation class of annotation that exists in Bean Validation?
[Swift] This is the solution! Illustration of Delegate implementation
About the function double-java
Implementation of search function
Let's understand the function!
Rails search function implementation
Implementation of pagination function
Search function [copype implementation]
[Java] Note the case where equals is false but == is ture.
Follow function association memorandum (understand the description of the User model)
Chat function that is updated immediately (implementation of Action Cable)