... | @@ -20,7 +20,9 @@ end |
... | @@ -20,7 +20,9 @@ end |
|
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:
|
|
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 will be one 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.through` Define the name of the association to be used while joining the CTE to the source query.
|
|
- `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.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.requires` Provide a list of cross-dependent auxiliary statements, where they will all be added to the source query when requested.
|
|
- `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 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'`.
|
|
|
|
|
... | @@ -71,3 +73,27 @@ user = User.with(:comments, args: {id: 1}).order(:last_comment_content) |
... | @@ -71,3 +73,27 @@ user = User.with(:comments, args: {id: 1}).order(:last_comment_content) |
|
|
|
|
|
You can also change the name of the key used to pass arguments using
|
|
You can also change the name of the key used to pass arguments using
|
|
[`auxiliary_statement.send_arguments_key`](https://github.com/crashtech/torque-postgresql/wiki/Configuring#auxiliary_statement.send_arguments_key) config.
|
|
[`auxiliary_statement.send_arguments_key`](https://github.com/crashtech/torque-postgresql/wiki/Configuring#auxiliary_statement.send_arguments_key) config.
|
|
|
|
|
|
|
|
### Detached
|
|
|
|
|
|
|
|
Auxiliary statements can be defined detached from models. Which means that you can define them anywhere, even during the Controller operations, and just provide to the Relation as `.with(detached_cte)` and making sure that both can either be automatically joined or you provided a join.
|
|
|
|
|
|
|
|
```ruby
|
|
|
|
class HomeController < ApplicationController
|
|
|
|
def index
|
|
|
|
last_notification = TorqueCTE.create(:last_notification) do |cte|
|
|
|
|
cte.query Notification.distinct_on(:post_id).order(created_at: :desc)
|
|
|
|
cte.attributes content: :last_notification
|
|
|
|
end
|
|
|
|
|
|
|
|
@posts = Post.active.with(last_notification)
|
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
|
|
|
recipients = TorqueCTE.create(User.recipient_of(params[:id]).post_groupped)
|
|
|
|
@notifications = Notification.from_post(params[:id]).with(recipients, select: {
|
|
|
|
Arel.sql('array_agg("users"."email")') => :recipient_emails
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
``` |
|
|
|
\ No newline at end of file |