has_many
Has many represents one of the counter part of belongs to relation. It assumes the current model is referenced by a collection of another model.
Let's see the example used in the chapter belongs_to
:
Here, we said a category has many posts. The posts can be accessed through the method posts
which return a Collection
:
Note: The relation can be refined after fetching:
Customizing the relation
Clear uses naming convention to infer the name of the foreign key. You may want to override this behavior by adding some parameters:
Argument | Description | Default value |
| The foreign key which is inside the relative model |
|
| The key against what the relation is tested, primary key by default |
|
| Never cache the relation (note: planned feature) |
|
Adding to relation
An object can be added into the relation collection using <<
operator:
In this case, the post is saved during the add operation.
Last updated