Clear ORM
  • Welcome to Clear
  • Introduction
    • Setup
  • Model
    • Defining your model
      • Describing your columns
      • Primary Keys
      • Converters
    • Associations
      • belongs_to
      • has_many
      • has_many through
      • has_one
    • Lifecycle
      • Persistence
      • Validations
      • Triggers
    • Batchs operations
      • Bulk update
      • Bulk insert & delete
    • Transactions & Save Points
      • Transaction & Savepoints
      • Connection pool
    • Locks
  • Querying
    • The collection object
      • Filter the query
        • Filter the query – The Expression Engine
        • Find, First, Last, Offset, Limit
        • Aggregation
        • Ordering & Group by
      • Fetching the query
        • Each and Fetch
        • Cursored fetching
        • Model extra attributes
      • Joins
      • Eager Loading
      • Window and CTE
      • Scopes
    • Writing low-level SQL
      • Select Clause
      • Insert Clause
      • Delete Clause
  • Migrations
    • Manage migrations
    • Call migration script
    • Migration CLI
  • Additional and advanced features
    • JSONB
    • Symbol vs String
    • Enums
    • BCrypt
    • Full Text Search
    • Handling multi-connection
  • Other resources
    • API Documentation
    • Inline documentation
    • Github repository
    • Credits
    • Benchmark
Powered by GitBook
On this page
  • Customizing the relation
  • Adding to relation
  1. Model
  2. Associations

has_many

Previousbelongs_toNexthas_many through

Last updated 4 years ago

Has many represents one of the counter part of relation. It assumes the current model is referenced by a collection of another model.

Let's see the example used in the chapter :

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

Argument

Description

Default value

foreign_key

The foreign key which is inside the relative model

[underscore_model_name]_id

own_key

The key against what the relation is tested, primary key by default

self.class.__pkey__

no_cache

Never cache the relation (note: planned feature)

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.

belongs to
belongs_to