Ruby on rails 创建新记录会自动用类名填充类型列
我是rails新手,正在研究我在文件task.rb中看到的继承代码,如下所示:Ruby on rails 创建新记录会自动用类名填充类型列,ruby-on-rails,activerecord,Ruby On Rails,Activerecord,我是rails新手,正在研究我在文件task.rb中看到的继承代码,如下所示: class Task < ApplicationRecord include ActiveRecord::UnionScope default_scope { where.not(status: 'Canceled') } scope :done, -> { where(status: 'Done').order(id: :desc) } scope :pending, -> {
class Task < ApplicationRecord
include ActiveRecord::UnionScope
default_scope { where.not(status: 'Canceled') }
scope :done, -> { where(status: 'Done').order(id: :desc) }
scope :pending, -> { where(status: 'Pending').order(id: :desc) }
scope :in_progress, -> { where.not(status: 'Done').order(id: :asc) }
def type_human_readable
self.class.to_s
end
end
class SwitchDbConfigFiles < Task
def type_human_readable
'Switch Database Configuration Files'
end
end
问题是:究竟是如何(根据什么规则)为type列分配模型类名的?在我看来,这与ActiveRecord类似。默认情况下,
type
属性用于此目的,因此会自动设置
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end
create_table "tasks", id: :integer, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.datetime "created_at"
t.datetime "updated_at"
t.string "type"
t.date "date"
t.text "message"
t.string "status", default: "Pending", null: false
t.text "data"
t.index ["date", "status", "type"], name: "last_task_of_type_3"
t.index ["date", "type", "status"], name: "last_task_of_type_2"
t.index ["type", "status", "date"], name: "last_task_of_type"
end