具有ActiveModel属性的模拟SQL表

问题描述

我正在使用

ActiveModel::Model 
ActiveModel::Attributes.

我注意到在表单对象(活动模型)中使用的attribute方法不允许:text类型。我想知道如何设置属性来模仿下面的sql表?

create_table "teachers",force: :cascade do |t|
    t.string "name"
    t.text "credentials",default: [],array: true
    t.integer "age"
    t.boolean "working",default: false
    t.bigint "user_id"
    t.datetime "created_at",null: false
    t.datetime "updated_at",null: false
    t.index ["user_id"],name: "index_cpas_on_user_id"
  end

解决方法

只需使用attribute :my_attribute,:string

ActiveRecord处理VARCHAR列(由:string类型表示)和TEXT列的投射方式之间没有实际区别。

唯一的区别是迁移产生的SQL,因此使用哪种类型的列存储实际信息。

例如,在Postgres上,TEXTVARCHAR列的上限约为1GB,但是VARCHAR(n)允许您设置较低的任意限制,例如VARCHAR(255)-但两者都没有性能优势,因此使用TEXT类型也没有优势。当然,这取决于所使用的RDBMS。

请参阅: