Update "Active Record Query Interface" guide [ci skip]

- Add missing `LIMIT 1` for some queries
  - Make some examples of query more readable
This commit is contained in:
bogdanvlviv 2017-12-04 01:18:59 +02:00
parent 33a3f7123b
commit 20bb26a428
No known key found for this signature in database
GPG Key ID: E4ACD76A6DB6DFDD
1 changed files with 9 additions and 9 deletions

View File

@ -801,7 +801,7 @@ The SQL that would be executed:
SELECT * FROM articles WHERE id > 10 ORDER BY id DESC
# Original query without `only`
SELECT "articles".* FROM "articles" WHERE (id > 10) ORDER BY id desc LIMIT 20
SELECT * FROM articles WHERE id > 10 ORDER BY id DESC LIMIT 20
```
@ -820,14 +820,14 @@ Article.find(10).comments.reorder('name')
The SQL that would be executed:
```sql
SELECT * FROM articles WHERE id = 10
SELECT * FROM articles WHERE id = 10 LIMIT 1
SELECT * FROM comments WHERE article_id = 10 ORDER BY name
```
In the case where the `reorder` clause is not used, the SQL executed would be:
```sql
SELECT * FROM articles WHERE id = 10
SELECT * FROM articles WHERE id = 10 LIMIT 1
SELECT * FROM comments WHERE article_id = 10 ORDER BY posted_at DESC
```
@ -1091,7 +1091,7 @@ This produces:
```sql
SELECT articles.* FROM articles
INNER JOIN categories ON articles.category_id = categories.id
INNER JOIN categories ON categories.id = articles.category_id
INNER JOIN comments ON comments.article_id = articles.id
```
@ -1871,14 +1871,14 @@ All calculation methods work directly on a model:
```ruby
Client.count
# SELECT count(*) AS count_all FROM clients
# SELECT COUNT(*) FROM clients
```
Or on a relation:
```ruby
Client.where(first_name: 'Ryan').count
# SELECT count(*) AS count_all FROM clients WHERE (first_name = 'Ryan')
# SELECT COUNT(*) FROM clients WHERE (first_name = 'Ryan')
```
You can also use various finder methods on a relation for performing complex calculations:
@ -1890,9 +1890,9 @@ Client.includes("orders").where(first_name: 'Ryan', orders: { status: 'received'
Which will execute:
```sql
SELECT count(DISTINCT clients.id) AS count_all FROM clients
LEFT OUTER JOIN orders ON orders.client_id = clients.id WHERE
(clients.first_name = 'Ryan' AND orders.status = 'received')
SELECT COUNT(DISTINCT clients.id) FROM clients
LEFT OUTER JOIN orders ON orders.client_id = clients.id
WHERE (clients.first_name = 'Ryan' AND orders.status = 'received')
```
### Count