[英]Rails migration of an array inside a model doesn't work
我正在嘗試創建一個包含一些數組的模型。 通過此命令創建遷移文件后:
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並不明白這些字段是數組。 我嘗試了很多不同的東西。 但沒有運氣。 我將不勝感激任何幫助。 (這可能是一個菜鳥問題)
請檢查以下答案: Ruby on Rails:在表單中提交數組
所以你可以理解如何使用它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.