... | @@ -4,7 +4,7 @@ Provides a way to write auxiliary statements for use in a larger query. It's rec |
... | @@ -4,7 +4,7 @@ Provides a way to write auxiliary statements for use in a larger query. It's rec |
|
|
|
|
|
### Model
|
|
### Model
|
|
|
|
|
|
First you need to configure the statements that you want to use. These statements are very similar to scopes, but with a little more options:
|
|
First you need to configure the statements you want to use. These statements are very similar to scopes, but with a little more options:
|
|
```ruby
|
|
```ruby
|
|
# models/user.rb
|
|
# models/user.rb
|
|
class User < ActiveRecord::Base
|
|
class User < ActiveRecord::Base
|
... | @@ -17,15 +17,15 @@ class User < ActiveRecord::Base |
... | @@ -17,15 +17,15 @@ class User < ActiveRecord::Base |
|
end
|
|
end
|
|
```
|
|
```
|
|
|
|
|
|
The statement is lazy load, so the block is called only when a query requires its usage. To configure your statement, those are the options available:
|
|
The statement is lazy load, so the block is called only when a query requires its usage. To configure your statement, these are the options available:
|
|
|
|
|
|
`cte.query` The query that will be performed inside the auxiliary statement (WITH). It most likely to be queries that brings extra information related to the main class, in this case the `User` class.
|
|
`cte.query` The query that will be performed inside the auxiliary statement (WITH). It most likely will be one that brings extra information related to the main class, in this case the `User` class.
|
|
|
|
|
|
`cte.attributes` The list of attributes that will be exposed to the main query and after be able to access through the entries fetch. It's read as `The column form the query => The alias exposed`. It accepts join columns in the left side as `'table.column' => 'alias'`.
|
|
`cte.attributes` The list of attributes that will be exposed to the main query and after it is able to access through the entries fetch. It's read as `The column form the query => The alias exposed`. It accepts join columns in the left side as `'table.column' => 'alias'`.
|
|
|
|
|
|
`cte.join_type` The type of join between the main query and statement query. By default it's set to `:inner`, that will perform an `INNER JOIN`. The options are `:inner, :left, :right, :full`.
|
|
`cte.join_type` The type of join between the main query and statement query. By default it's set to `:inner`, that will perform an `INNER JOIN`. The options are `:inner, :left, :right, :full`.
|
|
|
|
|
|
`cte.join` The columns to be used on the join in the main query. It has similar behavior as the attributes, and it's read as `The column from the main query == The column from the statement query`. It accepts join columns in both sides as `'table.column' => 'table.column'`.
|
|
`cte.join` The columns used on the join in the main query. It has similar behavior as the attributes, and it's read as `The column from the main query == The column from the statement query`. It accepts join columns in both sides as `'table.column' => 'table.column'`.
|
|
|
|
|
|
### Querying
|
|
### Querying
|
|
```ruby
|
|
```ruby
|
... | | ... | |