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

has_one

Previoushas_many throughNextLifecycle

Last updated 6 years ago

Has many represents the second counter part of relation. It assumes the current model is referenced by an object (or no objects) of another model.

Usually, it's used when another model optionally extend the current model by composition. A common example is the usage of User and UserInfo. UserInfo is setup after registration and filling of form from the user. An User can then exists without UserInfo – this handle all the connection lifecycle – while UserInfo will handle all the optional informations about the user.

class User
  include Clear::Model

  primary_key

  has_one user_info : UserInfo
end

class UserInfo
  include Clear::Model

  primary_key

  belongs_to user : User
end
belongs to