PgORM::Associations
Macros
__process_assoc_serialization__
Sourcebelongs_to(name, class_name = nil, foreign_key = nil, autosave = nil, dependent = nil)
Declares a belongs to relationship.
The foreign key attribute is not declared by this macro and must be
declared on the model (e.g. attribute author_id : Int64?).
This will add the following methods:
associationreturns the associated object (raises::PgORM::Error::RecordNotFoundif it doesn't exist);association?returns the associated object, or nil if it doesn't exist;association=assigns the associated object, assigning the foreign key if the object is persisted;build_associationbuilds the associated object, assigning the foreign key if the parent record is persisted, or delaying it to when the new record is saved;create_associationcreates the associated object withcreate, assigning the foreign key;create_association!same ascreate_associationbut usescreate!. Both raise::PgORM::Error::RecordInvalidwhen validation fails;reload_associationto reload the associated object.
For example a Book class declares belongs_to :author which will add:
Book#author(similar toAuthor.find(author_id))Book#author=(author)(similar tobook.author_id = author.id)Book#build_author(similar to book.author = Author.new)Book#create_author(similar to book.author = Author.create)Book#create_author!(similar to book.author = Author.create!)Book#reload_author(force reload book.author)
Options
class_nameoverrides the association class name (inferred asname.camelcaseby default);foreign_keyoverrides the foreign key on the association (inferred asname + "_id"by default);autosavecan be either:nil(default) to only save newly built associations when the parent record is saved,trueto always save the associations (new or already persisted),falseto never save the associations automatically.
dependentcan be either:nil(default) to do nothing,:deletetodeletethe associated record in SQL,:destroyto call#destroyon the associated object.
has_many(name, class_name = nil, foreign_key = nil, autosave = nil, dependent = nil, serialize = false)
Declares a has many relationship.
Adds an association method returning a ::PgORM::Relation of the
associated records. The foreign key attribute is not declared by this
macro and must be declared on the associated model.
Options
class_nameoverrides the association class name (inferred asnamewith a trailing "s" removed, camelcased, by default);foreign_keyoverrides the foreign key for the association (inferred as the name of this class with a trailing "s" removed, underscored, + "_id" by default);autosavecan be either:nil(default) to only save newly built associations when the parent record is saved,trueto always save loaded or built associations,falseto never save the associations automatically.
dependentcan be either:nil(default) to do nothing,:nullifyto set the foreign key tonilin SQL,:delete_allto delete the associated records in SQL,:destroyto call#destroyon each associated object.
serializewhentrueincludes the associated records into_jsonwhen they have been loaded (e.g. by a join or by iterating the relation).
has_one(name, class_name = nil, foreign_key = nil, autosave = nil, dependent = nil)
Declares a has one relationship.
The foreign key attribute is not declared by this macro and must be declared on the associated model.
This will add the following methods:
associationreturns the associated object (raises::PgORM::Error::RecordNotFoundif it doesn't exist);association?returns the associated object, or nil if it doesn't exist;association=assigns the associated object. If this record is persisted, the previously associated record is deleted (dependent: :delete), destroyed (dependent: :destroy) or otherwise has its foreign key set tonil, then the new object's foreign key is assigned and it is saved;build_associationbuilds the associated object, assigning its foreign key if this record is persisted;create_associationandcreate_association!build and save the associated object, raising::PgORM::Error::RecordInvalidwhen validation fails, likecreate. Both raise::PgORM::Error::RecordNotSavedif this record has no id;reload_associationto reload the associated object.
For example an Account class declares has_one :supplier which will add:
Account#supplier(similar toSupplier.find_by(account_id: account.id))Account#supplier=(supplier)(similar tosupplier.account_id = account.id)Account#build_supplierAccount#create_supplierAccount#create_supplier!Account#reload_supplier
Options
class_nameoverrides the association class name (inferred asname.camelcaseby default);foreign_keyoverrides the foreign key for the association (inferred as the name of this class + "_id" by default);autosavecan be either:nil(default) to only save newly built associations when the parent record is saved,trueto always save the associations (new or already persisted),falseto never save the associations automatically.
dependentcan be either:nil(default) to do nothing,:nullifyto set the foreign key tonilin SQL,:deletetodeletethe associated record in SQL,:destroyto call#destroyon the associated object.