Skip to content

Commit

Permalink
arel: add
Browse files Browse the repository at this point in the history
  • Loading branch information
rstacruz committed Nov 22, 2015
1 parent 7e23c82 commit 4907f96
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .projections.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"*.md": {
"type": "cheat",
"template": [
"---",
"title: {basename|capitalize}",
"---"
]
}
}
52 changes: 52 additions & 0 deletions arel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---
title: Arel
---

### Tables

```rb
users = Arel::Table.new(:users)
```

### Fields

```rb
users[:name]
users[:id]
```

### `where` (restriction)

```rb
users.where(users[:name].eq('amy'))
# SELECT * FROM users WHERE users.name = 'amy'
```

### `select` (projection)

```rb
users.project(users[:id])
# SELECT users.id FROM users
```

### `join`

```rb
users.join(photos)
users.join(photos, Arel::Nodes::OuterJoin).on(users[:id].eq(photos[:user_id]))
```

### `limit` / `offset`

```rb
users.take(5) # => SELECT * FROM users LIMIT 5
users.skip(4) # => SELECT * FROM users OFFSET 4
```

### Aggregates

```rb
users.project(users[:age].sum) # .average .minimum .maximum
users.project(users[:id].count)
users.project(users[:id].count.as('user_count'))
```

0 comments on commit 4907f96

Please sign in to comment.