module

Lustra::SQL::Query::Pluck

Instance methods

pluck(fields : Tuple(*T)) forall T

Select specifics columns and return an array of Tuple(*Lustra::SQL::Any) containing the columns in the order of the selected arguments:

User.query.pluck("first_name", "last_name").each do |(first_name, last_name)|
  # ...
end
Source
pluck(*fields) : Array

Select specifics columns and return an array of Tuple(*Lustra::SQL::Any) containing the columns in the order of the selected arguments:

User.query.pluck("first_name", "last_name").each do |(first_name, last_name)|
  # ...
end
Source
pluck

Select specifics columns and returns on array of tuple of type of the named tuple passed as parameter:

User.query.pluck(id: Int64, "UPPER(last_name)": String).each do #...
Source
pluck_col(field : Lustra::SQL::Symbolic, type : T.class) : Array(T) forall T

Select a specific column of your SQL query, execute the query and return an array containing this field.

User.query.pluck_col("id") # [1,2,3,4...]

Note: It returns an array of Lustra::SQL::Any. Therefore, you may want to use pluck_col(str, Type) to return an array of Type:

User.query.pluck_col("id", Int64)

The field argument is a SQL fragment; it's not escaped (beware SQL injection) and allow call to functions and aggregate methods:

# ...
User.query.pluck_col("CASE WHEN id % 2 = 0 THEN id ELSE NULL END AS id").each do
# ...
Source

Select a specific column of your SQL query, execute the query and return an array containing this field.

User.query.pluck_col("id") # [1,2,3,4...]

Note: It returns an array of Lustra::SQL::Any. Therefore, you may want to use pluck_col(str, Type) to return an array of Type:

User.query.pluck_col("id", Int64)

The field argument is a SQL fragment; it's not escaped (beware SQL injection) and allow call to functions and aggregate methods:

# ...
User.query.pluck_col("CASE WHEN id % 2 = 0 THEN id ELSE NULL END AS id").each do
# ...
Source