Skip to content

Latest commit

 

History

History
40 lines (32 loc) · 1.37 KB

get-an-array-of-values-from-the-database.md

File metadata and controls

40 lines (32 loc) · 1.37 KB

Get An Array Of Values From The Database

We generally get data from our database through ActiveRecord models:

> Product.where(available: true).pluck(:sku)
[ "efg-1234", "pqr-3455", ... ]

If we need to do a more specialized query, we might reach for execute:

> ActiveRecord::Base.connection.execute(<<-SQL)
    select split_part(sku, '-', 1) product_type
      from products
      where available = true;
  SQL
[{ "product_type" => "efg" }, { "product_type" => "pqr" }, ... ]

The results are bundled up in a predictable, but verbose array of hashes.

We could trim the result down to just the values using either select_values or select_rows:

> ActiveRecord::Base.connection.select_values(<<-SQL)
    select split_part(sku, '-', 1) product_type
      from products
      where available = true;
  SQL
[ "efg", "pqr", ... ]

If the SQL statement is to return more than one row in the result, then you'll want select_rows instead of select_values.