Documentation Blog Community Forum Media
|
|
Sign In
    • Ash
      • Get Started
      • Philosophy
      • Why Ash
      • Extending Resources
    • AshPostgres
      • Get Started With Postgres
    • AshSqlite
      • Get Started With Sqlite
    • AshPhoenix
      • Getting Started With Ash And Phoenix
    • AshGraphql
      • Getting Started With Graphql
    • AshJsonApi
      • Getting Started With Json Api
    • AshAuthentication
      • Auth0 Quickstart
      • Getting Started With Authentication
      • Github Quickstart
      • Magic Links Quickstart
    • AshAuthenticationPhoenix
      • Getting Started With Ash Authentication Phoenix
      • Use Ash Authentication With Liveview
    • AshStateMachine
      • Get Started With State Machines
    • AshCSV
      • Get Started With Csv
    • Reactor
      • Getting Started With Reactor
    • Spark
      • Get Started With Spark
    • Ash
      • Actions
      • Aggregates
      • Atomics
      • Attributes
      • Bulk Actions
      • Calculations
      • Code Interface
      • Constraints
      • Development Utilities
      • Embedded Resources
      • Expressions
      • Flows
      • Glossary
      • Identities
      • Managing Relationships
      • Manual Actions
      • Monitoring
      • Multitenancy
      • Notifiers
      • Pagination
      • Phoenix
      • Policies
      • Pub Sub
      • Relationships
      • Security
      • Store Context In Process
      • Testing
      • Timeouts
      • Validations
    • AshPostgres
      • Migrations And Tasks
      • Polymorphic Resources
      • Postgres Expressions
      • References
      • Schema Based Multitenancy
    • AshSqlite
      • Migrations And Tasks
      • Polymorphic Resources
      • References
      • Sqlite Expressions
    • AshPhoenix
      • Working With Phoenix
    • AshGraphql
      • Graphql Generation
      • Modifying The Resolution
      • Relay
    • AshJsonApi
      • Open Api
      • Relationships
    • AshAuthentication
      • Custom Strategy
      • Policies On Authentication Resources
      • Upgrading
    • Ash
      • Contribute
      • Define Idiomatic Actions
      • Defining Manual Relationships
      • Handle Errors
      • Structure Your Project
      • Upgrade
      • Use Without Data Layers
      • Validate Changes
    • AshPostgres
      • Join Manual Relationships
      • Test With Postgres
      • Using Fragments
    • AshSqlite
      • Join Manual Relationships
      • Test With Sqlite
      • Using Fragments
    • AshGraphql
      • Authorize With Graphql
      • Handle Errors
      • Use Enums With Graphql
      • Use Json With Graphql
      • Use Subscriptions With Graphql
      • Use Unions With Graphql
    • Spark
      • Writing Extensions
    • AshGraphql
      • Monitoring
    • AshArchival
      • Archival
      • Unarchiving
    • Ash
      • Get Started
      • Philosophy
      • Why Ash
      • Extending Resources
    • AshPostgres
      • Get Started With Postgres
    • AshSqlite
      • Get Started With Sqlite
    • AshPhoenix
      • Getting Started With Ash And Phoenix
    • AshGraphql
      • Getting Started With Graphql
    • AshJsonApi
      • Getting Started With Json Api
    • AshAuthentication
      • Auth0 Quickstart
      • Getting Started With Authentication
      • Github Quickstart
      • Magic Links Quickstart
    • AshAuthenticationPhoenix
      • Getting Started With Ash Authentication Phoenix
      • Use Ash Authentication With Liveview
    • AshStateMachine
      • Get Started With State Machines
    • AshCSV
      • Get Started With Csv
    • Reactor
      • Getting Started With Reactor
    • Spark
      • Get Started With Spark
    • Ash
      • Actions
      • Aggregates
      • Atomics
      • Attributes
      • Bulk Actions
      • Calculations
      • Code Interface
      • Constraints
      • Development Utilities
      • Embedded Resources
      • Expressions
      • Flows
      • Glossary
      • Identities
      • Managing Relationships
      • Manual Actions
      • Monitoring
      • Multitenancy
      • Notifiers
      • Pagination
      • Phoenix
      • Policies
      • Pub Sub
      • Relationships
      • Security
      • Store Context In Process
      • Testing
      • Timeouts
      • Validations
    • AshPostgres
      • Migrations And Tasks
      • Polymorphic Resources
      • Postgres Expressions
      • References
      • Schema Based Multitenancy
    • AshSqlite
      • Migrations And Tasks
      • Polymorphic Resources
      • References
      • Sqlite Expressions
    • AshPhoenix
      • Working With Phoenix
    • AshGraphql
      • Graphql Generation
      • Modifying The Resolution
      • Relay
    • AshJsonApi
      • Open Api
      • Relationships
    • AshAuthentication
      • Custom Strategy
      • Policies On Authentication Resources
      • Upgrading
    • Ash
      • Contribute
      • Define Idiomatic Actions
      • Defining Manual Relationships
      • Handle Errors
      • Structure Your Project
      • Upgrade
      • Use Without Data Layers
      • Validate Changes
    • AshPostgres
      • Join Manual Relationships
      • Test With Postgres
      • Using Fragments
    • AshSqlite
      • Join Manual Relationships
      • Test With Sqlite
      • Using Fragments
    • AshGraphql
      • Authorize With Graphql
      • Handle Errors
      • Use Enums With Graphql
      • Use Json With Graphql
      • Use Subscriptions With Graphql
      • Use Unions With Graphql
    • Spark
      • Writing Extensions
    • AshGraphql
      • Monitoring
    • AshArchival
      • Archival
      • Unarchiving
View this guide on GitHub View this guide on Hex

Join Manual Relationships

Table of Contents

  1. Example

See Defining Manual Relationships for an idea of manual relationships in general. Manual relationships allow for expressing complex/non-typical relationships between resources in a standard way. Individual data layers may interact with manual relationships in their own way, so see their corresponding guides.

Example

# in the resource

relationships do
  has_many :tickets_above_threshold, Helpdesk.Support.Ticket do
    manual Helpdesk.Support.Ticket.Relationships.TicketsAboveThreshold
  end
end

# implementation
defmodule Helpdesk.Support.Ticket.Relationships.TicketsAboveThreshold do
  use Ash.Resource.ManualRelationship
  use AshPostgres.ManualRelationship

  require Ash.Query
  require Ecto.Query

  def load(records, _opts, %{query: query, actor: actor, authorize?: authorize?}) do
    # Use existing records to limit resultds
    rep_ids = Enum.map(records, & &1.id)
     # Using Ash to get the destination records is ideal, so you can authorize access like normal
     # but if you need to use a raw ecto query here, you can. As long as you return the right structure.

    {:ok,
     query
     |> Ash.Query.filter(representative_id in ^rep_ids)
     |> Ash.Query.filter(priority > representative.priority_threshold)
     |> Helpdesk.Support.read!(actor: actor, authorize?: authorize?)
     # Return the items grouped by the primary key of the source, i.e representative.id => [...tickets above threshold]
     |> Enum.group_by(& &1.representative_id)}
  end

  # query is the "source" query that is being built. 

  # _opts are options provided to the manual relationship, i.e `{Manual, opt: :val}`

  # current_binding is what the source of the relationship is bound to. Access fields with `as(^current_binding).field`

  # as_binding is the binding that your join should create. When you join, make sure you say `as: ^as_binding` on the
  # part of the query that represents the destination of the relationship

  # type is `:inner` or `:left`.
  # destination_query is what you should join to to add the destination to the query, i.e `join: dest in ^destination-query`
  def ash_postgres_join(query, _opts, current_binding, as_binding, :inner, destination_query) do
    {:ok,
     Ecto.Query.from(_ in query,
       join: dest in ^destination_query,
       as: ^as_binding,
       on: dest.representative_id == as(^current_binding).id,
       on: dest.priority > as(^current_binding).priority_threshold
     )}
  end

  def ash_postgres_join(query, _opts, current_binding, as_binding, :left, destination_query) do
    {:ok,
     Ecto.Query.from(_ in query,
       left_join: dest in ^destination_query,
       as: ^as_binding,
       on: dest.representative_id == as(^current_binding).id,
       on: dest.priority > as(^current_binding).priority_threshold
     )}
  end

  # _opts are options provided to the manual relationship, i.e `{Manual, opt: :val}`

  # current_binding is what the source of the relationship is bound to. Access fields with `parent_as(^current_binding).field`

  # as_binding is the binding that has already been created for your join. Access fields on it via `as(^as_binding)`

  # destination_query is what you should use as the basis of your query
  def ash_postgres_subquery(_opts, current_binding, as_binding, destination_query) do
    {:ok,
     Ecto.Query.from(_ in destination_query,
       where: parent_as(^current_binding).id == as(^as_binding).representative_id,
       where: as(^as_binding).priority > parent_as(^current_binding).priority_threshold
     )}
  end
end
Source Report an issue