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:

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 Post
  include Clear::Model

  primary_key

  column name : String
  column content : String?

  belongs_to category : Category
end

class Category
  include Clear::Model

  primary_key

  column name : String

  has_many posts : Post
end

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 Technology

c.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

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:

has_many relation_name : RelationType,
    foreign_key: "column_name", own_key: "column_name", no_cache: true|false

Adding to relation

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.

Last updated