ash v1.24.1 Ash.Resource.Dsl.ManyToMany View Source

Link to this section Summary

Functions

Declares a many_to_many relationship. Many to many relationships require a join table.

Link to this section Functions

Link to this macro

many_to_many(name, destination, opts \\ [])

View Source (macro)

Declares a many_to_many relationship. Many to many relationships require a join table.

A join table is typically a table who's primary key consists of one foreign key to each resource.

Examples

# In a resource called `Word`
many_to_many :books, Book,
  through: BookWord,
  source_field: :text,
  source_field_on_join_table: :word_text,
  destination_field: :id,
  destination_field_on_join_table: :book_id

Arguments

  • :name - The name of the relationship

  • :destination - The destination resource

Options

  • :source_field_on_join_table - Required. The field on the join table that should line up with source_field on this resource.

  • :destination_field_on_join_table - Required. The field on the join table that should line up with destination_field on the related resource. Default: [relationshihp_name]_id

  • :through - Required. The resource to use as the join resource.

  • :join_relationship - The has_many relationship to the join table. Defaults to <relationship_name>_join_assoc

  • :join_attributes - Attributes to expose as editable when modifying the relationship.

Extensions may use this when deciding what fields to render from the join table.

See Ash.Changeset.append_to_relationship/3 and Ash.Changeset.replace_relationship/3 for how to edit these fields. The default value is [].

  • :destination_field - The field on the related resource that should match the source_field on this resource. The default value is :id.

  • :source_field - The field on this resource that should match the destination_field on the related resource. The default value is :id.

  • :writable? - Whether or not the relationship may be edited. The default value is true.

  • :description - An optional description for the relationship

  • :private? - Whether or not the relationship will appear in any interfaces created off of this resource, e.g AshJsonApi and AshGraphql The default value is false.