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

2 minute read

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

  • Ruby 2.6.5
  • Rails 5.2.4.3