module Rome::Associations
Direct including types
Defined in:
associations.crMacro Summary
-
belongs_to(name, class_name = nil, foreign_key = nil, autosave = nil, dependent = nil)
Declares a belongs to relationship.
-
has_many(name, class_name = nil, foreign_key = nil, autosave = nil, dependent = nil)
Declares a has many relationship.
-
has_one(name, class_name = nil, foreign_key = nil, autosave = nil, dependent = nil)
Declares a has one relationship.
Macro Detail
Declares a belongs to relationship.
This will add the following methods:
association
returns the associated object (or nil);association=
assigns the associated object, assigning the foreign key;build_association
builds the associated object, assigning the foreign key if the parent record is persisted, or delaying it to when the new record is saved;create_association
creates the associated object, assigning the foreign key, granted that validation passed on the associated object;create_association!
same ascreate_association
but raises a Rome::RecordNotSaved exception when validation fails;reload_association
to 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_name
overrides the association class name (inferred asname.camelcase
by default);foreign_key
overrides the foreign key on the association (inferred asname + "_id"
by default);autosave
can be either:nil
(default) to only save newly built associations when the parent record is saved,true
to always save the associations (new or already persisted),false
to never save the associations automatically.
dependent
can be either::delete
todelete
the associated record in SQL,:destroy
to call#destroy
on the associated object.
Declares a has many relationship.
This will add the following methods:
associations
returns aRelation(T)
;associations=
assigns the associated objects, assigning the associations' foreign key, then saving the associations; permanently deletes the previously associated objects;
For example an Account class declares has_many :sellers
which will add:
Account#sellers
Account#sellers=(sellers)
Options
class_name
overrides the association class name (inferred asname.singularize.camelcase
by default);foreign_key
overrides the foreign key for the association (inferred as the name of this class + "_id" by default);autosave
can be either:nil
(default) to only save newly built associations when the parent record is saved,true
to always save the associations (new or already persisted),false
to never save the associations automatically.
dependent
can be either::nullify
(default) to set the foreign key tonil
in SQL,:delete
todelete
the associated records in SQL,:destroy
to call#destroy
on each associated objects.
Declares a has one relationship.
This will add the following methods:
association
returns the associated object (or nil).association=
assigns the associated object, assigning the association's foreign key, then saving the association; permanently deletes the previously associated object;reload_association
to 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_supplier
Account#create_supplier
Account#create_supplier!
Account#reload_supplier
Options
class_name
overrides the association class name (inferred asname.camelcase
by default);foreign_key
overrides the foreign key for the association (inferred as the name of this class + "_id" by default);autosave
can be either:nil
(default) to only save newly built associations when the parent record is saved,true
to always save the associations (new or already persisted),false
to never save the associations automatically.
dependent
can be either::nullify
(default) to set the foreign key tonil
in SQL,:delete
todelete
the associated record in SQL,:destroy
to call#destroy
on the associated object.