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

Link to this section Summary

Functions

Declares a belongs_to relationship. In a relational database, the foreign key would be on the source table.

Link to this section Functions

Link to this macro

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

View Source (macro)

Declares a belongs_to relationship. In a relational database, the foreign key would be on the source table.

This creates a field on the resource with the corresponding name and type, unless define_field?: false is provided.

Examples

# In a resource called `Word`
belongs_to :dictionary_entry, DictionaryEntry,
  source_field: :text,
  destination_field: :word_text

Arguments

  • :name - The name of the relationship

  • :destination - The destination resource

Options

  • :primary_key? - Whether this field is, or is part of, the primary key of a resource. The default value is false.

  • :required? - Whether this relationship must always be present, e.g: must be included on creation, and never removed (it can still be changed) The default value is false.

  • :define_field? - If set to false a field is not created on the resource for this relationship, and one must be manually added in attributes. The default value is true.

  • :field_type - The field type of the automatically created field. The default value is :uuid.

  • :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. - Defaults to <name>_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.