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
:
CREATE TABLE categories (id bigserial NOT NULL PRIMARY KEY,name text NOT NULL)CREATE TABLE posts (id bigserial NOT NULL PRIMARY KEY,name text NOT NULL,content text,category_id bigint NOT NULL)
class Postinclude Clear::Modelprimary_keycolumn name : Stringcolumn content : String?belongs_to category : Categoryendclass Categoryinclude Clear::Modelprimary_keycolumn name : Stringhas_many posts : Postend
Here, we said a category has many posts. The posts can be accessed through the method posts
which return a Collection
:
c = Category.query.find!{name == "Technology"} # Retrieve the category named Technologyc.posts.each do |post|puts "Post name: #{post.name}"end
Note: The relation can be refined after fetching:
# Fetch only the posts which starts by a digit:c.posts.where{name =~ /^[0-9]/i}.each do |post|puts "Post name: #{post.name}"end
Clear uses naming convention to infer the name of the foreign key. You may want to override this behavior by adding some parameters:
has_many relation_name : RelationType,foreign_key: "column_name", own_key: "column_name", no_cache: true|false
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) |
|
An object can be added into the relation collection using <<
operator:
c.posts << Post.new({name: "A good post"})
In this case, the post is saved during the add operation.