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
  1. Model
  2. Associations

belongs_to

Belongs to represents an association where the associated object share its primary key in a column of the current object. Let's give an example:

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
)

In this case, Post belongs to categories, as it maintain a link to the category through category_id column.

In clear, this relation can be written like this:

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
  • Clear will take care for you of the declaration of the column category_id

  • You may notice has_many in Category model. We will go further onto it in the next chapter.

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:

belongs_to relation_name : RelationType, 
    foreign_key: "column_name", primary: true|false, 
    key_type: AnyType?

Argument

Description

Default value

foreign_key

The column used by the relation

[underscore_model_name]_id

primary

Set to true if the foreign_key is also the primary key of this table

false

key_type

The type of the column. Set to the primary key type of the relative table.

Int64?

no_cache

Never cache the relation (note: planned feature)

false

PreviousAssociationsNexthas_many

Last updated 6 years ago