跳至內容 跳至搜尋

Active Record Schema

讓程式設計師以可攜式 DSL 編寫方式規劃架構。這表示您可以定義資料表、索引等,而無需直接使用 SQL,因此您的應用程式可以更輕易地支援多個資料庫。

用法

ActiveRecord::Schema[7.0].define do
  create_table :authors do |t|
    t.string :name, null: false
  end

  add_index :authors, :name, :unique

  create_table :posts do |t|
    t.integer :author_id, null: false
    t.string :subject
    t.text :body
    t.boolean :private, default: false
  end

  add_index :posts, :author_id
end

ActiveRecord::Schema 僅受同時支援遷移的資料庫配接器支援,這兩個功能非常相似。

命名空間
方式
#
包含模組

類別公開方法

[](version)

# File activerecord/lib/active_record/schema.rb, line 70
def self.[](version)
  @class_for_version ||= {}
  @class_for_version[version] ||= Class.new(Migration::Compatibility.find(version)) do
    include Definition
  end
end