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) 链接
来源: 显示 | 在 GitHub 上
# 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