> For the complete documentation index, see [llms.txt](https://ploegert.gitbook.io/til/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ploegert.gitbook.io/til/programmy/rails/get-an-array-of-values-from-the-database.md).

# Get An Array Of Values From The Database

We generally get data from our database through [ActiveRecord models](https://api.rubyonrails.org/classes/ActiveRecord/Base.html):

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

If we need to do a more specialized query, we might reach for [`execute`](https://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/DatabaseStatements.html#method-i-execute):

```ruby
> 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`](https://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/DatabaseStatements.html#method-i-select_values) or [`select_rows`](https://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/DatabaseStatements.html#method-i-select_rows):

```ruby
> 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`.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ploegert.gitbook.io/til/programmy/rails/get-an-array-of-values-from-the-database.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
