ash v0.1.1 Ash.Resource.Relationships View Source
DSL components for declaring relationships.
Relationships are a core component of resource oriented design. Many components of Ash
will use these relationships. A simple use case is side_loading (done via the side_load
option, given to an api action). A more complex use case might be building authorization
rules that grant access to a resource based on how the user is related to it.
Link to this section Summary
Functions
Declares a belongs_to relationship. In a relational database, the foreign key would be on the source table.
Declares a has_many relationship. There can be any number of related entities.
Declares a has_one relationship. In a relationsal database, the foreign key would be on the other table.
Declares a many_to_many relationship. Many to many relationships require a join table.
Link to this section Functions
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, unless define_field?: false is provided.
Practically speaking, a belongs_to and a has_one are interchangable in every way.
Opts
destination_field(atom): The field on the related resource that should match thesource_fieldon this resource. - Default: :idsource_field(atom): The field on this resource that should match thedestination_fieldon the related resource. Default: [relationship_name]_idprimary_key?(boolean): Whether this field is, or is part of, the primary key of a resource. - Default: falsedefine_field?(boolean): If set tofalsea field is not created on the resource for this relationship, and one must be manually added inattributes. - Default: truefield_type(atom): The field type of the automatically created field. - Default: :uuid
Examples
# In a resource called `Word`
belongs_to :dictionary_entry, DictionaryEntry,
source_field: :text,
destination_field: :word_text
Declares a has_many relationship. There can be any number of related entities.
Opts
destination_field(atom): The field on the related resource that should match thesource_fieldon this resource. Default: [resource.name]_idsource_field(atom): The field on this resource that should match thedestination_fieldon the related resource. - Default: :id
Examples
# In a resource called `Word`
has_many :definitions, DictionaryDefinition,
source_field: :text,
destination_field: :word_text
Declares a has_one relationship. In a relationsal database, the foreign key would be on the other table.
Generally speaking, a has_one also implies that the destination table is unique on that foreign key.
Practically speaking, a has_one and a belongs_to are interchangable in every way.
Opts
destination_field(atom): The field on the related resource that should match thesource_fieldon this resource. Default: [resource.name]_idsource_field(atom): The field on this resource that should match thedestination_fieldon the related resource. - Default: :id
Examples
# In a resource called `Word`
has_one :dictionary_entry, DictionaryEntry,
source_field: :text,
destination_field: :word_text
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.
You can specify a join table as a string or as another resource.
Opts
through(atom | string) Required: Either a string representing a table/generic name for the join table or a module name of a resource.source_field_on_join_table(atom): The field on the join table that should line up withsource_fieldon this resource. Default: [resource_name]_iddestination_field_on_join_table(atom): The field on the join table that should line up withdestination_fieldon the related resource. Default: [relationshihp_name]_idsource_field(atom): The field on this resource that should line up withsource_field_on_join_tableon the join table. - Default: :iddestination_field(atom): The field on the related resource that should line up withdestination_field_on_join_tableon the join table. - Default: :id
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