[RUBY] Use the permutation method to make all the default users follow each other

Introduction

I will post to qiita for the first time. I am a beginner. We would appreciate it if you could point out any mistakes in the wording.

In the portfolio for job change activities being created, the users of the initial data were made to follow each other in seeds.rb. I received a reply from a company engineer who wantedly including a code review. Regarding this seeds.rb, it was pointed out that "the logic is abnormally difficult to understand".

Seeds.rb before modification

The code below made the default users follow each other.

db/seeds_relationships.rb


19.times do |n| #Repeat processing 19 times
  users = User.all #Assign all User objects to the variable users
  user = users.find(n + 1) #The id (n) specified from the variable users+1) Assign the User object to the variable user
  following = users[0..18] #1 of array users in variable following~Substitute up to 19th User object
  following.shift(n + 1) #From the beginning of array following with shift method(n+1)Get rid of one element
  followers = users[0..18] #1 of array users in variable followers~Substitute up to 19th User object
  followers.shift(n + 1) #From the beginning of the array followers with the shift method(n+1)Get rid of one element
  following.each { |followed| user.follow(followed) } #Each user whose user is included in the array following(followed)Follow
  followers.each { |follower| follower.follow(user) } #Each user contained in the array followers(follower)Follows user
end

You can see it at a glance, but it's unusually difficult to understand.

Use Array # permutation

The engineer pointed out as above, What is a permutation? I thought, and when I translated it, I found that it meant "permutation".

What is a permutation?

It is the number of different r pieces taken out from n different pieces and arranged in one column. [High school mathematics] Differences in permutations and combinations that can be seen from 1

It's a guy like AB, BA, BC, CB, AC, CA ().

Create a permutation

[1, 2, 3].permutation do |x|
  p x
end

The following permutations are created from the elements in the array.

[1, 2, 3]
[1, 3, 2]
[2, 1, 3]
[2, 3, 1]
[3, 1, 2]
[3, 2, 1]

By putting (n) after permutation, you can extract n different elements from the array and arrange them.

[1, 2, 3].permutation(2) do |x|
  p x
end
[1, 2]
[1, 3]
[2, 1]
[2, 3]
[3, 1]
[3, 2]

Source: Create a permutation

Modified seeds.rb

db/seeds_relationships.rb


users = User.all.to_a
users.permutation(2) do |n|
  n[0].follow(n[1])
end

1st user follows 2,3,4,5… second user, The second user follows the 1,3,4,5… second user, The third user follows the 1,2,4,5… third user, and becomes I was able to use the permutation method to make all the default users follow each other.

[Addition] Seeds.rb for easier viewing

db/seeds_relationships.rb


users = User.all.to_a
users.permutation(2) do |user1, user2|
  user1.follow(user2)
end

I received a comment and corrected it. Readability is pretty good!

environment

Recommended Posts

Use the permutation method to make all the default users follow each other
I want to use the sanitize method other than View.
How to use the link_to method
How to use the include? method
[Rails] How to use the map method
[Java] How to use the toString () method
When you want to use the method outside
Output of how to use the slice method
How to use the replace () method (Java Silver)
[Ruby basics] How to use the slice method
How to use the getter / setter method (in object orientation)
Prevent other users from going to the post edit screen
Easy to use array.map (&: method)
Use Modifier # isStatic to determine if the [Reflection] method is static.
[Rails] Don't use the select method just to narrow down the columns!
[Java] I tried to make a maze by the digging method ♪
[Ruby on Rails] Use the resources method to automatically create routes.