Client.where(active: true).pluck(:id) |
SELECT id FROM clients WHERE active = 1 |
# => [1, 2, 3] |
Client.distinct.pluck(:role) |
SELECT DISTINCT role FROM clients |
# => ['admin', 'member', 'guest'] |
Client.pluck(:id, :name) |
SELECT clients.id, clients.name FROM clients |
# => [[1, 'David'], [2, 'Jeremy'], [3, 'Jose']] |
pluck can be used to query single or multiple columns from the underlying table of a model.
pluck makes it possible to replace code like Client.select(:id).map { |c| c.id }
Unlike select, pluck directly converts a database result into a Ruby Array, without constructing ActiveRecord objects
pluck makes it possible to replace code like Client.select(:id).map { |c| c.id }
Unlike select, pluck directly converts a database result into a Ruby Array, without constructing ActiveRecord objects
Comments
Related