Since we learned about the pluck method this time, we will output it.
What is the pluck method? How to use it?
I will describe about.
The pluck method is written as follows. "A method that returns the value of the column specified in the argument as an array."
The map method can be mentioned as a method with the same effect, but if you want to get the return value as an array, it is simpler and easier to use the pluck method.
Let's actually use it.
Suppose there are four people in the name column of the users table: "Tanaka", "Yoshida", "Suzuki", and "Takahashi". So, if you want to get only the name column of the user table, the pluck method is useful.
User.pluck(:name)
#Then the following processing is done
SELECT `users`.`name` FROM `users`
=> ["Tanaka", "Yoshida", "Suzuki", "Takahashi"] #Return value
This is very convenient when you want to get all the information registered in a table or database as an array.
Recommended Posts