Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • T torque-postgresql
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 6
    • Issues 6
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 1
    • Merge requests 1
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • Carlos
  • torque-postgresql
  • Wiki
  • Auxiliary Statements

Auxiliary Statements · Changes

Page history
Updated Auxiliary Statements (markdown) authored Aug 18, 2019 by Carlos's avatar Carlos
Hide whitespace changes
Inline Side-by-side
Auxiliary-Statements.md
View page @ e605d8b4
......@@ -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:
- `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.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` 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'`.
......@@ -70,4 +72,28 @@ 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
[`auxiliary_statement.send_arguments_key`](https://github.com/crashtech/torque-postgresql/wiki/Configuring#auxiliary_statement.send_arguments_key) config.
\ No newline at end of file
[`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
Clone repository
  • Wiki
  • Configuring

Core Extensions

  • Range

Data types

  • Enum
  • Enum Set
  • Interval
  • Date/Time Range
  • Box
  • Circle
  • Line
  • Segment

Querying

  • Arel
  • Has Many
  • Belongs to Many
  • Dynamic Attributes
  • Distinct On
  • Auxiliary Statements
  • Inherited Tables