What is the pluck method?

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.

What is the pluck method?

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.

How to use

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

What is the pluck method?
What is the initialize method?
What is the main method in Java?
What is the BufferedReader class?
What is the constructor for?
'% 02d' What is the percentage of% 2?
What kind of method is define_method?
What is the difference between an action and an instance method?
What is Cubby
What is Docker?
What is java
What is the right migration data type? ??
What is Keycloak
What is testing? ・ About the importance of testing
What is maven?
What is Jackson?
[For programming beginners] What is a method?
What is Docker
What is self
What is Jenkins
What is ArgumentMatcher?
What is IM-Juggling?
What is the best file reading (Java)
What is params
What is SLF4J?
About the method
What is Facade? ??
What is the model test code testing
What is Java <>?
What is Gradle?
What is POJO
What is Java
What is the data structure of ActionText?
What is the Facade pattern useful for?
What is centOS
What is RubyGem?
What is programming?
What is before_action?
What is Docker
What is Byte?
What is Tomcat
[Ruby] What happens if the method self is used as the return value?
What is JSP? ~ Let's know the basics of JSP !! ~
What is the Java Servlet / JSP MVC model?
The order of Java method modifiers is fixed
What is different from the PHP language. [Note]
What is the volatile modifier for Java variables?
What is the difference between SimpleDateFormat and DateTimeFormatter? ??
What is Maven Assembly?
What is `docker-compose up`?
What is a constructor?
What is vue cli
What is an interface?
What is Ruby's self?
What is hard coding?
What is a stream
What is Ruby's attr_accessor?
What is Java Encapsulation?
What is permission denied?
What is instance control?
What is an initializer?