【发布时间】:2016-07-16 22:52:36
【问题描述】:
我正在尝试创建一个模型,其中包含一些数组。通过此命令创建迁移文件后:
rails g model post poster_first_name:string poster_first_name:string email:string poster_id:integer poster_profile_pic:string message:string post_type:string comments:text
迁移文件已创建,我将迁移文件修改为如下所示(尝试创建 post_type 和 comments 数组):
class CreatePosts < ActiveRecord::Migration
def change
create_table :posts do |t|
t.integer :poster_id, null: false, default: 0
t.string :poster_first_name, default: ""
t.string :poster_last_name, default: ""
t.string :poster_profile_pic, default: ""
t.string :message, null: false, default: ""
t.string :post_type, array: true, default: []
t.text :comments, array: true, default: []
t.timestamps null: false
end
end
end
此迁移文件看起来正确。在我跑完rake db:migrate
这是我的架构的外观:
create_table "posts", force: :cascade do |t|
t.integer "poster_id", default: 0, null: false
t.string "poster_first_name", default: ""
t.string "poster_last_name", default: ""
t.string "poster_profile_pic", default: ""
t.string "message", default: "", null: false
t.string "post_type", default: "--- []\n"
t.text "comments", default: "--- []\n"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
所以基本上 rails 不明白这些字段是数组。我尝试了很多不同的东西。但没有运气。我将不胜感激任何帮助。 (这可能是一个菜鸟问题)
【问题讨论】:
-
您使用的 Rails 版本是什么?
-
我的 Rails 版本:Rails 4.2.6
标签: ruby-on-rails arrays ruby rails-migrations