It is sudden…
① Imagine a "colleague who can work tremendously" near you. ② Your "wife". Or think of an "ideal woman (male)".
A method that merges the two. I can work tremendously, yet cheerful and soft. That is the "pluck method" introduced this time.
(... I'm sorry I'll do it seriously)
This article
I will explain about. (* This article is useless for those who have already mastered the pluck method.)
In conclusion, __ "get the value of a column in an array" __ method.
Let's take a table as an example.
id | name | age |
---|---|---|
1 | ken | 21 |
2 | takuya | 22 |
3 | masato | 29 |
4 | kouta | 24 |
5 | hirokatsu | 23 |
6 | yuuhei | 28 |
7 | takumi | 25 |
8 | akihito | 26 |
For example, if you have a table like this, you can use the pluck method to
users_controller.rb
@users = User.pluck(:id)
#Output result below
@users = [1, 2, 3, 4, 5, 6, 7, 8]
In this way, by setting __model name.pluck (column name) __, the contents of that column could be obtained as an array.
You can also specify a second argument ...
users_controller.rb
@users = User.pluck(:id, :name) #Specify name as the second argument
#Output result below
@users = [ [1, "ken"],[2, "takuya"],[3, "masato"],[4, "kouta"],
[5, "hirokatsu"],[6, "yuuhei"],[7, "takumi"],[8, "akihito"] ]
It is also possible to pass multiple arguments in this way and get it as an array. Even if you pass the 3rd and 4th arguments, you will get it as an array in the same way.
By the way, since it is an array, the acquisition method is slightly different,
users_controller.rb
@users = User.pluck(:id, :name) #Specify name as the second argument
@users[0][1] = "ken" #First array[1,"ken"]Because it is the second element of"ken"Get
@users[3][0] = 1 #4th array[4,"kouta"]Get 1 because it is the first element of
You can get the contents like this.
So what can you do with the pluck method?
For example, if you want to find out if there is 24-year-old data in the User model,
users_controller.rb
User.where(age: 24).exists?
# => true
You can get it by doing the above, but if you use the pluck method
users_controller.rb
User.pluck(:age).include?(24)
# => true
You can get it with an expression like this: __ "... No, which one is fine?" __, but for these two I use pluck. I'll explain the reason next.
At first glance, the two formulas above look the same. However, the processing being performed and the number of data being acquired are completely different.
users_controller.rb
User.where(age: 24).exists?
↓ Here is the query (instruction for SQL) issued at this time ↓
MySQL
User Exists? (0.6ms) SELECT 1 AS one FROM `users` WHERE `users`.`age` = 24 LIMIT 1
=> true
So what happens when you use the pluck method?
users_controller.rb
User.pluck(:age).include?(24)
↓ Click here for the issued query (command to SQL) ↓
MySQL
(0.5ms) SELECT `users`.`age` FROM `users`
=> true
Do you see that it's obviously short? The processing time is also different by 0.1 milliseconds.
In the former, we are investigating "whether there is a 24-year-old user", whereas The latter is looking at "whether there is data for 24 years in the user's age".
In other words, by using pluck __ You can get only the columns you need, which means that your query will be shorter and faster __.
The more data you have, the wider the difference in processing speed. For those who say, "I made a portfolio, but the processing is somewhat sluggish ...", why not review the description of data acquisition?
① The pluck method is the __ "get the value of a certain column as an array" __ method (2) By using pluck, in some cases, useless query issuance is suppressed and __ processing speed is improved __
I hope you can understand the goodness of the pluck method even a little ... Thank you for watching until the end.
Recommended Posts