Skip to content
On this page

Ruby ActiveRecord

ruby

Generate ActiveRecord model classes with associations and validations from SQL schema

Example Output

Generated from the pet-store sample schema:

adoption.rb

ruby
# Generated by @sqldoc/templates/ruby-activerecord -- DO NOT EDIT
# frozen_string_literal: true

class Adoption < ApplicationRecord
  self.table_name = 'adoptions'

  belongs_to :owner
  belongs_to :pet

  attribute :adoption_fee, :decimal
end

adoptions_audit_log.rb

ruby
# Generated by @sqldoc/templates/ruby-activerecord -- DO NOT EDIT
# frozen_string_literal: true

class AdoptionsAuditLog < ApplicationRecord
  self.table_name = 'adoptions_audit_log'

  attribute :old_data, :json
  attribute :new_data, :json
end

category.rb

ruby
# Generated by @sqldoc/templates/ruby-activerecord -- DO NOT EDIT
# frozen_string_literal: true

class Category < ApplicationRecord
  self.table_name = 'categories'

  has_many :pets

  validates :name, presence: true
end

legacy_inventory.rb

ruby
# Generated by @sqldoc/templates/ruby-activerecord -- DO NOT EDIT
# frozen_string_literal: true

class LegacyInventory < ApplicationRecord
  self.table_name = 'legacy_inventory'
end

location.rb

ruby
# Generated by @sqldoc/templates/ruby-activerecord -- DO NOT EDIT
# frozen_string_literal: true

class Location < ApplicationRecord
  self.table_name = 'locations'

  has_many :reviews
end

medical_record.rb

ruby
# Generated by @sqldoc/templates/ruby-activerecord -- DO NOT EDIT
# frozen_string_literal: true

class MedicalRecord < ApplicationRecord
  self.table_name = 'medical_records'

  belongs_to :pet
end

owner.rb

ruby
# Generated by @sqldoc/templates/ruby-activerecord -- DO NOT EDIT
# frozen_string_literal: true

class Owner < ApplicationRecord
  self.table_name = 'owners'

  has_many :adoptions

  validates :name, presence: true
  validates :email, format: { with: Regexp.new("^[^@]+@[^@]+\\.[^@]+$") }
  validates :phone, length: { minimum: 7, maximum: 20 }
end

pet.rb

ruby
# Generated by @sqldoc/templates/ruby-activerecord -- DO NOT EDIT
# frozen_string_literal: true

class Pet < ApplicationRecord
  self.table_name = 'pets'

  belongs_to :category

  has_many :adoptions
  has_many :medical_records

  attribute :price, :decimal

  validates :name, presence: true
  validates :sku, format: { with: Regexp.new("^[A-Z]{3}-[0-9]{4}$") }
  validates :price, numericality: { greater_than_or_equal_to: 0, less_than_or_equal_to: 99999 }
end

review.rb

ruby
# Generated by @sqldoc/templates/ruby-activerecord -- DO NOT EDIT
# frozen_string_literal: true

class Review < ApplicationRecord
  self.table_name = 'reviews'

  belongs_to :location

  validates :rating, numericality: { greater_than_or_equal_to: 1, less_than_or_equal_to: 5 }
end

staff_audit_log.rb

ruby
# Generated by @sqldoc/templates/ruby-activerecord -- DO NOT EDIT
# frozen_string_literal: true

class StaffAuditLog < ApplicationRecord
  self.table_name = 'staff_audit_log'

  attribute :old_data, :json
  attribute :new_data, :json
end

Usage

Add to your sqldoc.config.ts:

typescript
export default {
  dialect: 'postgres',
  schema: ['./schema.sql'],
  codegen: [
    {
      template: 'ruby-activerecord',
      output: './generated/models/',
    },
  ],
}

Then run:

bash
sqldoc codegen