1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-06 08:38:01 +00:00

Add Query A Single Value From The Database as a rails til

This commit is contained in:
jbranchaud
2020-06-11 13:35:48 -05:00
parent c87a6ec2ad
commit b27879210f
2 changed files with 24 additions and 1 deletions

View File

@@ -0,0 +1,22 @@
# Query A Single Value From The Database
In a Rails context, most database interactions tend to happen through the ORM
(e.g. `Book.find("123")`). There is a general purpose escape hatch that lets
you execute a SQL statement directly against the DB -- `execute`. The resulting
value of `execute`, however, tends to be a little clunky to work with.
If you just need a single value from the DB, use the
[`select_value`](https://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/DatabaseStatements.html#method-i-select_value)
method.
```ruby
> statement = "select gen_random_uuid()"
> ActiveRecord::Base.connection.select_value(statement)
(5.0ms) select gen_random_uuid()
=> "abc2e780-f442-418b-afa3-56f0ccd0a903"
```
This is the cleanest way to get the result of a "single value" query.
If you happen to pass in a query that results in more than one row or column,
it will return the value of the first column from the first row.