module Jennifer::Model::RelationDefinition

Direct including types

Defined in:

jennifer/model/relation_definition.cr

Instance Method Summary

Macro Summary

Instance Method Detail

def append_relation(name : String, hash) #

[View source]
abstract def get_relation(name : String) #

[View source]
def relation_retrieved(name : String) #

[View source]
abstract def set_inverse_of(name : String, object) #

[View source]

Macro Detail

macro belongs_to(name, klass, request = nil, foreign = nil, primary = nil, dependent = :none, polymorphic = false, foreign_type = nil) #

Specifies a one-to-one association with another class.

This macro should only be used if this class contains the foreign key. If the other class contains the foreign key, then you should use has_one instead.

Options:

  • name - relation name
  • klass - specify the class name of the association; in case of polymorphic relation use Union(Class1 | Class2) syntax
  • request - extra request scope to retrieve a specific set of records when access the associated collection (ATM only WHERE conditions are respected)
  • foreign - specify the foreign key used for the association
  • primary - specify the name of the column to use as the primary key for the relation
  • dependent - specify the destroy strategy of the associated objects when their owner is destroyed; available options are: destroy, delete, nullify (exception is polymorphic relation), restrict_with_exception
  • polymorphic - specifies that this relation is a polymorphic
  • foreign_type - specify the column used to store the associated object's type, if this is a polymorphic relation

Methods will be added for retrieval and query for a single associated object, for which this object holds an id:

association is a placeholder for the symbol passed as the name argument.

  • .association_relation
  • #association
  • #association!
  • #append_association(rel)
  • #add_association(rel)
  • #remove_association
  • #association_query
  • #association_reload

Polymorphic relation also generates next methods

  • #association_class_name - returns casted related object to ClassName
  • #association_class_name? - returns whether related object is a ClassName

[View source]
macro has_and_belongs_to_many(name, klass, request = nil, foreign = nil, primary = nil, join_table = nil, association_foreign = nil) #

Specifies a many-to-many relationship with another class.

This associates two classes via an intermediate join table. Unless the join table is explicitly specified as an option, it is guessed using the lexical order of the class names. So a join between Developer and Project will give the default join table name of "developers_projects" because "D" precedes "P" alphabetically. Note that this precedence is calculated using the < operator for String. This means that if the strings are of different lengths, and the strings are equal when compared up to the shortest length, then the longer string is considered of higher lexical precedence than the shorter one.

Options:

  • name - relation name
  • klass - specify the class name of the association
  • request - extra request scope to retrieve a specific set of records when access the associated collection (ATM only WHERE conditions are respected)
  • foreign - specify the foreign key used for the association
  • primary - specify the name of the column to use as the primary key for the relation
  • join_table - specifies the name of the join table if the default based on lexical order isn't what you want
  • association_foreign - specifies the foreign key used for the association on the receiving side of the association

The following methods for retrieval and query of a single associated object will be added:

association is a placeholder for the symbol passed as the name argument.

  • .association_relation
  • #association
  • #append_association(rel)
  • #add_association(rel)
  • #remove_association(rel)
  • #association_query
  • #association_reload

[View source]
macro has_many(name, klass, request = nil, foreign = nil, foreign_type = nil, primary = nil, dependent = :nullify, inverse_of = nil, polymorphic = false) #

Specifies a one-to-many association.

Options:

  • name - relation name
  • klass - specify the class name of the association
  • request - extra request scope to retrieve a specific set of records when access the associated collection (ATM only WHERE conditions are respected)
  • foreign - specify the foreign key used for the association
  • foreign_type - specify the column used to store the associated object's type, if this is a polymorphic relation
  • primary - specify the name of the column to use as the primary key for the relation
  • dependent - specify the destroy strategy of the associated objects when their owner is destroyed; available options are: destroy, delete, nullify, restrict_with_exception
  • inverse_of - specifies the name of the belongs_to relation on the associated object that is the inverse of this relation; required for polymorphic relation
  • polymorphic - specifies that this relation is a polymorphic

The following methods for retrieval and query of a single associated object will be added:

association is a placeholder for the symbol passed as the name argument.

  • .association_relation - returns association relation
  • #association - returns array of related objects
  • #append_association(rel) - builds related object from hash (or use given instance) and adds to relation
  • #add_association(rel) - insert given object to db and relation; doesn't support inverse_of option
  • #remove_association(rel) - removes given object from relation array
  • #association_query - returns association relation query for the object
  • #association_reload - reloads related objects from the DB.

[View source]
macro has_one(name, klass, request = nil, foreign = nil, foreign_type = nil, primary = nil, dependent = :nullify, inverse_of = nil, polymorphic = false) #

Specifies a one-to-one association with another class.

This macro should only be used if the other class contains the foreign key. If the current class contains the foreign key, then you should use belongs_to instead.

Options:

  • name - relation name
  • klass - specify the class name of the association
  • request - extra request scope to retrieve a specific set of records when access the associated collection (ATM only WHERE conditions are respected)
  • foreign - specify the foreign key used for the association
  • foreign_type - specify the column used to store the associated object's type, if this is a polymorphic relation
  • primary - specify the name of the column to use as the primary key for the relation
  • dependent - specify the destroy strategy of the associated objects when their owner is destroyed; available options are: destroy, delete, nullify, restrict_with_exception
  • inverse_of - specifies the name of the belongs_to relation on the associated object that is the inverse of this relation; required for polymorphic relation
  • polymorphic - specifies that this relation is a polymorphic

The following methods for retrieval and query of a single associated object will be added:

association is a placeholder for the symbol passed as the name argument.

  • .association_relation
  • #association
  • #association!
  • #append_association(rel)
  • #add_association(rel)
  • #remove_association
  • #association_query
  • #association_reload

[View source]